简体   繁体   中英

Python Dash Callback is not updating Data when I select Dropdown Values

Can anyone let me know why my code is not updating the graph with data when I select drop-down value? (entire GitHub code in link in comments below)

def filterPollutants(selected_pollutants):
    if selected_pollutants:
        dff = df.loc[df.pollutant_abb.isin([selected_pollutants])]
    else:
        dff = df

    bar_fig = {
        "data": [
            go.Bar(
                x=dff["U_SAMPLE_DTTM"],
                y=dff["DISPLAYVALUE"],
            )
        ],
        "layout": go.Layout(
            title="Sampling for Local Limits",
            # yaxis_range=[0,2],
            yaxis_title_text="Metals mg/L",
        ),
    }

From testing the code at the Github link you shared I think the problem is in this line where you filter your data set in the callback:

dff = df.loc[df.pollutant_abb.isin([selected_pollutants])]

The problem with this line is that the value of selected_pollutants inside the callback is of the form ['SELECTED_VALUE_FROM_DROPDOWN'] and not 'SELECTED_VALUE_FROM_DROPDOWN' . This is because your dropdown has multi set to True .

But because of this your isin filter doesn't work, since you're essentially doing this:

dff = df.loc[df.pollutant_abb.isin([['SELECTED_VALUE_FROM_DROPDOWN']])]

instead of this:

dff = df.loc[df.pollutant_abb.isin(['SELECTED_VALUE_FROM_DROPDOWN'])]

So the fix could be to remove the list surrounding selected_pollutants :

dff = df.loc[df.pollutant_abb.isin(selected_pollutants)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM