Search code examples
pythonstreamlit

DuplicateWidgetID: There are multiple identical st.checkbox widgets with the same generated key


I am building stream lit app, I defined two function, sidebar, tab, etc. output of first function is simply a data frame, and output of the second function is chart. I get the error as follows. The error seems to be because of the second function


def func():
    option_1 = st.sidebar.checkbox('x1', value=True)
    option_2 = st.sidebar.checkbox('x2')
    option_3 = st.sidebar.checkbox('x3')

    dfall = read()
    dfs = []
    if option_1 :
        dfs.append(dfall[0])
    if option_2 :
        dfs.append(dfall[1])
    if option_3 :
        dfs.append(dfall[2])

    if len(dfs) > 1:

        df = pd.concat(dfs)
       
    elif len(dfs) == 1:
        df = dfs[0]

   do something on df..
   return df

def chart()
   df= func()

   plot the chart 
   return 


with tab1:
    intro()

with tab2:
    func()
    
with tab3:
    chart()


   

The error:

DuplicateWidgetID: There are multiple identical st.checkbox widgets with the same generated key.

When a widget is created, it's assigned an internal key based on its structure. Multiple widgets with an identical structure will result in the same internal key, which causes this error.

To fix this error, please pass a unique key argument to st.checkbox.


Solution

  • The error is self-explanatory. You are having multiple st.checkbox widgets maybe in one of your functions with same keys. every check box must have a unique key. If key is not set, the default key is None all wiget will have None as their key and it will still throw the same error.
    Example:

    check_one = st.checkbox("Tick", key="check_1") # key must be unique
    check_two = st.checkbox("Tick", key="check_2") # key must be unique
    

    In your case

    def func(key1, key2, key3):
        option_1 = st.sidebar.checkbox('x1', key={key1}, value=True)
        option_2 = st.sidebar.checkbox('x2', key={key2})
        option_3 = st.sidebar.checkbox('x3', key={key3})
    
        dfall = read()
        dfs = []
        if option_1 :
            dfs.append(dfall[0])
        if option_2 :
            dfs.append(dfall[1])
        if option_3 :
            dfs.append(dfall[2])
    
        if len(dfs) > 1:
    
            df = pd.concat(dfs)
           
        elif len(dfs) == 1:
            df = dfs[0]
    
       do something on df..
       return df
    
    def chart():
       df= func(key1="8reh", key2="hdfu03", key3="ryyw32")
    
       plot the chart 
       return 
    
    
    with tab1:
        intro()
    
    with tab2:
        func(key1="wwr93", key2="rpt49", key3="rt40")
        
    with tab3:
        chart()