正常情况下,jupyter notebook 一个cell只有一个输出
本文实现在服务器运行的jupyter notebook实现一个cell有多个输出,而不是只有最后一个输出
至于如何在服务器运行jupyter notebook 可以参考本人另外一篇文章
临时解决方案
在cell前面运行下面代码。
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
但是该方法需要在每个新的 .ipynb上输入,比较麻烦。可以采用第二种方案永久解决
永久解决方案
创建并修改jupyter notebook 配置文件
在终端输入,有则修改,无则创建
vim ~/.ipython/profile_default/ipython_config.py
shift + G 进入到最后一行,i 进入编辑模式,粘贴下列内容到最后一行
c = get_config()
c.InteractiveShell.ast_node_interactivity = "all"
esc 退出编辑模式,shift + : 进入命令模式,wq保存退出
刷新配置文件或重启jupyter notebook
刷新配置文件
source ~/.ipython/profile_default/ipython_config.py
重启jupyter notebook (推荐)
matplotlib绘图输出信息太多问题解决
-
经过前述操作后,会出现matplotlib绘图输出信息太多的问题,如下
-
在绘图命令最后加上半角的分号 ; 即可只输出最后图像
Enjoy It !
参考文献
https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/
https://ipython.readthedocs.io/en/stable/config/intro.html