Update graph by clearing dropdown value

Hi, I’m very beginner of python and dash.
I have two dropdowns 1) Single: Country, 2) Multi: Province.

If both country and province are selected, I would like to show a province graph.
if country is selected, but province is none, I would like to show a country graph.
However, when I clear province from dropdown, no graph will be shown.

I would to have your kind help and suggestion.

@app.callback(Output('my_graph', 'figure'),
                Input('country_dropdown', 'value'),
                Input('province_dropdown', 'value'),
                Input('date_picker', 'start_date'),
                Input('date_picker', 'end_date')

def update_graph(country, province, start_date, end_date):
    if country is not None and province is None:
        df1 = df.groupby(['ObservationDate','Country/Region'], as_index=True).agg('sum')
        df1 = df1.loc[(slice(start_date,end_date),country),:]
        fig = px.line(df1, x= df1.index.get_level_values('ObservationDate') , y= 'Confirmed', color=df1.index.get_level_values('Country/Region'))
        return fig

        df1 = df.groupby(['ObservationDate','Country/Region','Province/State'], as_index=True).agg('sum')
        df1 = df1.loc[(slice(start_date,end_date),country, province),:]
        fig = px.line(df1, x= df1.index.get_level_values('ObservationDate') , y= 'Confirmed', color=df1.index.get_level_values('Province/State'))
        return fig

If I clear ‘California’, graph is disappeared.


This happens because your province dropdown (with "multi"=True) returns an empty list as “value” when no value is selected. As you are checking it for None only, then you end up in the second statement, which (probably) returns an empty dataframe and an empty figure.

Checking if province is not an empty list should do the work.

Hope this helps! :slight_smile:

Thank you very much!! I could figure out it now :grinning: