Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print all variables defined in one Jupyter cell

Is there an easier way to display the name and the value of all variables defined in a single cell in a pretty way?

The way I'm doing now is like this, but I waste a lot of time when there are 30 variables or more:

enter image description here

like image 520
Yuri F Becker Avatar asked Oct 19 '17 07:10

Yuri F Becker


People also ask

How to print the value of all the variables in Jupyter?

We can change the above behaviour and can easily ask the Jupyter notebook to print the value of all the variables in the cell and not just the last one. This can be done by adding only two lines of the code in our Jupyter notebook. Here’s how we can do it. Add the following lines in your Jupyter Notebook and execute them.

How to print multiple data frames in pretty-print format in Jupyter Notebook?

Edit: To print multiple data frames in pretty-print format from a single cell in Jupyter Notebook, use the following: from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = 'all' df df Doesn't work with large data frames with lots of columns.

How to inspect interactive variables in Jupyter/IPython?

Jupyter/IPython provide three helpful magics for inspecting variables. First, there is %who. With no arguments it prints all the interactive variables with minimal formatting. You can supply types to only show variables matching the type given. The %who_ls magic does the same thing, but returns the variables as a list.

What happens to variables in a notebook?

Variables, functions, classes, and any other code will continue to exist and possibly affect code in other cells. This causes some obvious problems, first for the current session of the notebook, and second for any future invocations of the notebook.


1 Answers

You can use whos command to see all variables stored in the current kernel.

k, g, lx = .4, .8, 6.6
m = k*g*lx**2
whos

outputs:

Variable   Type     Data/Info
-----------------------------
g          float    0.8
k          float    0.4
lx         float    6.6
m          float    13.939200000000001

But as said, it displays all variables, so it will display other variables from earlier cells you've run.

A similar result can be achieved using locals() or globals() command from python built-in functions, which return a dictionary of variables. But the way jupyter represents is prettier.


Alternatively you can use InteractiveShell. This will change the behavior of cells and act like a python shell would, so it will output every called value (to output cell) once run.

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

k
g
... do stuff ...
lx
m
... do more stuff ...

outputs:

Out[2]: 0.4
Out[2]: 0.8
Out[2]: 6.6
Out[2]: 13.939200000000001

And finally you can return the interactivity to default by setting it to last_expr.

InteractiveShell.ast_node_interactivity = "last_expr"

But the way you do it is probably the easiest and prettiest way, you can just remove the assignment on dataframe to make it a one liner or you can make it more compact to call by:

k, g, lx, m

Out[3]: (0.4, 0.8, 6.6, 13.939200000000001)
like image 166
umutto Avatar answered Oct 25 '22 21:10

umutto