usual me
usual me

Reputation: 8788

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?

Upvotes: 18

Views: 22985

Answers (2)

Kevin
Kevin

Reputation: 8227

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

Upvotes: 6

Union find
Union find

Reputation: 8160

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.

Upvotes: 8

Related Questions