✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Callback errors

Hi All,

I have 4 dropdown options 1 is a date and the other 3 are strings, basically I need them to each affect each other, I have created the following callback which logically should do what I want it to do but I am getting errors:

@app.callback(
    Output('country_name', 'options'),
    Output('league_name', 'options'),
    Output('match_name', 'options'),
    Input('country_name', 'value'),
    Input('league_name', 'value'),
    Input('date_name', 'date'))
def update_league_dropdown(chosen_category, chosen_league, chosen_date):
    if chosen_date is not None and chosen_category is None and chosen_league is None:
        dff = df[df.EventDate == chosen_date]
        return [{'label': i, 'value': i} for i in sorted(dff.CategoryName.unique())], \
            [{'label': i, 'value': i} for i in sorted(dff.TournamentName.unique())], \
            [{'label': i, 'value': i} for i in sorted(dff.Match.unique())]
    elif chosen_category is not None and chosen_date is None and chosen_league is None:
        dff = df[df.CategoryName == chosen_category]
        return [{'label': i, 'value': i} for i in sorted(df.CategoryName.unique())], \
               [{'label': i, 'value': i} for i in sorted(dff.TournamentName.unique())], \
               [{'label': i, 'value': i} for i in sorted(dff.Match.unique())]
    elif chosen_league is not None and chosen_category is None and chosen_date is None:
        dff = df[df.TournamentName == chosen_league]
        return [{'label': i, 'value': i} for i in sorted(dff.CategoryName.unique())], \
               [{'label': i, 'value': i} for i in sorted(df.TournamentName.unique())], \
               [{'label': i, 'value': i} for i in sorted(dff.Match.unique())]
    elif chosen_date is not None and chosen_category is not None and chosen_league is None:
        dff = df[df.EventDate == chosen_date] and df[df.CategoryName == chosen_category]
        return [{'label': i, 'value': i} for i in sorted(df.CategoryName.unique())], \
               [{'label': i, 'value': i} for i in sorted(dff.TournamentName.unique())], \
               [{'label': i, 'value': i} for i in sorted(dff.Match.unique())]
    elif chosen_date is not None and chosen_category is None and chosen_league is not None:
        dff = df[df.EventDate == chosen_date] and df[df.TournamentName == chosen_league]
        return [{'label': i, 'value': i} for i in sorted(dff.CategoryName.unique())], \
               [{'label': i, 'value': i} for i in sorted(df.TournamentName.unique())], \
               [{'label': i, 'value': i} for i in sorted(dff.Match.unique())]
    elif chosen_date is None and chosen_category is not None and chosen_league is not None:
        dff = df[df.CategoryName == chosen_category] and df[df.TournamentName == chosen_league]
        return [{'label': i, 'value': i} for i in sorted(df.CategoryName.unique())], \
               [{'label': i, 'value': i} for i in sorted(dff.TournamentName.unique())], \
               [{'label': i, 'value': i} for i in sorted(dff.Match.unique())]
    elif chosen_date is not None and chosen_category is not None and chosen_league is not None:
        dff = df[df.CategoryName == chosen_category] and df[df.TournamentName == chosen_league] \
              and df[df.EventDate == chosen_date]
        return [{'label': i, 'value': i} for i in sorted(df.CategoryName.unique())], \
               [{'label': i, 'value': i} for i in sorted(dff.TournamentName.unique())], \
               [{'label': i, 'value': i} for i in sorted(dff.Match.unique())]
    elif chosen_date is None and chosen_category is None and chosen_league is None:
        return [{'label': i, 'value': i} for i in sorted(df.CategoryName.unique())], \
               [{'label': i, 'value': i} for i in sorted(df.TournamentName.unique())], \
               [{'label': i, 'value': i} for i in sorted(df.Match.unique())]

I am getting errors though when attempting to select 2 or more of the dropdowns together:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

The error line for my test is:

dff = df[df.CategoryName == chosen_category] and df[df.TournamentName == chosen_league]

I selected a Category and a league.

I have attempted to change my where clauses to be:

dff = (df[df.CategoryName == chosen_category]) & (df[df.TournamentName == chosen_league])

but I get Bool errors - any assistance as to where I have gone wrong would be greatly appreciated!

Hello @DrSmith69,

Looks like your where clause needs some fixing.

Try this:

dff = df[(df.CategoryName == chosen_category) & (df.TournamentName == chosen_league)]
1 Like

Thank you worked like a treat, I had my brackets in the wrong place originally!

1 Like