Reset all CheckBoxes after submittion!

Hello everybody, I am working on a project for daily inspection of forklifts. Specifically, when I enter submit, appears another button having the chance to submit another form I want when I enter this button all my checkboxes to be reseted. Thank you in advance, I share my code below:

date = st.date_input("Date")
employee_name = st.selectbox("Employee Name", ["Simeon Papadopoulos", "Alexandridis Christos"])
num_forklifts = st.selectbox("Number of Forklifts", ["ME 119135", "ME 125321"])
inspection_fields = [
    {"name": "Brake Inspection", "checked": False, "broken": False, "comment": ""},
    {"name": "Engine", "checked": False, "broken": False, "comment": ""},
    {"name": "Lights", "checked": False, "broken": False, "comment": ""},
    {"name": "Tires", "checked": False, "broken": False, "comment": ""},
]
for i, field in enumerate(inspection_fields):
    st.subheader(field["name"])
    field["checked"] = st.checkbox("Checked", key=f"checked_{i}")
    field["broken"] = st.checkbox("Breakdown", key=f"broken_{i}")
    if field["broken"] and not field["comment"]:
        st.warning(f"Please provide comments for {field['name']} breakdown.")
    field["comment"] = st.text_area("Comments", max_chars=50, height=10, key=f"comment_{i}")
if st.button("Submit"):
    st.success("Form submitted successfully!")
    if st.button("Submit Another Form"):
        st.experimental_session_state.pop("form_data", None)
        for field in inspection_fields:
            field["checked"] = False
            field["broken"] = False
            field["comment"] = ""
1 Like

Hi @pappasym153

Since you’re having nested buttons, the app reset is to be expected owing to the inherent app logic of Streamlit. To circumvent this, you’ll need to use Session State for the first and second button so that the app knows the button selection status.

Please go through our in-depth video on Session state and the Streamlit Docs page on Session State.

Best regards,
Chanin

Thank you very much for your reply I try to catch it!

I tried to do it with Session State but the same result. If someone can help I would be grateful.

import streamlit as st
import pandas as pd
import os 

date = st.date_input("Date")
employee_name = st.selectbox("Employee Name", ["Simeon Papadopoulos", "Alexandridis Christos"])
num_forklifts = st.selectbox("Number of Forklifts", ["ME 119135", "ME 125321"])

inspection_fields = [
    {"name": "Brake Inspection", "checked": False, "broken": False, "comment": ""},
    {"name": "Engine", "checked": False, "broken": False, "comment": ""},
    {"name": "Lights", "checked": False, "broken": False, "comment": ""},
    {"name": "Tires", "checked": False, "broken": False, "comment": ""},
]
for i, field in enumerate(inspection_fields):
    st.subheader(field["name"])
    st.session_state.setdefault(f"checked_{i}", False)  # initialize checked key
    st.session_state.setdefault(f"broken_{i}", False)  # initialize broken key
    field["checked"] = st.checkbox("Checked", key=f"checked_{i}")
    field["broken"] = st.checkbox("Breakdown", key=f"broken_{i}")
    if field["broken"] and not field["comment"]:
        st.warning(f"Please provide comments for {field['name']} breakdown.")
    field["comment"] = st.text_area("Comments", max_chars=50, height=10, key=f"comment_{i}")

if "inspection_data.csv" not in os.listdir():
    df = pd.DataFrame(columns=["Date", "Employee Name", "Number of Forklifts"] + [field["name"] for field in inspection_fields] + [f"{field['name']} Comments" for field in inspection_fields])
    df.to_csv("inspection_data.csv", index=False)

if st.button("submit"):
    # Check if all required fields are filled out
    if not all(field["checked"] or (field["broken"] and field["comment"]) for field in inspection_fields):
        st.warning("Please fill out all required fields.")
    else:
        # Create a pandas DataFrame from the form data
        data = {"Date": date, "Employee Name": employee_name, "Number of Forklifts": num_forklifts}
        

        df = pd.DataFrame(data, index=[0])
        st.write(df)

        # Save the form data to a CSV file
        df.to_csv("inspection_data.csv", mode="a", header=not os.path.exists("inspection_data.csv"), index=False)

        # Show a success message and a button to submit another form
        st.success("Form submitted successfully!")
        if st.button("Submit another form"):
            for i, field in enumerate(inspection_fields):
                checked_key = f"checked_{i}"
                broken_key = f"broken_{i}"
                st.session_state[checked_key] = False  # reset checked key
                st.session_state[broken_key] = False  # reset broken key