Search code examples
pythonpandasfunctiondashboardstreamlit

multiple same key button generated in streamlit


I have been trying to create a web dashboard with streamlit. The error after running a segment comes to be, " There are multiple identical st.button widgets with the same generated key. "

I am attaching a section of my code below

x = 1
while x > 0:
    if st.sidebar.button("1. Mouthshut.com"):
        analyse(df1)
    if st.sidebar.button("2. Bankbazaar"):
        analyse(df2)
    if st.sidebar.button("3. Creditkaro"):
        analyse(df3)
    if st.sidebar.button("4. Appgrooves"):
        analyse(df4)
    st.header("All the websites combined")
    analyse(df)
    if st.sidebar.button("Exit"):
        break

I would appreciate the help. Thank you


Solution

  • Per the docs: https://docs.streamlit.io/en/stable/api.html#streamlit.button

    key (str) – An optional string to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. Multiple widgets of the same type may not share the same key.

    By not providing a key argument, all widgets have the same None key value. Set a unique value for the key keyword argument in each if statement to fix the error.

    x = 1
    
    b1 = st.sidebar.button("1. Mouthshut.com", key="1")
    b2 = st.sidebar.button("2. Bankbazaar", key="2")
    b3 = st.sidebar.button("3. Creditkaro", key="3")
    b4 = st.sidebar.button("4. Appgrooves", key="4")
    b5 = st.sidebar.button("Exit", key="5")
    
    while x > 0:
        if b1:
           # analyse(df1)
           pass
        if b2:
           # analyse(df2)
           pass
        if b3:
           # analyse(df3)
           pass
        if b4:
           # analyse(df4)
           pass
    
        st.header("All the websites combined")
        #analyse(df)
    
        if b5:
            break