如何在Jupyter中显示完整输出,而不仅仅是最后结果?


115

我希望Jupyter打印所有交互式输出,而不要打印,而不仅仅是最后的结果。怎么做?

范例:

a=3
a
a+1

我想展示

3
4


21
它并不经常使用,但是实际上有一个配置选项应该执行此操作- 在IPython内核配置文件中设置InteractiveShell.ast_node_interactivity为。'all'
托马斯K

谢谢托马斯,那是我想要的东西:)
mbh86 '16

它存在!!!
vasili111

Answers:


179

感谢Thomas,这是我一直在寻找的解决方案:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

2
这是一个很好的提示。-仅还原到输出的最后一行的标志值是什么?
Reblochon Masque

12
默认值为:“ last_expr”。您还可以在这里找到许多其他选项:ipython.readthedocs.org/en/stable/config/options/terminal.html
mbh86 '16

8
作为参考,选项为“全部”,“无”,“最后”和“ last_expr”。“ last”和“ last_expr”之间的区别:如果您的单元格以包含表达式的循环结尾,则“ last”将在循环的每次迭代中向您显示该表达式的结果。“ last_expr”(默认值)不会显示:仅在单元格末尾显示裸表达式的结果。
Thomas K

1
神圣的钼...此功能是杀手。
flow2k

新的(ish)last_expr_or_assign演示很神奇!不再需要多次重复键入相同的项目以使其也可以打印。
亨利·施莱纳

33

https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/

1)将此代码放在Jupyter单元格中:

from IPython.core.interactiveshell import InteractiveShell

InteractiveShell.ast_node_interactivity = "all"

2)在Windows中,以下步骤使更改永久生效。应该适用于其他操作系统。您可能必须更改路径。

C:\Users\your_profile\\.ipython\profile_default

使用以下代码在profile_defaults中创建一个ipython_config.py文件:

c = get_config()

c.InteractiveShell.ast_node_interactivity = "all"

0

每个笔记本的基础

正如其他人回答的那样,将以下代码放入Jupyter Lab或Jupyter Notebook单元将起作用:

from IPython.core.interactiveshell import InteractiveShell

InteractiveShell.ast_node_interactivity = "all"

永久改变

但是,如果您想永久保留它并使用Jupyter Lab,则需要创建一个IPython Notebook配置文件。运行以下命令来执行此操作(如果您使用Jupyter Notebook,则不要运行-以下是更多详细信息):

ipython profile create

如果您使用的是Jupyter Notebook,则应该已经创建了此文件,因此无需再次运行它。实际上,运行此命令可能会覆盖您当前的首选项。

创建此文件后,对于Jupyter Lab和Notebook用户一样,将以下代码添加到该文件中C:\Users\USERNAME\\.ipython\profile_default\ipython_config.py

c.InteractiveShell.ast_node_interactivity = "all"

我发现c = get_config()在新版本的Jupyter中没有必要 ,但是如果这对您不起作用,请c = get_config()在文件的开头添加。

有关以外的其他标志选项"all",请访问以下链接:https : //ipython.readthedocs.io/en/stable/config/options/terminal.html#configtrait-InteractiveShell.ast_node_interactivity

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.