Hi I trying to create a bidirectional multiselect
eg: I have include cities and exclude cities dropdown I need to update the list of include and exclude cities based on the selection. I’m using session state to achieve it
in the below code cities is a dictionary I maintain of my list of cities
with st.container():
if 'include_cities' not in st.session_state:
st.session_state.include_cities = []
if 'exclude_cities' not in st.session_state:
st.session_state.exclude_cities = []
def get_available_cities(cities_include, cities_exclude):
return {
"include": [city for city in cities if city not in cities_exclude],
"exclude": [city for city in cities if city not in cities_include]
}
def update_cities():
if 'include_multiselect_c' in st.session_state:
st.session_state.include_cities = st.session_state.include_multiselect_c
if 'exclude_multiselect_c' in st.session_state:
st.session_state.exclude_cities = st.session_state.exclude_multiselect_c
available_cities = get_available_cities(st.session_state.include_cities, st.session_state.exclude_cities)
include_treatment_c = st.multiselect("**INCLUDE:**", options=available_cities['include'],default=st.session_state.include_cities,key='include_multiselect_c', on_change=update_cities() )
exclude_treatment_c = st.multiselect("**EXCLUDE:**", options=available_cities['exclude'],default=st.session_state.exclude_cities, key='exclude_multiselect_c', on_change=update_cities())
The multiselect doesn’t behave as expected
- The multiselect has an erratic behavior - it sometimes allows me to select multiple cities sometimes, it just closes the dropdown
- The list of available cities isn’t updated - say i add a city in exclude option it shows the option in the include as well but when i click on it. the dropdown just closes and next time i open the options are corrected and doesn’t show the state already chosen
- Help me with the fix