How to selectively update Outputs in callback (i.e. update some, not all) - for an error message implementation


I’m relatively new to Dash and am trying to build a simple GUI with inputs, a button and an output plots (initiailly… will scale this to a bigger app later).

At present, I’m trying to implement an error catching script:. I have an alert component (from dash bootstrap components) which is usually set to is_open=False. When the user presses my button, the output is computed based on the inputs, but if an error occurs (using try/except structure), then I set the alert’s child with the error message and set is_open=True.

This could work ok, but results in a messy callback structure:

@app.callback([MultipleOutputsForMyPlotsEtc, Output(‘alert’,‘is_open’), Output(‘alert’, ‘children’],
[Input(‘btn’, 'n_clicks)], [State(InputsOnMyPage)])
def update(n, input1, input2):
** some processing
return outputs-for-successful-processed-data, False, “No Error to Be Displayed”
except Exception as msg:
return I-Don’t-Want-To-Update-The-Outputs-Here, True, f"Your error is {msg}"

I’d like to use the same error alert component for numerous callbacks. I guess I could solve this by use callback chains. However, the main problem is that when an error occurs (as shown above), I don’t want the callback to update the output plots (they should remain as they are), but DO want the callback to update the alert output. How can I selectively achieve this?

I believe one solution is to define States() for all the current output values, then just return these current states as the output. However, this seems very hacky and won’t work well since there’s other callbacks that fire when the output gets updated, thus it’s computationally wasteful to recompute everything when the output hasn’t actually changed.

Are there any other solutions please? e.g. How can I selectively update certain outputs depending on logic in the callback function?


There is a solution! though it looks like we haven’t documented it yet… dash.no_update. Use it like:

    return outputs-for-successful-processed-data, False, "No Error to Be Displayed"
except Exception as msg:
    return dash.no_update, True, f"Your error is {msg}"

Thanks, this looks great. I’ll try it out.

As a side comment, do you think this is a good way to implement an error display system? Or is there a better (or simpler?) way to pipe all exceptions into a nicely formatted component that shows in the browser?

Unless the built-in devtools system (app.run_server(debug=True)) meets your needs, this does seem to me like a reasonable way to do it. It’s a bit heavy, would be interesting to think about what kind of interface would make it simpler to build something like this… but given what exists today I think you’re on the right track.