python - Jupyter 实验室中的后台线程时钟

标签 python jupyter-notebook jupyter-lab

在我的 Jupyter 笔记本中,我想执行一些长时间运行的后台作业并在单元格中显示它们的状态。我想,在做一些困难的事情之前,我可以先从一个时钟开始。

我在一个单元格中有以下代码

import ipywidgets as widgets
import time
from datetime import datetime as dt
from IPython.display import HTML
from IPython.display import display
import threading

out = widgets.Output()

@out.capture(clear_output=True, wait=True)
def display_time():
    now = dt.now().strftime("%H:%M:%S")
    display(HTML(f"""<h1><center>{now}</center></h1>"""))

def run_time():
    while True:
        time.sleep(1)
        display_time()

out

当我调用 run_time() 时,我每秒更新一次时间,但我的笔记本阻塞在这个单元格上。所以我尝试了 threading.Thread(target=run_time).start() 但这不知何故只会在我与笔记本进行交互时更新时间。即使不进行任何单元格交互,如何更新笔记本?

最佳答案

您想查看 ipyrallel 以在您的笔记本中运行多内核。

这是一个solved issue

这是 documentation

关于python - Jupyter 实验室中的后台线程时钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57389808/

相关文章:

具有列表理解的python三元迭代

python - Ipython raw_input 变通了吗?

html - 在 jupyterlab/jupyternotebooks 中运行 highcharter

python - Jupyter Notebook 和 JupyterLab 有什么区别?

python - 使用三次样条在 Pandas 中插值时间序列

python - 如何使用 Python 请求库发出 post 请求?

python - 错误 : An exception has occurred, 使用 %tb 在 Python Jupyter Notebook 中查看 Dash App 的完整回溯

python - 如何在 python 中获取当前的 jupyter notebook 服务器?

jupyter-lab - 更改 jupyterlab 中输出单元的字体系列

python - 如何从 Tensorflow 检查点将权重加载到 Keras 模型