138

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?

krassowski
  • 6,277
  • 1
  • 30
  • 55
Zero
  • 9,142
  • 7
  • 45
  • 65

4 Answers4

204

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

Zero
  • 9,142
  • 7
  • 45
  • 65
  • 2
    Anyone know what the opposite of %%capture is? As in, how do you stop %%capture within a cell, or is it enabled until the end of the cell? – David Parks Sep 16 '16 at 17:18
  • 2
    Good thing to know is `%%capture` is only enabled until the end of the cell, and it must appear before any code in the cell. (So it appears there isn't a way to uncapture within a cell.) – Arel Oct 17 '16 at 19:32
  • ICYMI, @David Parks and @Arel's discussion is covered by @gwd2's answer on this page about using `with io.capture_output() as captured:` you can fine-tune within a cell to only capture what is run in that `with` context. – Wayne Feb 19 '20 at 17:23
  • Note this only works for Python kernels (e.g. not R). – Max Ghenis Feb 20 '20 at 05:47
107

Suppress output

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

Farhad Maleki
  • 2,764
  • 1
  • 19
  • 20
  • 6
    This doesn't for a code inside a `for` loop. Any ideas? I only want to suppress output from particular lines of code in the cell, not all lines. Thanks – Confounded Nov 15 '19 at 11:15
  • this method doesn't work for `print()` at jupyter notebook. however, codes like `1+1;` works. – song.xiao Oct 03 '20 at 14:59
  • 1
    Also doesn't work for some 3rd party components; it seems to be supressing only native python stdout. – Kirk Broadhurst Oct 05 '20 at 00:16
21

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

gdw2
  • 6,362
  • 4
  • 40
  • 46
  • Thanks a lot, this was driving me crazy -- I have a startup function that I run every time I open a notebook that calls `%autosave`, and wanted to suppress its output. Thought it would be simple -- but weirdly, `contextlib.redirect_stdout` and `sys.stdout = open(os.devnull, 'w')` both fail (end up printing an extra blank line). This should be the accepted answer. – Luke Davis Nov 04 '18 at 18:20
  • 1
    This was my favourite answer, because one can suppress *some* of the output in a cell without suppressing all of it. – Wolpertinger Sep 10 '19 at 14:01
  • Thanks, this works great and is easy, – Chris Norris Jan 07 '21 at 00:45
-6

If anyone is interested in clearing all outputs:

  1. Go to Cell
  2. Go to All Output

Then choose whichever option you like.

Yuqin Peng
  • 43
  • 2