I asked on SO and reproduce here. A notebook with two cells:
In [1]
import numpy as np
import matplotlib.pyplot as plt
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all";
In [2]
%matplotlib inline
data ={'first':np.random.rand(100),
'second':np.random.rand(100)}
fig, axes = plt.subplots(2)
for idx, k in enumerate(data):
axes[idx].hist(data[k], bins=20);
does not suppress the output of the plt.hist()
:
Output from python -c "import IPython; print(IPython.sys_info())"
{'commit_hash': 'd86648c5d',
'commit_source': 'installation',
'default_encoding': 'UTF-8',
'ipython_path': '/Users/okomarov/anaconda/lib/python3.6/site-packages/IPython',
'ipython_version': '6.1.0',
'os_name': 'posix',
'platform': 'Darwin-16.7.0-x86_64-i386-64bit',
'sys_executable': '/Users/okomarov/anaconda/bin/python3',
'sys_platform': 'darwin',
'sys_version': '3.6.2 |Anaconda custom (x86_64)| (default, Jul 20 2017, '
'13:14:59) \n'
'[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]'}
You've set InteractiveShell.ast_node_interactivity = "all";
, so you've set all nodes to have ast interactivity enabled.
And ;
works only for the last top level expression, axes[idx].hist(data[k], bins=20);
is not a top level, as it is nested in the for
, the for last top level node is the for
, which is a statement.
Simply add a last no-op statement, and end it with ;
%matplotlib inline
data ={'first':np.random.rand(100),
'second':np.random.rand(100)};
fig, axes = plt.subplots(2);
for idx, k in enumerate(data):
axes[idx].hist(data[k], bins=20)
pass;
And you wont' have any outputs.
@Carreau Thanks for the clarification!
I could not find about:
And ; works only for the last top level expression
Is that documented somewhere? Would you like to copy-paste your answer in SO, so I can accept it?
Is that documented somewhere?
Probably not, the ast_interactivity
option is so rarely used that we don't really bother with the distinction as _most_ use case are only for the last expression.
Would you like to copy-paste your answer in so, so I can accept it?
Already did, with a couple of other details :-)
Most helpful comment
You've set
InteractiveShell.ast_node_interactivity = "all";
, so you've set all nodes to have ast interactivity enabled.And
;
works only for the last top level expression,axes[idx].hist(data[k], bins=20);
is not a top level, as it is nested in thefor
, the for last top level node is thefor
, which is a statement.Simply add a last no-op statement, and end it with
;
And you wont' have any outputs.