简体   繁体   English

你如何在运行 IPython 的 Jupyter 中抑制 output?

[英]How do you suppress output in Jupyter running IPython?

How can output to stdout be suppressed?如何抑制到stdout的 output?

A semi-colon can be used to supress display of returned objects, for example分号可用于禁止显示返回的对象,例如

>>> 1+1
2

>>> 1+1;   # No output!

However, a function that prints to stdout is not affected by the semi-colon.但是,打印到标准输出的 function 不受分号的影响。

>>> print('Hello!')
Hello!

>>> MyFunction()
Calculating values...

How can the output from print / MyFunction be suppressed?如何抑制来自print / MyFunction的 output?

Add %%capture as the first line of the cell.添加%%capture作为单元格的第一行。 eg例如

%%capture
print('Hello')
MyFunction()

This simply discards the output, but the %%capture magic can be used to save the output to a variable - consult the docs这只是丢弃输出,但%%capture魔法可用于将输出保存到变量 - 请参阅文档

Suppress output抑制输出

Put a ;放一个; at the end of a line to suppress the printing of output [ Reference ].在一行的末尾抑制输出的打印 [参考]。

A good practice is to always return values from functions rather than printing values inside a function.一个好的做法是始终从函数返回值,而不是在函数内部打印值。 In that case, you have the control;在这种情况下,您拥有控制权; if you want to print the returned value, you can;如果你想打印返回值,你可以; otherwise, it will not be printed just by adding a ;否则,不会仅通过添加 ; 来打印它。 after the function call.在函数调用之后。

(credit: https://stackoverflow.com/a/23611571/389812 ) (信用: https : //stackoverflow.com/a/23611571/389812

You could use io.capture_output :您可以使用io.capture_output

from IPython.utils import io

with io.capture_output() as captured:
    MyFunction()

to supress (eg capture) stdout and stderr for those lines within the with-statement .抑制(例如捕获) with-statement那些行的 stdout 和 stderr 。

If anyone is interested in clearing all outputs:如果有人有兴趣清除所有输出:

  1. Go to Cell转到单元格
  2. Go to All Output转到所有输出

Then choose whichever option you like.然后选择您喜欢的任何选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM