【问题标题】:How to display full output in Jupyter, not only last result?如何在 Jupyter 中显示完整输出,而不仅仅是最后一个结果?
【发布时间】:2016-08-15 15:58:10
【问题描述】:

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

例子:

a=3
a
a+1

我想显示

3
4

【问题讨论】:

  • 它不经常使用,但实际上有一个配置选项应该这样做 - 在IPython kernel config file 中将InteractiveShell.ast_node_interactivity 设置为'all'
  • 谢谢你,Thomas,这就是我要找的 :)
  • 它存在! ! !

标签: python ipython jupyter


【解决方案1】:

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

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

【讨论】:

  • 这是一个很棒的提示。 - 仅恢复到最后一行输出的标志值是多少?
  • 默认值为:'last_expr'。您可以在这里找到很多其他选项:ipython.readthedocs.org/en/stable/config/options/terminal.html
  • 作为参考,选项有“all”、“none”、“last”和“last_expr”。 'last' 和 'last_expr' 之间的区别:如果您的单元格以包含表达式的循环结尾,则 'last' 将在循环的每次迭代中向您显示该表达式的结果。 'last_expr'(默认值)不会显示:它只会在单元格末尾显示裸表达式的结果。
  • 天哪...这个功能简直是杀手锏。
  • 新的(ish)last_expr_or_assign 非常适合做演示!不再需要多次重新输入同一个项目来打印它。
【解决方案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:\Users\your_profile\\.ipython\profile_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:\Users\USERNAME\.ipython\profile_default\ipython_config.py

    c.InteractiveShell.ast_node_interactivity = "all"
    

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

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

    【讨论】:

    • 有没有办法在每个单元格中做到这一点?假设我只想在几个单元格上查看 Pandas.Series 的完整结果,有可能吗?
    • @RicardoSanchez 也许只使用display() 函数?首先导入它from IPython.display import display 然后只做display(df)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多