General questions on performance:
Which is a better practice for increasing app performance (both on initial load and during use):
- many callbacks that update a single output, or
- single callback that updates many outputs? (does using dash.callback_context affect overall callback performance?)
See example below for toggling a component that opens/closes
## MULTIPLE "SINGLE" CALLBACKS
@app.callback(Output("dmc-modal-1", "opened"), Input("dmc-modal-btn-1", 'n_clicks'), State("dmc-modal-1", "opened"))
def _toggle_modal_1(n, is_open):
return not is_open if n else is_open
@app.callback(Output("dmc-modal-2", "opened"), Input("dmc-modal-btn-2", 'n_clicks'), State("dmc-modal-2", "opened"))
def _toggle_modal_2(n, is_open):
return not is_open if n else is_open
@app.callback(Output("dmc-modal-3", "opened"), Input("dmc-modal-btn-3", 'n_clicks'), State("dmc-modal-3", "opened"))
def _toggle_modal_3(n, is_open):
return not is_open if n else is_open
## === VS. === ##
## SINGLE "MULTIPLE" CALLBACK
@app.callback(
Output("dmc-modal-1", "opened"),
Output("dmc-modal-2", "opened"),
Output("dmc-modal-3", "opened"),
Input("dmc-modal-btn-1", "n_clicks"),
Input("dmc-modal-btn-2", "n_clicks"),
Input("dmc-modal-btn-3", "n_clicks"),
State("dmc-modal-1", "opened"),
State("dmc-modal-2", "opened"),
State("dmc-modal-3", "opened"),
)
def _toggle_modals(n1, n2, n3, is_open_1, is_open_2, is_open_3):
ctx = dash.callback_context
triggered_id = ctx.triggered[0]["prop_id"]
if triggered_id == "dmc-modal-btn-1.n_clicks":
is_open_1 = not is_open_1
elif triggered_id == "dmc-modal-btn-1.n_clicks":
is_open_2 = not is_open_2
elif triggered_id == "dmc-modal-btn-1.n_clicks":
is_open_3 = not is_open_3
return is_open_1, is_open_2, is_open_3