Hi,
I have a rather complex app where the layout content is generated by a callback (depending on what should be displayed). For one specific layout content, I need a long callback because one callback function takes some time to execute. The problem is that, somehow, right after the layout is generated, the long callback is triggered automatically (even when using prevent_initial_call). No problem if I use a simple callback (not long), though.
Below is a sample script to reproduce this issue (if it is intended) based on the long callback example in the documentation:
import dash
from dash import html
from dash.long_callback import DiskcacheLongCallbackManager
from dash.dependencies import Input, Output
import diskcache
cache = diskcache.Cache("./cache")
long_callback_manager = DiskcacheLongCallbackManager(cache)
app = dash.Dash(
__name__,
suppress_callback_exceptions=True,
long_callback_manager=long_callback_manager,
)
layout1 = html.Div(
[
html.Button("Generate layout", id="button_1"),
html.Div(id="main"),
],
)
layout2 = html.Div(
[
html.Div([html.P(id="paragraph_id", children=["Button not clicked"])]),
html.Button(id="button_id", children="Run Job!"),
]
)
app.layout = layout1
app.validation_layout = html.Div([layout1, layout2])
@app.callback(
Output("main", "children"),
Input("button_1", "n_clicks"),
prevent_initial_call=True,
)
def generate_layout(n_clicks):
return layout2
@app.long_callback(
Output("paragraph_id", "children"),
Input("button_id", "n_clicks"),
prevent_initial_call=True,
)
def callback(n_clicks):
return [f"Clicked {n_clicks} times"]
if __name__ == "__main__":
app.run_server(debug=True)
Execute the app, click on “Generate layout”, wait a few seconds, “paragraph_id” children will automatically change to “Clicked None times”.
Otherwise, if it’s actually intended, why is it happening and how to prevent it?
Edit: following the documentation, I also tried with a validation layout to reference all the callbacks, but same issue (code updated).