Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Pretty Print in Jupyter Notebook

I am trying to pretty print a dict in Jupyter Notebook.

I am using the following:

import pprint
stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
stuff.insert(0, stuff[:])
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(stuff)

However upon pressing shift+enter, no [out] cell appear (i.e. I can't see the pretty printed output)

enter image description here

Any idea why this is so/ what should I change in order to see the pretty printed output?


Edit: Actually this is a python 2.7 problem - it works fine in 3.x. Have anyone tried it on python 2.7 and seen it work?

like image 574
jim jarnac Avatar asked Feb 27 '17 01:02

jim jarnac


People also ask

How do I get beautify code in Jupyter notebook?

a keyboard shortcut for reformatting the current code-cell (default shortcut is Ctrl-L , can also be configured not to add the keyboard shortcut). a keyboard shortcut for reformatting the whole notebook (default shortcut is Ctrl-Shift-L , can also be configured not to add the keyboard shortcut).

How do I print beautify in Python?

Use pprint() to Pretty Print a Dictionary in Python Within the pprint module there is a function with the same name pprint() , which is the function used to pretty-print the given string or object. First, declare an array of dictionaries. Afterward, pretty print it using the function pprint.

What is %% Writefile in Jupyter notebook?

%%writefile magic command in regular Python I just read what this does. It writes the contents of a Jupyter Cell to the specified file.

How do I change the text style in a Jupyter notebook?

If you want to change the text cell font and font size you can type the following in a cell and hit shift+enter.


1 Answers

Whatever you develop is correct. The only possible reason could be, Jupyter Notebook is unable to connect to the server. If you see connecting to Kernal/server like the image below in the toolbar, try to refresh the connection or reload the page.

enter image description here

I used the same code and I am able to see the output. check the image below.

enter image description here

like image 183
Pradeep Thimmireddy Avatar answered Oct 04 '22 16:10

Pradeep Thimmireddy