Hi,
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):
try:
** 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?
Thanks