Here, I got one that isnt working obviously due to the error:
import time
import os
import dash
from dash import Dash, DiskcacheManager, CeleryManager, Input, Output, html, callback, dcc
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__, background_callback_manager=background_callback_manager, use_pages=True, pages_folder='')
app.layout = html.Div(
[
dcc.Link('page1', '/'), dcc.Link('page2', '/2'),
dash.page_container
]
)
layout1 = [html.Div([html.P(id="paragraph_id", children=["Button not clicked"])]),
html.Button(id="button_id", children="Run Job!"),
html.Button(id="cancel_button_id", children="Cancel Running Job!")]
layout2 = [html.Div([html.P(id="paragraph_id2", children=["Button not clicked"])]),
html.Button(id="button_id2", children="Run Job!"),
html.Button(id="cancel_button_id2", children="Cancel Running Job!")]
dash.register_page('page1', path='/', layout=layout1)
dash.register_page('page1', path='/2', layout=layout2)
@callback(
output=Output("paragraph_id", "children"),
inputs=Input("button_id", "n_clicks"),
background=True,
running=[
(Output("button_id", "disabled"), True, False),
(Output("cancel_button_id", "disabled"), False, True),
],
cancel=[Input("cancel_button_id", "n_clicks"), Input('_pages_location', 'href')],
)
@callback(
output=Output("paragraph_id2", "children"),
inputs=Input("button_id2", "n_clicks"),
background=True,
running=[
(Output("button_id2", "disabled"), True, False),
(Output("cancel_button_id2", "disabled"), False, True),
],
cancel=[Input("cancel_button_id2", "n_clicks"), Input('_pages_location', 'href')],
)
def update_clicks(n_clicks):
time.sleep(4.0)
return [f"Clicked {n_clicks} times"]
if __name__ == "__main__":
app.run(debug=True)