user3595632
user3595632

Reputation: 5730

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?

Upvotes: 26

Views: 46266

Answers (5)

Gerry Harp
Gerry Harp

Reputation: 159

Above there is the correct rigth answer, except it isn't actually an official "answer". It in the first comment after the first posting, which reads:

You can quickly split that cell 
into multiple cells by putting your  
cursor at the desired location to split at, 
and press CTRL+SHIFT+- – AGS
Jul 14, 2016 at 0:48  

This is the best correct answer for regular use. While you are programming you can insert breaks by "splitting the cell" at the breakpoint. The "split cell" option is found under the Edit tab (menu).

Upvotes: 0

user5807327
user5807327

Reputation: 111

I think you can print the variables between lines. it is the easy way

Upvotes: 0

Jonathan
Jonathan

Reputation: 7098

In PyCharm Jupyter Notebooks you can just right-click and split cell, the right click merge when you're done.

Upvotes: 3

Eric Duminil
Eric Duminil

Reputation: 54223

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").

Upvotes: 20

TheBlackCat
TheBlackCat

Reputation: 10298

You can just add new cells, then cut-and-paste the parts you want to the new cells. So, for example, you can put the imports and %matplotlib inline in the first cell (since those only ever need to be run when the notebook is first opened), the y generation in the second, the X generation in the third, and the plotting in the fourth. Then you can just run each cell one after another. That is just an example, you can split it up however you want (although I do recommend putting the imports together at the very beginning).

As for printing, if the last line in a cell is not assigned to a variable, it is automatically printed. So, for example, say the following is a cell:

y = df.iloc[0:100, 4].values
y = np.where(y == 'spam', -1, 1)
y

Then the contents of y will be displayed after the cell. Similarly, if you have a cell with these contents:

y = df.iloc[0:100, 4].values
y = np.where(y == 'spam', -1, 1)
y.sum()

Then the result of the y.sum() operation will be displayed after the cell. On the other hand, if the following cell is executed, then nothing is printed:

y = df.iloc[0:100, 4].values
y = np.where(y == 'spam', -1, 1)

Nor is anything printed for this one:

z = {}
y = df.iloc[0:100, 4].values
z['spam'] = np.where(y == 'spam', -1, 1)

Upvotes: 4

Related Questions