form_management / form_helper.py
louiecerv
first save
1e6bb5e
import streamlit as st
class FormHelper:
def __init__(self):
"""Initializes the FormHelper with default session state values."""
if "exam_loaded" not in st.session_state:
st.session_state.exam_loaded = False
if "exam_started" not in st.session_state:
st.session_state.exam_started = False
if "current_form" not in st.session_state:
st.session_state.current_form = "about"
def show_about_page(self):
"""Displays the About page."""
st.title("About This App")
st.write("This is a dummy app for demonstrating form management in Streamlit.")
def show_load_exam_page(self):
"""Displays the Load Exam page and handles exam loading."""
st.title("Load Exam")
st.write("Simulating exam loading...")
if st.button("Exam Loaded"):
st.session_state.exam_loaded = True
st.session_state.current_form = "take_exam"
st.rerun()
def show_take_exam_page(self):
"""Displays the Take Exam page with dummy questions and handles exam finishing."""
st.title("Take Exam")
st.session_state.exam_started = True
st.write("Displaying exam questions here...")
for i in range(5):
st.write(f"Question {i+1}: What is the capital of France?")
st.radio(f"Select answer for question {i+1}", ("Berlin", "Paris", "Madrid", "Rome"))
if st.button("Finish Exam"):
st.session_state.exam_started = False
st.session_state.current_form = "view_result"
st.rerun()
if st.button("End Exam Early"):
st.session_state.exam_started = False
st.session_state.current_form = "view_result"
st.rerun()
def show_view_result_page(self):
"""Displays the View Result page with dummy results and handles exam retake/reload."""
st.title("View Result")
st.write("Displaying exam results here...")
st.write("You answered 3 out of 5 questions correctly.")
if st.button("Retake Exam"):
st.session_state.current_form = "take_exam"
st.rerun()
if st.button("Load Another Exam"):
st.session_state.exam_loaded = False
st.session_state.current_form = "load_exam"
st.rerun()
def show_sidebar(self):
"""Displays the sidebar with navigation buttons."""
with st.sidebar:
st.title("Navigation")
if st.button("About"):
st.session_state.current_form = "about"
st.rerun()
if not st.session_state.exam_started:
if st.button("Load Exam"):
st.session_state.current_form = "load_exam"
st.rerun()
if st.session_state.exam_loaded and st.session_state.exam_started:
if st.button("Take Exam"):
st.session_state.current_form = "take_exam"
st.rerun()
if "current_form" in st.session_state and st.session_state.current_form == "view_result":
if st.button("View Result"):
st.session_state.current_form = "view_result"
st.rerun()
def run(self):
"""Runs the app by displaying the appropriate page based on the current form."""
self.show_sidebar()
if st.session_state.current_form == "about":
self.show_about_page()
elif st.session_state.current_form == "load_exam":
self.show_load_exam_page()
elif st.session_state.current_form == "take_exam":
self.show_take_exam_page()
elif st.session_state.current_form == "view_result":
self.show_view_result_page()