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"] = ""
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.
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
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
Hello there ๐๐ป
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.