【问题标题】:Dash: progress bar for reading files破折号:读取文件的进度条
【发布时间】:2019-12-09 02:10:46
【问题描述】:

我已经看到了 ProgressInterval 组件在 Dash 中显示进度的示例:

import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

progress = html.Div(
    [
        dcc.Interval(id="progress-interval", n_intervals=0, interval=500),
        dbc.Progress(id="progress"),
    ]
)

@app.callback(
    [Output("progress", "value"), Output("progress", "children")],
    [Input("progress-interval", "n_intervals")],
)
def update_progress(n):
    # check progress of some background process, in this example we'll just
    # use n_intervals constrained to be in 0-100
    progress = min(n % 110, 100)
    # only add text after 5% progress to ensure text isn't squashed too much
    return progress, f"{progress} %" if progress >= 5 else ""

但是,我找不到任何实际显示如何将进度条正确链接到某个后台进程的示例。

例如,如果我有一个带有常规 for 循环的回调,我如何显示这个 for 循环的进度(即,如果我在 100 的第 10 个元素上,我希望它显示进度条中的 10%)?

【问题讨论】:

    标签: python plotly-dash


    【解决方案1】:

    @Valeria,我创建了一个关于进度条和微调器的教程,重点介绍了加载器的格式。 https://youtu.be/t1bKNj021do

    但是对于进度条与后台进程的链接,这个人很好地创建了一个示例应用程序和代码。 https://github.com/tcbegley/dash-rq-demo

    【讨论】:

    • 此“答案”只是指向其他内容的链接(通过您的视频视频获利??)。我投票支持另一个答案。
    【解决方案2】:

    我能够使用 for 循环来更新进度条。

    但我不得不:

    1. 在仪表板应查找新值时使用dcc.Interval
    2. 使用线程使工作人员独立工作,而不是阻塞仪表板。
    3. 使用队列在工作线程之间进行线程安全通信并更新进度条。

    使用全局 progress_memoryQueue,这可能可以使用类很好地分组。

    import dash
    import time
    import threading
    import dash_bootstrap_components as dbc
    from queue import Queue
    
    
    progress_queue = Queue(1)
    progress_memeory = 0
    
    
    app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
    
    
    app.layout = dash.html.Div(children=[
        dash.html.H1(children='Test Progress Bar Using Loop'),
        dash.dcc.Interval(id='clock', interval=1000, n_intervals=0, max_intervals=-1),
        dbc.Progress(value=0, id="progress_bar"),
        dash.html.Button("Start Work", id='start_work', n_clicks=0)
    ])
    
    
    @app.callback(
        [dash.Output("progress_bar", "value")],
        [dash.Input("clock", "n_intervals")])
    def progress_bar_update(n):
        global progress_memeory
        if not progress_queue.empty():
            progress_bar_val = progress_queue.get()
            progress_memeory = progress_bar_val
        else:
            progress_bar_val = progress_memeory
        return(progress_bar_val,)
       
    
    @app.callback([
        dash.Output("start_work", "n_clicks")],
        [dash.Input("start_work", "n_clicks")])
    def start_bar(n):
        if n==0:
            return(0,)
        threading.Thread(target=start_work, args=(progress_queue,)).start()
        return(0,)
    
    
    def start_work(output_queue):
        for i in range(101):
            time.sleep(0.5)
            if output_queue.empty():
                output_queue.put(i)
        return(None)
    
    if __name__ == '__main__':
        app.run_server(debug=True, use_reloader=False)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      相关资源
      最近更新 更多