Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you suppress output in Jupyter running IPython?

People also ask

How do I interrupt in Jupyter?

To interrupt a calculation which is taking too long, use the Kernel, Interrupt menu option, or the i,i keyboard shortcut. Similarly, to restart the whole computational process, use the Kernel, Restart menu option or 0,0 shortcut.

How do I turn off IPython?

Entering (and exiting) ipython You can also hit Ctrl-D to exit out of ipython. In fact, using your keyboard is the most highly recommended way.


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


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.


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


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!