How can I execute the code line by line in jupyter-notebook?
Solution 1
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:
- In IPython console:
Notes
None
isn't displayed.There are many other useful tips here ("28 Jupyter Notebook tips, tricks and shortcuts - Dataquest").
Solution 2
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)
Solution 3
In PyCharm Jupyter Notebooks you can just right-click and split cell, the right click merge when you're done.
Related videos on Youtube
user3595632
Updated on July 09, 2022Comments
-
user3595632 almost 2 years
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,
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 inMATLAB
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()
ordisplay()
? As you know, I don't have to useprint()
to check the value inpython interactive shell
in terminal. Is there a similar way inJupyter
?-
AGS almost 8 yearsYou can quickly split that cell into multiple cells by putting your cursor at the desired location to split at, and press CTRL+SHIFT+-
-
Eric Duminil over 6 years@AGS Thanks. Somehow, it doesn't seem to work with the minus on the keypad.
-
Confounded over 4 yearsHi. Did you find a way to do this? I don't really like the suggestions involving cell-splitting. Ideally, I would like to be able just to select a line of code and execute it using some key-shortcut. Thanks
-
Erdogan CEVHER over 3 years@AGS, Thanks. This shortcut is what I was looking for.
-
-
Bruno Feroleto over 5 yearsThe difficulty with this solution is that all this copy and pasting is cumbersome (and might even have to be undone when the behavior of the original code is understood). Many editors allow selected lines to be executed, even when they are inside a function (but Jupyter doesn't, as of now): this is much lighter.
-
Basilique about 3 yearsthis solution did not work for me. when I Shift Enter it still runs the whole cell.
-
Basilique about 3 yearspython 3.8, latest version of jupyter (just installed). You would like to see the scree shot of which part exactly ?
-
Basilique about 3 yearsI think I had made a mistake, it works perfectly ! thanks a lot!