import time
from uuid import uuid4
import dash
from dash import html
from dash.long_callback import DiskcacheLongCallbackManager
from dash.dependencies import Input, Output
## Diskcache
import diskcache
launch_uid = uuid4()
cache = diskcache.Cache("./cache")
long_callback_manager = DiskcacheLongCallbackManager(
cache, cache_by=[lambda: launch_uid], expire=60,
)
app = dash.Dash(__name__, long_callback_manager=long_callback_manager)
app.layout = html.Div(
[
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!"),
]
)
@app.long_callback(
output=(Output("paragraph_id", "children"), Output("button_id", "n_clicks")),
inputs=Input("button_id", "n_clicks"),
running=[
(Output("button_id", "disabled"), True, False),
(Output("cancel_button_id", "disabled"), False, True),
],
cancel=[Input("cancel_button_id", "n_clicks")],
)
def callback(n_clicks):
time.sleep(2.0)
return [f"Clicked {n_clicks} times"], (n_clicks or 0) % 4
if __name__ == "__main__":
app.run_server(debug=True)
code is from the doc of long callbacks
Here you can see that it takes a few seconds to run the callback function, but the cached results are used after n_clicks
cycles back around to 0.
The input is n_clicks which always changes, but the result returned from the cache.
In general, if the input changes constantly, it will always be cached, but this does not.
Why