DuplicateWidgetID: How to generate a key in this specific case?

Here are my functions:

def unit_select():
  unit_select.unit = st.selectbox('Select unit for force', ('lbs', 'klbs', 'tons'))

def unit_convert(df):
    if unit_select.unit == 'tons':
        df['A'] = df['B'].apply(some_conversion_func)  
    elif unit_select.unit == 'klbs':
        df['A'] = df['B'].apply(some_other_conversion_func)
    else:
        pass
    return df

if option == 'Unit Conversion Panel':
    unit_select()
    unit_confirm = st.button('Confirm')
    if unit_confirm:
        df = upload_my_df()  # I am loading my df using upload_my_df func which is not shown here
        unit_convert(df)

I am getting an error ’ DuplicateWidgetID: There are multiple identical st.selectbox widgets with the same generated key.’ because I am referring to the same function unit_select multiple time in the func unit_convert while checking for its value. Not sure how I can add the key attribute to the st.selectbox in this particular scenario.

After defining the undefined names your code runs for me without any DuplicateWidgetID errors. Probably they are defined differently in your actual code and that is where the issue lies.

Unfortunately, I am still getting the same error:
DuplicateWidgetID : There are multiple identical st.selectbox widgets with the same generated key.
It comes up at the very last line of code.

I would recommend changing your code to something like this:

def unit_select(key):
  unit_select.unit = st.selectbox('Select unit for force', ('lbs', 'klbs', 'tons'), key=key)

And then passing a unique key whenever you call unit_select()

noted, thanks @blackary

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.