Zero
Zero

Reputation: 12099

How do you suppress output in Jupyter running IPython?

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?

Upvotes: 209

Views: 208469

Answers (5)

Kamlesh Kumar
Kamlesh Kumar

Reputation: 293

pip install ipython-extensions

%%capture

print("hello")
#This cell will not return any output

%%Capture is a magic command to suppress the output of the cell in Jupiter notebook. But to run that you will have to install ipython-extensions first.

Upvotes: 3

Farhad Maleki
Farhad Maleki

Reputation: 3627

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.

Upvotes: 147

Yuqin Peng
Yuqin Peng

Reputation: 53

If anyone is interested in clearing all outputs:

  1. Go to Cell
  2. Go to All Output

Then choose whichever option you like.

Upvotes: -9

gdw2
gdw2

Reputation: 8016

(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.

Upvotes: 57

Zero
Zero

Reputation: 12099

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

Upvotes: 313

Related Questions