Hello everyone,
I have a problem getting the same output in two places. I used the callback_contex to do that. but the second function I have defined in my code is not working.
Here is my case:
when I select a csv file from the 1st dropdown, it returns the column names as the options for the 2nd dropdown, and plots a 3d figure using the csv file.
Then, by choosing from 2nd dropdown, a rangeslider generates. I want to update the same 3d plot by moving the rangeslider.
Here is my code,
I appreciate in advance for any feedback and suggestions.
path = 'The address to the csv files'
# call back to store and share the df between callbacks
@callback(
Output('table1', 'data'),
Input('dropdown1', 'value'),
prevent_initial_call=True
)
def store_df(chosen_par):
if chosen_par is None:
return dash.no_update
else:
df = pd.read_csv(path + chosen_par + '.csv')
df_json = df.to_json(date_format='iso', orient='split')
return df_json
# it gets csv files from dropdown1 and returns the feature options in dropdown2
@callback(
Output('dropdown2', 'options'),
Input('table1', 'data'),
prevent_initial_call=True
)
def set_well_options(js_df):
if js_df is None:
return dash.no_update
else:
dff = pd.read_json(js_df, orient='split')
features = np.sort(dff.columns.values[2:])
return features
# it gets the value from dropdown2 and updates the rangeslider
@callback(
Output('my_rangeslider', 'value'),
Input('table1', 'data'),
Input('dropdown2', 'value'),
prevent_initial_call=True
)
def set_rangeslider_values(js_df, feature):
if (feature == []) or (feature is None):
return dash.no_update
else:
dff = pd.read_json(js_df, orient='split')
min_value = dff[feature].min()
max_value = dff[feature].max()
return [min_value, max_value]
@callback(
Output('graph1', 'figure'),
Input('dropdown1', 'value'),
Input('my_rangeslider', 'value'),
Input('table1', 'data'),
Input('my_rangeslider', 'value'),
Input('dropdown2', 'value'),
prevent_initial_call=True
)
def group1(b1, b2, js_df, ranges, feature):
triggered_id = ctx.triggered_id
if triggered_id == 'dropdown1':
return plot3d(js_df)
elif triggered_id == 'my_rangeslider':
return update_with_rangeslider(js_df, ranges, feature)
def plot3d(js_df):
dff = pd.read_json(js_df, orient='split')
fig = go.Figure()
fig.add_trace(go.Scatter3d(x=dff['X'], y=dff['Y'], z=dff['Z'], mode='markers', marker={'color': '#F3CAD2', 'size': 4}))
return fig
def update_with_rangeslider(js_df, ranges, feature):
if (feature is None) or (ranges == []):
return dash.no_update
else:
dff = pd.read_json(js_df, orient='split')
df_sliced_min = dff[(dff[feature] < ranges[0])]
df_sliced_max = dff[(dff[feature] > ranges[1])]
fig = go.Figure()
fig.add_trace(go.Scatter3d(x=df_sliced_min['X'], y=df_sliced_min['Y'], z=df_sliced_min['Z'], mode='markers', marker={'color': '#1F7BAA', 'size': 6}))
fig.add_trace(go.Scatter3d(x=df_sliced_max['X'], y=df_sliced_max['Y'], z=df_sliced_max['Z'], mode='markers', marker={'color': '#FF206E', 'size': 6}))
return fig