-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplotlyDash_progressbar_in_modal.py
104 lines (92 loc) · 2.9 KB
/
plotlyDash_progressbar_in_modal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import json
import time
import os
from dash import Dash, DiskcacheManager, CeleryManager, Input, Output, html, callback
import dash_bootstrap_components as dbc
if 'REDIS_URL' in os.environ:
# Use Redis & Celery if REDIS_URL set as an env variable
from celery import Celery
celery_app = Celery(__name__, broker=os.environ['REDIS_URL'], backend=os.environ['REDIS_URL'])
background_callback_manager = CeleryManager(celery_app)
else:
# Diskcache for non-production apps when developing locally
import diskcache
cache = diskcache.Cache("./cache")
background_callback_manager = DiskcacheManager(cache)
app = Dash(
__name__,
external_stylesheets=[dbc.themes.BOOTSTRAP],
background_callback_manager=background_callback_manager
)
# define the modal. In this case it only shows the progress bar and a cancel button
modal = dbc.Modal(
[
dbc.ModalHeader(
dbc.ModalTitle("Your progress bar"),
close_button=False
# ^^ important, otherwise the user can close the modal
# but the callback will be running still
),
dbc.ModalBody(
html.Progress(
id="progress_bar",
value="0",
style={'width': '100%'}
)
),
dbc.ModalFooter(
dbc.Button(
"Cancel",
id="cancel_button_id",
className="ms-auto",
n_clicks=0
)
)
],
id="modal",
is_open=False,
backdrop="static",
keyboard=False
# ^^ important, otherwise the user can close the modal via the ESC button
# but the callback will be running still
)
app.layout = html.Div(
[
html.Div(
html.P(
id="paragraph_id",
children=["Button not clicked"]
)
),
html.Button(
id="button_id",
children="Run Job!"
),
modal
]
)
@callback(
output=Output("paragraph_id", "children"),
inputs=Input("button_id", "n_clicks"),
background=True,
running=[
(Output("button_id", "disabled"), True, False),
(Output('modal', 'is_open'), True, False)
],
progress=[
Output("progress_bar", "value"),
Output("progress_bar", "max")
],
cancel=Input("cancel_button_id", "n_clicks"),
prevent_initial_call=True
)
def update_progress(set_progress, _):
total = 500
just_a_list = []
for i in range(total + 1):
just_a_list.append(i)
set_progress((str(i), str(total)))
time.sleep(0.01)
return json.dumps(just_a_list)
if __name__ == "__main__":
app.run(debug=True)