在已发布的 Jupyter Notebook 中显示函数 'running' 的小部件

Widget to show that function is 'running' in published Jupyter Notebook

在已发布的 Jupyter 笔记本中,有没有一种方法可以在我 运行ning 一个函数时插入一个显示“运行ning”或类似内容的小部件。

我知道 tqdm 函数,但据我所知,只有当函数/进程包含 for 循环时才会出现这种情况。

我目前有一系列带有提交按钮的下拉小部件,但有些功能需要一段时间才能计算到 运行,所以我无法判断它们是否 运行宁与否

干杯

我过去这样做的方法是让一个函数作为上下文管理器,它向文本小部件显示一些值以指示该函数是 运行。您还可以使用输出小部件来显示不确定的 'progress' 栏,如下所示:

https://www.iselect.com.au/content/themes/iselect/images/post/loader.gif

import ipywidgets as ipyw
import time
from contextlib import contextmanager

label = ipyw.Text('Ready')
button = ipyw.Button(description='Click me')

@contextmanager
def show_loading():
    label.value = 'Running...'
    yield
    label.value = 'Ready'

def long_running_function(self):
    with show_loading():
        time.sleep(2)
        
button.on_click(long_running_function)

display(button)    
display(label)