Dropdowns not clearing when switching radioitems

I have a few dropdowns dependent on radio items. What is allowed to populate in the dropdowns depend on the radio items. However when I change the radio items, back and forth, it doesn’t reset the dropdown selections. Here is a simple example of what I’m attempting that’s not working. Didn’t include the layout part but the df is in a dcc.Store component, the drops are in dcc.Dropdown and the radio is a dbc.RadioItems.

df = pd.DataFrame({'A': [1, 2, 3, 4, 5],
                   'B': [6, 7, 8, 9, 10],
                   'C': [11, 12, 13, 14, 15],
                   'D': [16, 17, 18, 19, 20]}

app.callback(
   Output('drop-2', 'options'),
   Output('drop-3', 'options'),
   Output('drop-1', 'value'),
   Output('drop-2', 'value'),
   Input('drop-1', 'value'),
   Input('drop-2', 'value'),
   Input('radio', 'value'),
   State('store', 'data')
def update(value1, value2, radio, data):
    df = pd.DataFrame(data['df'])
    if radio == 'foo':
        if not value1:
            return [], [], '', '', ''
        elif value1:
            if not value2:
                return df['A'], [], value1, '', ''
            elif value2:
                return df['A'], df['B'], value1, value2
    elif radio == 'goo':
        if not value1:
            return [], [], [], '', '', ''
        elif value1:
            if not value2:
            if not value2:
                return df['C'], [], [], value1, '', ''
            elif value2:
                return df['C'], df['D'], value1, value2

With this code I can get the dropdowns to reset when changing previous drop downs but when I change radios back and forth they aren’t clearing. I’m sure it’s a simple fix but I just can’t see it.

Hi @bpolasek, You have lots of errors in the sample code you provided. In the future if you’re posting a MRE you should test it to see if it will interpret otherwise the person attempting to help you needs to correct all that before they can troubleshoot it (Obviously if you’re having issues getting it to Interpreted that’s a different scenario).

Ideally you would also include the components as well. This way the person trying to assist can copy paste the code and go from there instead of fixing the interpreter errors as well as coding the components in.

Since there’s no components supplied in this example can you confirm if your dropdowns are set with clearable=True?

Another thing I’m seeing is you have Drop-1 and Drop-2 Values as both an Input and Output maybe you’re running into some circular import issues?

Lastly have you looked into the Dash Dev Tools, Error Reporting and Callbacks Graphs and see what each component is displaying when you do the various toggles?

If you can reply back with a MRE that has the components and doesn’t error out when Interpreting I can do some further troubleshooting to try and assist.

Thanks
Payton

1 Like

Ugh that’s what I get for trying to do it on the fly :weary:

Anyways, I removed the output options that had drop-down values along with changing how I updated the Dropdowns in general and got it to work.

But thank you for all your advice! I was trying to make it easier since the actual code is all over the place. Next time I’ll have to work on it more!