Why checkboxes status do not change after st.form_submit_button()?

  1. Running locally.

Here is the code:

import streamlit as st
from datetime import datetime

# Streamlit App
st.title("Checkbox Tracking Example")

# List of items
items = ["Item 1", "Item 2", "Item 3", "Item 4"]

# Initialize session state if not present
if "checkbox_statusses" not in st.session_state:
    st.session_state.checkbox_statusses = [False] * len(items)

# Using st.form to group the checkboxes
with st.form("checkbox_form"):
    # Display checkboxes
    for i, item in enumerate(items):
        st.checkbox(f"{item} - Checkbox {i + 1}", key=i, value=st.session_state.checkbox_statusses[i])

    # Submit button to update session state
    submit_button = st.form_submit_button("Submit")

# Display session state after submitting
if submit_button:
    st.write("Checkbox Statuses:", st.session_state.checkbox_statusses)

Why should they?

1 Like

I :heartbeat: toxicity !!!
Poisonousness !!!

Can you come up with an honest answer, badged streamlit guy?

Hi @Vitor_Rolla,

Thanks for sharing your question. If you set clear_on_submit to True, the checkboxes should clear:

import streamlit as st
from datetime import datetime

# Streamlit App
st.title("Checkbox Tracking Example")

# List of items
items = ["Item 1", "Item 2", "Item 3", "Item 4"]

# Initialize session state if not present
if "checkbox_statusses" not in st.session_state:
    st.session_state.checkbox_statusses = [False] * len(items)

# Using st.form to group the checkboxes
with st.form("checkbox_form", clear_on_submit=True):
    # Display checkboxes
    for i, item in enumerate(items):
        st.checkbox(f"{item} - Checkbox {i + 1}", key=i, value=st.session_state.checkbox_statusses[i])

    # Submit button to update session state
    submit_button = st.form_submit_button("Submit")

# Display session state after submitting
if submit_button:
    st.write("Checkbox Statuses:", st.session_state.checkbox_statusses)

I asked a honest question. The honest answer is that they donā€™t change because there is nothing in your code that makes them change. I wonder why you would think otherwise.

Of course tthe values of the checkboxes change, but they are not in st.session_state.checkbox_statusses. If that is what you want, look up the keys. That is what they are there for.

st.write([st.session_state[i] for i in range(len(items))])
1 Like

Thanks Caroline!

I made a mocked example; ergo, the truth is that I am issuing the form after clicking on a standard button.

I found some hints over hereā€¦

Thank you very much!

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