import streamlit as st class _QuizGUI: def __init__(self, quiz_data): self.quiz_data = quiz_data if "current_question" not in st.session_state: st.session_state.current_question = 0 if "user_answers" not in st.session_state: st.session_state.user_answers = [] if "completed" not in st.session_state: st.session_state.completed = False if "score" not in st.session_state: st.session_state.score = 0 if "show_confirm" not in st.session_state: st.session_state.show_confirm = False def check_answer(self): selected_option = st.session_state[f"selected_option_{st.session_state.current_question}"] question_data = self.quiz_data[st.session_state.current_question] if selected_option == "---" or selected_option is None: st.warning("Please select an option before proceeding.") return st.session_state.user_answers.append(selected_option) if selected_option == question_data["correct_answer"]: st.session_state.score += 1 if st.session_state.current_question + 1 < len(self.quiz_data): st.session_state.current_question += 1 else: st.session_state.completed = True def load_quiz(self, metadata): if metadata: st.title(f"Subject: {metadata.get("subject", "Unknown Subject")}") st.header(f"Topic: {metadata.get("topic", "Unknown Topic")}") st.markdown(f""" **Quiz Type: {metadata.get("exam_type")}** Number of Questions: {metadata.get("num_questions")} "Timestamp: {metadata.get("timestamp")} """) else: st.warning("No metadata was found.") def start_quiz(self): self.quiz_data = st.session_state.quiz_data metadata = st.session_state.metadata if not st.session_state.quiz_completed: if metadata.get("exam_type") == "Multiple Choice": question_data = self.quiz_data[st.session_state.current_question] st.write(f"**Question {st.session_state.current_question + 1}:** {question_data['question']}") elif metadata.get("exam_type") == "True or False": question_data = self.quiz_data[st.session_state.current_question] st.write(f"**Statement {st.session_state.current_question + 1}:** {question_data['statement']}") st.selectbox( "Select an option:", ["---"] + question_data["options"], key=f"selected_option_{st.session_state.current_question}" ) st.button("Next", on_click=self.check_answer) else: st.success(f"Quiz completed! Your final score is {st.session_state.score}/{len(self.quiz_data)}.") st.write("You can view the summary of your quiz in the results page.") st.session_state.quiz_completed = True def view_results(self): if st.session_state.quiz_completed: st.write("### Quiz Summary") if st.session_state.metadata.get("exam_type") == "Multiple Choice": for i, question_data in enumerate(self.quiz_data): st.write(f"**Question {i + 1}:** {question_data['question']}") st.write(f"- **Options:** {', '.join(question_data['options'])}") st.write(f"- **Correct Answer:** {question_data['correct_answer']}") st.write(f"- **Your Answer:** {st.session_state.user_answers[i]}") if st.session_state.user_answers[i] == question_data["correct_answer"]: st.write("- **Result:** ✅ Correct") else: st.write("- **Result:** ❌ Incorrect") elif st.session_state.metadata.get("exam_type") == "True or False": for i, question_data in enumerate(self.quiz_data): st.write(f"**Statement {i + 1}:** {question_data['statement']}") st.write(f"- **Correct Answer:** {question_data['correct_answer']}") st.write(f"- **Your Answer:** {st.session_state.user_answers[i]}") if st.session_state.user_answers[i] == question_data["correct_answer"]: st.write("- **Result:** ✅ Correct") else: st.write("- **Result:** ❌ Incorrect") def confirm_end_quiz(self): if st.button("End Quiz"): st.session_state.show_confirm = True if st.session_state.show_confirm: with st.container(): st.warning("Really end the quiz now?") col1, col2 = st.columns(2) with col1: if st.button("OK"): # ending the quiz and updating the session state remaining_questions = len(self.quiz_data) - st.session_state.current_question for item in range(remaining_questions): st.session_state.user_answers.append("---") st.session_state.quiz_completed = True st.session_state.page_access["View Results"] = True st.session_state.page_access["Take Exam"] = False st.success("Exam ended successfully! View the results in the result page.") st.session_state.show_confirm = False st.rerun() with col2: if st.button("Cancel"): st.session_state.show_confirm = False st.rerun()