iPython notebook avoid printing within a function

≡放荡痞女 提交于 2019-12-23 20:31:05

问题


I want to prevent a function to print in iPython notebook.

In standard python one can prevent printing some lines of code as answered in the question: To prevent a function from printing in the batch console in Python However this method do not work in iPython notebook, losing the output until a restart of the Kernel.

The most similar feature I found is to avoid a full cell to display using the magic function:

%%capture capt

However this magic function blocks the whole cell, is there any way in iPython notebook to avoid printing just some of the lines within the code?


回答1:


You could use io.capture_output:

from IPython.utils import io

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

to capture stdout and stderr for only those lines within the with-statement.



来源:https://stackoverflow.com/questions/23610585/ipython-notebook-avoid-printing-within-a-function

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