I try to use a session state to remove an item from a list (i want in the second step to use this new list, to select another item from it).
My minimal code is here (I have commented out the 2nd select box, since it throws the error before)
import streamlit as st
if 'teams' not in st.session_state:
st.session_state['teams'] = ['GER', 'FRA', 'NED', 'USA', 'UAE', 'ISR', 'THA', 'COL']
teamA_name = st.sidebar.selectbox("Team A", options=st.session_state['teams'])
teamA_name_sub = st.sidebar.button('Submit Team A',key="Team A key")
if teamA_name_sub:
st.session_state['teams'].remove(teamA_name)
st.header(teamA_name)
#teamB_name = st.sidebar.selectbox('Team B', options=st.session_state['teams'])
#teamB_name_sub = st.sidebar.button('Submit Team B', key="Team B key" )
st.write(st.session_state['teams'])
The error occurs after I press the submit button and says:
File "/Users/behnk001/Library/Python/3.8/lib/python/site-packages/streamlit/elements/selectbox.py", line 120, in serialize_select_box
return index_(opt, v)
File "/Users/behnk001/Library/Python/3.8/lib/python/site-packages/streamlit/util.py", line 129, in index_
raise ValueError("{} is not in iterable".format(str(x)))
ValueError: NED is not in iterable
I think the reason why it is complaining is because you are not only trying to change the options list for the teamB_name selectbox, but also the options list for the teamA_name selectbox. Once you click the submit button, you will remove the teamA_name variable from the session_state['teams'], and therefore you remove it from the teamA_name selectbox options. Therefore you have created a workflow that --after clicking the submit button-- removes the chosen option from the list, making it impossible to select the item you want to select.
I recommend to not change session_state['teams'] after you created it. Instead, I would do this:
teamA_name = st.sidebar.selectbox("Team A", options=st.session_state['teams'])
teamB_name = st.sidebar.selectbox('Team B', options=[x for x in st.session_state['teams'] if x != teamA_name)
This method filters the list to remove the item that was already picked, while not affecting the session_state['teams'] list.
@DirkRemmers i have a situation like 2 selectboxes, 1 with regions and another 1 one with countries. how to write the session state for both selectboxes such that when region APAC is selected, countries related to APAC region only to be visible as option and vice versa.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.