How do you suppress output in IPython Notebook?

雨燕双飞 提交于 2019-12-17 06:31:13

问题


How can output to stdout be suppressed?

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.

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

>>> MyFunction()
Calculating values...

How can the output from print / MyFunction be suppressed?


回答1:


Add %%capture as the first line of the cell. 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




回答2:


Suppress output

Put a ; at the end of a line to suppress the printing of output [Reference].




回答3:


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

You could use io.capture_output:

from IPython.utils import io

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

to supress (e.g. capture) stdout and stderr for those lines within the with-statement.




回答4:


If anyone is interested in clearing all outputs:

  1. Go to Cell
  2. Go to All Output

Then choose whichever option you like.



来源:https://stackoverflow.com/questions/23692950/how-do-you-suppress-output-in-ipython-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!