【问题标题】:Is there a way to "tail -f" output of a cell in Jupyter有没有办法在 Jupyter 中“tail -f”输出单元格
【发布时间】:2017-01-28 05:59:20
【问题描述】:

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

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

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

【问题讨论】:

    标签: jupyter-notebook jupyter


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 1970-01-01
      • 2016-10-20
      • 2018-08-13
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      相关资源
      最近更新 更多