在我的 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()
但这不知何故只会在我与笔记本进行交互时更新时间。即使不进行任何单元格交互,如何更新笔记本?
最佳答案
关于python - Jupyter 实验室中的后台线程时钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57389808/