django-plotly-dash icon indicating copy to clipboard operation
django-plotly-dash copied to clipboard

Prevent Callback Execution Upon Initial Component Render

Open johanvergeer opened this issue 4 years ago • 2 comments

The Dash callback decorator has a parameter called prevent_initial_call to prevent callbacks from firing when their inputs initially appear in the layout of your Dash application.

I haven't seen this in the Django Plotly Dash callback or expanded_callback decorators.

So at this moment the only thing we can do is something like this:

@app.expanded_callback(
    Output("message-container", "style"), [Input("confirm", "submit_n_clicks")]
)
def show_message_container(id_value: str, **kwargs: Dict[str, Any]) -> Dict[str, str]:
    if id_value is None:
        raise PreventUpdate

    return {"display": "block"}

or

@app.expanded_callback(
    Output("message-container", "style"), [Input("confirm", "submit_n_clicks")]
)
def show_message_container(id_value: str, **kwargs: Dict[str, Any]) -> Dict[str, str]:
    if kwargs["callback_context"].triggered is []:
        raise PreventUpdate

    return {"display": "block"}

It would be nice if the prevent_initial_call parameter can be added to the callback and advanced_callback decorators.

johanvergeer avatar Feb 01 '21 12:02 johanvergeer

Thank you @johanvergeer !, I was looking for the solution and here I found :) Hope the option be added soon.

Elon-Chan avatar Feb 09 '21 15:02 Elon-Chan

I know neither Dash nor django_plotly_dash, but is dash.callback_context usable for this? I added it after wrestling with the initial callback execution challenge and looking through the Dash docs.

I think it works, but as mentioned: lack of knowledge and a lot of flailing means it could've been something else. Throwing it here in case someone with more knowledge can chime in on the issue.

@app.callback(...., [...])
def clean_inventory(...):
    ctx = dash.callback_context

    if not ctx.triggered:
        raise dash.exceptions.PreventUpdate

brianwisti avatar Feb 11 '21 17:02 brianwisti