如何在 Jupyter 中显示完整输出,而不仅仅是最后的结果?
- 2025-02-17 09:25:00
- admin 原创
- 74
问题描述:
我希望 Jupyter 打印所有交互式输出,而不依赖于打印,而不仅仅是最后的结果。怎么做?
例子 :
a=3
a
a+1
我想展示
三
四
解决方案 1:
感谢托马斯,这是我正在寻找的解决方案:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
解决方案 2:
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:Usersyour_profile\\.ipythonprofile_default
使用以下代码在profile_defaults中创建一个ipython_config.py文件:
c = get_config()
c.InteractiveShell.ast_node_interactivity = "all"
解决方案 3:
按笔记本计算
正如其他人所回答的那样,将以下代码放入 Jupyter Lab 或 Jupyter Notebook 单元中即可起作用:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
永久的改变
但是,如果您想将其永久保留并使用 Jupyter Lab,则需要创建一个 IPython 笔记本配置文件。运行以下命令即可执行此操作(如果您使用 Jupyter Notebook,请不要运行 - 更多详细信息如下):
ipython profile create
如果您使用的是 Jupyter Notebook,则此文件应该已经创建,无需再次运行。事实上,运行此命令可能会覆盖您当前的首选项。
创建此文件后,对于 Jupyter Lab 和 Notebook 用户,请将以下代码添加到文件中C:UsersUSERNAME.ipythonprofile_defaultipython_config.py
:
c.InteractiveShell.ast_node_interactivity = "all"
我发现在新版本的 Jupyter 中不需要 c = get_config()
,但如果这对您不起作用,请将其添加c = get_config()
到文件的开头。
有关除 之外的更多标志选项"all"
,请访问此链接:
https: //ipython.readthedocs.io/en/stable/config/options/terminal.html#configtrait-InteractiveShell.ast_node_interactivity
解决方案 4:
我们可以在每个需要显示的语句前添加显示方法