@kristada619 wrote:
I am making a Dash app where I let the user choose a latitude, longitude, radius and location type to display a map. I want to add exception handling to the code - so that if, say, the user enters strings for latitude, then upon clicking the Submit button, it should return a text in the dcc.Graph area saying “Please enter a numeric value for latitude”, instead of the code breaking and throwing an error there. And likewise for the other inputs.
So I have the following lines in my code:
@app.callback( [Output('output_text', 'children'), Output('output_graph', 'figure')], [Input('submit_button', 'n_clicks')], state=[State('input_lat', 'value'), State('input_lon', 'value'), State('input_radius', 'value'), State('input_type', 'value'), State('input_key', 'value')]) def update_output(n_clicks, lat_, lon_, radius, loc_type, keyword): if n_clicks: if not isinstance(lat_, float): return "Please enter a numeric value for Latitude." if not isinstance(lon_, float): return "Please enter a numeric value for Longitude." if not isinstance(radius, float): return "Please enter a numeric value for radius." if not isinstance(loc_type, str): return "Please enter a string value for Location Type." ## rest of the code
But it does not have the desired output. For example, I entered the string “blah” in the input box for longitude, instead of a floating numeric value, and it still throws error instead of returning the text I intended for it to display in case of this error:
What is the way to do exception handling in Plotly Dash?
Posts: 2
Participants: 2