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

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

范例:

a=3
a
a+1

我想展示

3
4

I want Jupyter to print all the interactive output without resorting to print, not only the last result. How to do it?

Example :

a=3
a
a+1

I would like to display

3
4


回答 0

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

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

Thanks to Thomas, here is the solution I was looking for:

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

回答 1

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"

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

1) Place this code in a Jupyter cell:

from IPython.core.interactiveshell import InteractiveShell

InteractiveShell.ast_node_interactivity = "all"

2) In Windows, the steps below makes the change permanent. Should work for other operating systems. You might have to change the path.

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

Make a ipython_config.py file in the profile_defaults with the following code:

c = get_config()

c.InteractiveShell.ast_node_interactivity = "all"

回答 2

每个笔记本的基础

正如其他人回答的那样,将以下代码放入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

Per Notebook Basis

As others have answered, putting the following code in a Jupyter Lab or Jupyter Notebook cell will work:

from IPython.core.interactiveshell import InteractiveShell

InteractiveShell.ast_node_interactivity = "all"

Permanent Change

However, if you would like to make this permanent and use Jupyter Lab, you will need to create an IPython notebook config file. Run the following command to do so (DO NOT run if you use Jupyter Notebook – more details below):

ipython profile create

If you are using Jupyter Notebook, this file should have already been created and there will be no need to run it again. In fact, running this command may overwrite your current preferences.

Once you have this file created, for Jupyter Lab and Notebook users alike, add the following code to the file C:\Users\USERNAME\\.ipython\profile_default\ipython_config.py:

c.InteractiveShell.ast_node_interactivity = "all"

I found there is no need for c = get_config() in the newer versions of Jupyter, but if this doesn’t work for you, add the c = get_config() to the beginning of the file.

For more flag options other than "all", visit this link: https://ipython.readthedocs.io/en/stable/config/options/terminal.html#configtrait-InteractiveShell.ast_node_interactivity


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。