Hi.
I am working on an App and I want to make a selectbox where you can choose how many checkbox you want, but when you hide and show the checkbox the values return to false. ¿Can someone help with this?
import streamlit as st
nArranques=st.session_state.get(f'num_arranques')
if nArranques == None:
nArranques=1
def crear():
for i in range(nArranques):
checkbox(i)
def checkbox(num):
st.checkbox("Prueba",key=f'checkbox{num}',value=st.session_state.get(f'checkbox{num}'))
nArranques=st.selectbox("Número de arranques",(0,1,2,3),key=f'num_arranques', index=1, on_change=crear())
st.write(st.session_state)
Tal vez te pueda servir, hay un pequeño error al iniciar la página pero el resto de código muestra los checkbox en función a la selección:
Maybe it can help you, there is a small error at the start of the page but the rest of the code shows the checkboxes according to the selection:
import streamlit as st
# Get the number of arranques from the session state or default to 0
nArranques = st.session_state.get('num_arranques', 0)
new_nArranques = st.selectbox("Número de arranques", (0, 1, 2, 3), key='num_arranques', index=1)
# Check if the value of nArranques has changed
if new_nArranques != nArranques:
st.session_state.num_arranques = new_nArranques
# Define a function to create checkboxes based on the selected number of arranques
def crear():
checkboxes = []
for i in range(new_nArranques):
# Generate unique key for each checkbox
checkbox_key = f"checkbox_{i}"
checkbox_value = st.checkbox(f"Prueba {i+1}", key=checkbox_key)
checkboxes.append(checkbox_value)
return checkboxes
# Call the function to create checkboxes based on the selected number of arranques
checkbox_values = crear()
# Write the session state to the app
st.write(st.session_state)
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.