Trying to create chained callback

Hi,

I have 3 dropdowns in my code - State, BU and Injury. I have successfully created chained callback with BU and State. But when I create a chained callback from BU and State to affect the output of Injury I get following error -
Invalid argument options passed into Dropdown with ID “cause-filter”.
Expected one of type [object].
Value provided:
[
{
“label”: “Slip / Trip (pulse)”,
“value”: “Slip / Trip (pulse)”
},

Here is my callback code:


@app.callback(
        Output('cause-filter', 'options'),
        Input('state-filter', 'value'),
        Input('BU-filter', 'value'),
        suppress_callback_exceptions=True
    )
    def set_state_options(selected_state, selected_bu):
        if type(selected_state) != str or type(selected_bu) != str:
            df_filtered = df.query(
                'State == selected_state and BU == selected_bu')
        else:
            df_filtered = df.loc[(df['State'] == selected_state) & (
                df['BU'] == selected_bu)]
            return [{'label': i, 'value': i} for i in df['Incident_Cause'].unique()]

Where am i going wrong ? @adamschroeder

:wave: Welcome to the community, @chopchop30

Can you please share your full code as well as sample data set so we can try to run this locally.

Thanks for the Reply @adamschroeder . Below is the code for where the dropdown appears. Also adding the callback again.

dcc.Dropdown(
options=[{‘label’: i, ‘value’: i}
for i in df_aggregated[‘Incident_Cause’].unique()],
value=‘Slip / Trip (pulse)’,
id=‘cause-filter’,
placeholder=“Incident Cause”,
multi=True

Chained Callback that is not working -

@app.callback(
    Output('cause-filter', 'options'),
    Input('state-filter', 'value'),
    Input('BU-filter', 'value'),
    suppress_callback_exceptions=True
)
def set_state_options(selected_state,selected_bu):
    if type(selected_state) != str or type(selected_bu) != str:
        df_filtered = df.query(
            'State == @selected_state and BU == @selected_bu')
    else:
        df_filtered = df.loc[(df['State'] == selected_state)& (
            df['BU'] == selected_bu)]
        return [{'label': i, 'value': i} for i in df['Incident_Cause'].unique()]

Error I am getting from above callback

Invalid argument options passed into Dropdown with ID “cause-filter”.
Expected one of type [object].
Value provided:
[
{
“label”: “Slip / Trip (pulse)”,
“value”: “Slip / Trip (pulse)”
},

The chained Callback that is working (BU to State Filter)

@app.callback(
    Output('state-filter', 'options'),
    Input('BU-filter', 'value'),
    suppress_callback_exceptions=True
)
def set_state_options(selected_bu):
    if type(selected_bu) != str:
        df_filtered = df.query('BU == @selected_bu')
    else:
        df_filtered = df.loc[df['BU'] == selected_bu]
    return [{'label': i, 'value': i} for i in df_filtered['State'].unique()]

Hope this is what you were after ?

Unfortunately, I can’t share the data :frowning_face:

hi @chopchop30
It is still not clear to me what you’re trying to do. I’m not sure why you’re returning a list with the initial df in the first callback instead of the df_filtered. Also you are returning an object only after the else. Nothing is returned after the first if statement.

To see examples of chained callbacks, I recommend going to the Dash-Example-Index and clicking the Callbacks dropdown, then click on Chained.

Thanks @adamschroeder i followed the dash example from the index and it solved my problem !

1 Like