Hello everybody, I am so excited to be part of this community.
I try to create an app for daily inspection on a mobile equipment. I have put some check boxes and I want when I enter another submit to clear all check boxes.
This is my code:
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"])
form = st.form("checkboxes", clear_on_submit = True)
# Inspection fields
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": ""},
]
# Loop through the inspection fields
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}")
# Submit button
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}
for i, field in enumerate(inspection_fields):
data[field["name"]] = f"{'X' if field['checked'] else ''} {'B' if field['broken'] else ''}"
data[f"{field['name']} Comments"] = field["comment"]
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)
# Reset the form fields
# Show a success message and a button to submit another form
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"] = ""
Thank you in advance!