Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute the code line by line in jupyter-notebook?

I'm reading the book, Python Machine Learning, and tried to analyze the code. But it offers only *.ipynb file and it makes me very bothersome.

For example,

enter image description here

In this code, I don't want to run whole In[9] but want to run line by line so that I can check each value of variable and know what each library function do.

Do I have to comment everytime I want to execute part of codes? I just want something like Execute the block part like in MATLAB

And also, let say I comment some part of code and execute line by line. How can I check each variable's value without using print() or display()? As you know, I don't have to use print() to check the value in python interactive shell in terminal. Is there a similar way in Jupyter?

like image 307
user3595632 Avatar asked Sep 10 '25 19:09

user3595632


1 Answers

ast_node_interactivity

In Jupyter Notebook or in IPython console, you can configure this behaviour with ast_node_interactivity:

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

Examples

With this config, every single line will be pretty printed, even if they're in the same cell.

  • In Notebook:

Jupyter notebook multiple lines

  • In IPython console:

IPython console multiple lines

Notes

  • None isn't displayed.

  • There are many other useful tips here ("28 Jupyter Notebook tips, tricks and shortcuts - Dataquest").

like image 59
Eric Duminil Avatar answered Sep 12 '25 11:09

Eric Duminil