14

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?

4

3 回答 3

6

您可以使用以下命令抑制输出:

‘;’ at the end of a line 

也许在你的循环中创建一个条件来抑制超过某个阈值的输出。

于 2014-07-06T03:48:09.420 回答
5

对于其他偶然发现的人:

如果您想查看一些输出而不是完全抑制输出,则有一个名为limit-output的扩展

您必须按照第一个链接中的扩展安装说明进行操作。然后我运行以下代码来更新每个单元格输出的最大字符数:

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

注意:您需要运行代码块,然后完全重新启动您的笔记本服务器(不仅仅是内核)以使更改生效。

运行 Python 2.7.12 内核的 jupyter v4.0.6 上的结果

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

0
1
2
3
4

limit_output extension: Maximum message size exceeded
于 2016-09-19T01:58:20.367 回答
2

这也给我带来了一些问题。我写了一个类来为打印功能添加一些保护。我只是把它扔在 github 上:staggerprint on github

我会将“使用”部分放在笔记本的顶部,以防止发生此类崩溃。如果我发现如何从笔记本级别更改此行为,我将添加另一个答案,但现在必须这样做!

于 2014-07-07T20:48:24.390 回答