Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit the number of output lines in a given cell of the Ipython notebook?

Sometimes my Ipython notebooks crash because I left a print statement in a big loop or in a recursive function. The kernel shows busy and the stop button is usually unresponsive. Eventually Chrome asks me if I want to kill the page or wait.

Is there a way to limit the number of output lines in a given cell? Or any other way to avoid this problem?

like image 595
usual me Avatar asked Jul 05 '14 08:07

usual me


People also ask

How do you suppress the output of a cell in Jupyter Notebook?

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

How do you number lines in a Jupyter Notebook?

You can enable row numbers in all code cells by pressing Shift + L while no cell is active. If you cannot remember these hotkeys, open the command palette by pressing Control + Shift + P and search for 'line numbers'.

What is %% capture in Python?

IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. from __future__ import print_function import sys. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.


2 Answers

You can suppress output using this command:

‘;’ at the end of a line 

Perhaps create a condition in your loop to suppress output past a certain threshold.

like image 71
Union find Avatar answered Oct 22 '22 20:10

Union find


For anyone else stumbling across:

If you want to see some of the output rather than suppress the output entirely, there is an extension called limit-output.

You'll have to follow the installation instructions for the extensions at the first link. Then I ran the following code to update the maximum number of characters output by each cell:

from notebook.services.config import ConfigManager
cm = ConfigManager().update('notebook', {'limit_output': 10})

Note: you'll need to run the block of code, then restart your notebook server entirely (not just the kernel) for the change to take effect.

Results on jupyter v4.0.6 running a Python 2.7.12 kernel

for i in range(0,100):
    print i

0
1
2
3
4

limit_output extension: Maximum message size exceeded
like image 38
Kevin Avatar answered Oct 22 '22 20:10

Kevin