有没有办法去" tail -f" Jupyter中单元格的输出

时间:2017-01-26 20:25:21

标签: jupyter-notebook jupyter

如果我有一个简单的脚本,如:

for i in range(100): sleep(1) print(i)

有没有办法只显示输出的最后5行,类似于" tail -f"命令?

1 个答案:

答案 0 :(得分:1)

我找到了一个适合我的解决方案,我也意识到我需要这个过程在后台工作。

我首先必须启用小部件: jupyter nbextension enable --py --sys-prefix widgetsnbextension

from IPython.display import display
from ipywidgets import Label
from time import sleep

import threading

class App(object):
    def __init__(self, nloops=2000):
        self.nloops = nloops
        self.pb = Label(description='Thread loops', value="0")

    def start(self):
        display(self.pb)
        for i in range(10):
            self.pb.value += str(i) 
            sleep(1)

app = App(nloops=20000)

t = threading.Thread(target=app.start)

t.start()

from IPython.display import display from ipywidgets import Label from time import sleep import threading class App(object): def __init__(self, nloops=2000): self.nloops = nloops self.pb = Label(description='Thread loops', value="0") def start(self): display(self.pb) for i in range(10): self.pb.value += str(i) sleep(1) app = App(nloops=20000) t = threading.Thread(target=app.start) t.start()