Reputation: 361
Recently Jupyter (both Notebook and Lab) started to print anything as if I am executing each line separately. It used to print out the output of the very last line of the execution, but now it prints everything as shown below.
I can suppress this by doing, e.g., _ = axs[0].plot(~~~)
, but putting semicolon at the end of each line does not help. I have reinstalled Anaconda but nothing changed.
I am using macOS 10.14.6 on MBP 2018 15", Python 3.8 (basically everything that the current Anaconda installs, without any additional installation) on MS Edge 87.0.664.47.
EDIT:
More precisely, any line of code which has a return value that is not assigned to a variable is always printed. Previously, each line was "printed" only when it is the very last line of the code. This strange behavior happens regardless of packages, such as numpy or pandas:
(FYI, copiable text:
import numpy as np
import pandas as pd
def add_with_return(arr):
arr += 1
return arr
a = np.arange(10)
b = pd.DataFrame(dict(value=a))
a, b
add_with_return(a)
c = np.linspace(0, 1)
)
EDIT2:
Versions of packages I used
$ jupyter --version
jupyter core : 4.6.3
jupyter-notebook : 6.1.4
qtconsole : 4.7.7
ipython : 7.19.0
ipykernel : 5.3.4
jupyter client : 6.1.7
jupyter lab : 2.2.6
nbconvert : 6.0.7
ipywidgets : 7.5.1
nbformat : 5.0.8
traitlets : 5.0.5
EDIT 3:
I think I finally found a workaround:
Put ;
at the end of the last line of the code cell.
Example: add a dummy line None;
at the end of the cell to silence all the unwanted prints.
But once you have any line of code which does not have ;
at the end of it at the last line of the code cell, it prints everything again (whether or not you have ;
at the end of the other lines)
I want to hear if there is any better solution for this. (Who would ever want to put semicolon all the time...?)
Upvotes: 6
Views: 1316
Reputation: 4921
In IPython, there is an option InteractiveShell.ast_node_interactivity
that you can set to 'all', 'last', 'last_expr', 'none', or 'last_expr_or_assign'. The default setting is 'last_expr'. Find more information in the IPython documentation here.
When I type the following in a Jupyter cell, to imitate your situation.
InteractiveShell.ast_node_interactivity = 'all'
The code you provided:
import numpy as np
import pandas as pd
def add_with_return(arr):
arr += 1
return arr
a = np.arange(10)
b = pd.DataFrame(dict(value=a))
a, b
add_with_return(a)
c = np.linspace(0, 1)
It gives the following output:
(array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
value
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Finally using the following brings it back to the previous state.
InteractiveShell.ast_node_interactivity = 'last_expr'
Upvotes: 5
Reputation: 15588
The semicolon should work if placed on correct level.
Make sure ;
is set at last top level in multiple layers e.g.
fig, axes = plt.subplots(2);
You can then add ;
at the end of your code lines that generate unwanted outputs to mute them. E.g: ABC.plot(XYZ);
Updates: Mute ;
worked even when I placed only on the last line. If it does not, then the issue comes somewhere else.
Upvotes: 1