Keep checkbox values

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)

Thanks in advance

Hola,

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)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.