Session state for selectbox widgets inside a form

Hi, Streamlit community,

I have two forms with 27 select boxes, each has a key (key1, key2, , key27) as follows:

ans1 = col1.selectbox('is there any ...?', ('no', 'yes'), key="key1")
ans2 = col1.selectbox('is there any ...?', ('no', 'yes'), key="key2")
...
ans27 = col6.selectbox('is there any ...?',('no', 'yes'), key="key27")

Every time, a random key from the 27 keys I have appears in the key error message after 5 to 30 minutes at random from opening the tab.


From the log:

I tried different solutions such as:
On from submission, I call widgets_states function:

def widgets_states():
    for k in range(1,27):
        keyName = "key"+str(k)
        if keyName not in st.session_state:
            st.session_state[keyName] = ""
    st.experimental_rerun() 

and Checking session states for the select box keys every 5 minutes via Streamlit autorefresh:


count = st_autorefresh(interval=300000, limit=40, key="fizzbuzzcounter")
if(count%5 == 0):
        for k in range(1,27):
            keyName = "key"+str(k)
            if keyName not in st.session_state:
                st.session_state[keyName] = ""
        st.experimental_rerun()

But none of the previous solutions solve the issue. I see that Streamlit generates keys for the widget although I already gave keys! maybe this is the issue reason but how to solve it!

It would be much appreciated if you can help me fix this issue, please?

  • Streamlit version: 1.13.0 and 1.15.0
  • Python version: 3.9
  • Browser version: Google Chrome 107.0.5304.107, Firefox 10.7.0

Requirements file

streamlit==1.13.0
pymongo==4.3.2
boto3==1.26.5
streamlit_authenticator==0.2.1
htbuilder==0.6.1
streamlit_autorefresh==0.0.1