selected_shape = st.selectbox(“Select a shape:”, shapes, index=default_index)
st.write(“You selected:”, selected_shape)
st.session_state[“chosen_shape”] = selected_shape
I’m encountering an issue with a simple selectbox with index parameter. see the example code, , the selectbox resets its display back to the previous selection on every rerun. i understand when selectionbox is rerendered, the default index is still the previous values, so even when user select a new value, it immediately resets back to previous one because of index. so you have to eseentially select twice for a new item be selected. but Is this expected behavior when using the ‘index’ parameter? then how do we give a default value to selectbox when it’s rendered?
try this: Using shapes.index(st.session_state[‘chosen_shape’]). This index is calculated each time the selection box is rendered, ensuring that the current value of chosen_shape is always displayed
import streamlit as st
shapes = ["Círculo", "Cuadrado", "Triángulo"]
# Inicializa la forma elegida en el estado de sesión si no existe
if "forma_elegida" not in st.session_state:
st.session_state["forma_elegida"] = "Círculo"
# Crea el cuadro de selección con el valor actual de forma_elegida
forma_seleccionada = st.selectbox("Seleccione una forma:", shapes, index=shapes.index(st.session_state["forma_elegida"]))
# Muestra la forma seleccionada
st.write("Ha seleccionado:", forma_seleccionada)
# Actualiza el estado de sesión con la nueva forma seleccionada
st.session_state["forma_elegida"] = forma_seleccionada
This is a major problem in streamlit… Streamlit confuses itself with the index it is called with and the index of the new option that was selected just now…
Replace the call to st.selectbox() in your code with the following gist (be careful with the argument order, they are not the same)
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.