Hello, I am new to the forum and new to Dash. I love it a lot and plan to work mostly on it and join this community.
I recently learned to work with multipage apps, but sometimes I struggle with callbacks. I am using Dash to create engineering models and based on the selection, display different plots with different parameters.
A simple example is when I input a speed of 20 mph, and it plots the distance over time. Here is my callback. My app starts with the model set to value ‘A’, everything works perfectly. I am changing the value to ‘B’, and it works well again. But when I try to switch the shear option back to ‘A’, it gives me the following error:
A nonexistent object was used in an State
of a Dash callback. The id of this object is gas_velocity
and the property is value
. The string ids in the current layout are: [_pages_location, _pages_content, _pages_store, _pages_dummy, pH-slider-norsok, shear-dropdown-norsok, display-options, corrosion-plot-norsok, liq_density, liq_viscosity, liq_velocity, calculate-button]
this is how i defined my input states in main layout:
dbc.CardHeader("Shear option", style={"margin-top": "10px"}),
dcc.Dropdown(id='shear-dropdown-norsok', multi = False, clearable = False, disabled = False,
options=[{'label': 'Neglect shear (S=19)', 'value': 'A'},
{'label': "Norsok's original method", 'value': 'B'},
{'label': 'Single-phase simplification', 'value': 'C'}, ], value='A',
style={"margin-bottom": "20px", 'color': 'black',}),
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
html.Div(id='display-options'),
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # NORSOK
dbc.Input(id="liq_velocity", style={"display": "none"}),
dbc.Input(id="gas_velocity", style={"display": "none"}),
dbc.Input(id="liq_density", style={"display": "none"}),
dbc.Input(id="gas_spg", style={"display": "none"}),
dbc.Input(id="liq_viscosity", style={"display": "none"}),
dbc.Input(id="gas_viscosity", style={"display": "none"}),
dbc.Button(id="calculate-button", style={"display": "none"}),
first callback that updates dropdown and selections
@callback(
Output('display-options', 'children'),
[Input('shear-dropdown-norsok', 'value')]
)
and this is my second callback with plot display
@callback(Output('corrosion-plot-norsok', 'figure'),
[Input('calculate-button', 'n_clicks')],
[State('shear-dropdown-norsok', 'value'),
State('pH-slider-norsok', 'value'),
State('liq_velocity', 'value'),
State('gas_velocity', 'value'),
State('liq_density', 'value'),
State('gas_spg', 'value'),
State('liq_viscosity', 'value'),
State('gas_viscosity', 'value'),
])
def update_plot(n_clicks, shear_option, selected_pH, liq_vel, gas_vel, liq_dens, gas_spg, liq_myu, gas_myu):
if shear_option == 'B':
if n_clicks is None:
raise PreventUpdate
else:
fig = get_plot_neglect(shear_option, selected_pH)
if shear_option == 'norsok':
if n_clicks is None:
raise PreventUpdate
# n_clicks = None
else:
fig = get_plot_default(shear_option, selected_pH, liq_vel, gas_vel, liq_dens, gas_spg, liq_myu, gas_myu)
if shear_option == 'C':
if n_clicks is None:
raise PreventUpdate
else:
fig = get_plot_homogen(shear_option, selected_pH, liq_vel, liq_dens, liq_myu) # they will be set to be mixture values
return fig