简体   繁体   English

在已发布的 Jupyter Notebook 中显示函数正在“运行”的小部件

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

In a published Jupyter notebook is there a way to insert a widget that says "running" or something similar when I am running a function.在已发布的 Jupyter 笔记本中,有没有办法在我运行函数时插入一个显示“正在运行”或类似内容的小部件。

I am aware of the tqdm function but to the best of my knowledge this is only when the function / process contains a for-loop.我知道 tqdm 函数,但据我所知,这只是在函数/进程包含 for 循环时。

I currently have a series of dropdown widgets with a submit button but some of the functions take a while for the calcs to run so i have no way of telling if the're running or not我目前有一系列带有提交按钮的下拉小部件,但有些功能需要一段时间才能运行计算,所以我无法判断它们是否正在运行

Cheers干杯

The way I have done this in the past is to have a function as a context manager, that displays some value to a Text widget to indicate that the function is running.我过去这样做的方法是使用一个函数作为上下文管理器,它向文本小部件显示一些值以指示该函数正在运行。 You could also use an Output widget to display a indeterminate 'progress' bar like the one below:您还可以使用输出小部件来显示不确定的“进度”栏,如下所示:

https://www.iselect.com.au/content/themes/iselect/images/post/loader.gif 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM