6

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?

Community
  • 1
  • 1
David Mabodo
  • 745
  • 5
  • 16
  • 1
    Perhaps you can wrap the code in a [`with io.capture_output():` statement](http://stackoverflow.com/a/14573397/190597). – unutbu May 12 '14 at 13:51
  • However I think this is not duplicated. Despite the solution is the same, the question is clearly diferent, and they solve diferents problems. – David Mabodo May 12 '14 at 14:10

1 Answers1

10

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.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677