louiecerv commited on
Commit
00c87b7
·
1 Parent(s): 9c3ca76

progress in the quiz interface

Browse files
__pycache__/quiz_gui_helper.cpython-313.pyc ADDED
Binary file (5.41 kB). View file
 
app.py CHANGED
@@ -4,6 +4,22 @@ import streamlit as st
4
  if "page_access" not in st.session_state:
5
  st.session_state.page_access = {"About": True, "Load Exam": True, "Take Exam": False, "View Results": False}
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  def main():
9
 
@@ -11,11 +27,12 @@ def main():
11
  st.title("Exam Kiosk")
12
 
13
  # Logic to control page access
14
- if "exam_loaded" not in st.session_state:
15
- st.session_state.exam_loaded = False
 
 
 
16
 
17
- if "exam_completed" not in st.session_state:
18
- st.session_state.exam_completed = False
19
 
20
  # Provide instructions
21
  instructions = """
@@ -69,5 +86,6 @@ def main():
69
  """
70
  st.markdown(instructions)
71
 
 
72
  if __name__ == "__main__":
73
  main()
 
4
  if "page_access" not in st.session_state:
5
  st.session_state.page_access = {"About": True, "Load Exam": True, "Take Exam": False, "View Results": False}
6
 
7
+ if "current_page" not in st.session_state:
8
+ st.session_state.current_page = "Main"
9
+
10
+ if "quiz_data" not in st.session_state:
11
+ st.session_state.quiz_data = []
12
+
13
+ if "metadata" not in st.session_state:
14
+ st.session_state.metadata = []
15
+
16
+ # Navigation logic
17
+ if st.session_state.current_page == "About":
18
+ st.session_state.current_page = "1_About"
19
+ if st.session_state.current_page == "Load Exam":
20
+ st.session_state.current_page = "2_Load Exam"
21
+ elif st.session_state.current_page == "Take Exam":
22
+ st.session_state.current_page = "3_Take Exam"
23
 
24
  def main():
25
 
 
27
  st.title("Exam Kiosk")
28
 
29
  # Logic to control page access
30
+ if "quiz_loaded" not in st.session_state:
31
+ st.session_state.quiz_loaded = False
32
+
33
+ if "quiz_completed" not in st.session_state:
34
+ st.session_state.quiz_completed = False
35
 
 
 
36
 
37
  # Provide instructions
38
  instructions = """
 
86
  """
87
  st.markdown(instructions)
88
 
89
+
90
  if __name__ == "__main__":
91
  main()
pages/2_Load_Exam.py CHANGED
@@ -1,14 +1,53 @@
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  def main():
4
  st.title("Load Exam")
 
 
5
 
6
- # Mock exam loading functionality
7
- if st.button("Load Exam"):
8
- st.session_state.exam_loaded = True
 
 
 
 
9
  st.session_state.page_access["Take Exam"] = True
10
  st.session_state.page_access["Load Exam"] = False
11
  st.success("Exam loaded successfully!")
12
 
 
 
 
 
 
 
13
  if __name__ == "__main__":
14
  main()
 
1
  import streamlit as st
2
+ import json
3
+ from quiz_gui_helper import _QuizGUI
4
+
5
+ # Initialize session state
6
+ if "page_access" not in st.session_state:
7
+ st.session_state.page_access = {"About": True, "Load Exam": True, "Take Exam": False, "View Results": False}
8
+
9
+ def load_quiz_from_file():
10
+ uploaded_file = st.file_uploader("Upload a JSON quiz file", type="json")
11
+ if uploaded_file is not None:
12
+ try:
13
+ # Load the JSON content
14
+ quiz = json.load(uploaded_file)
15
+
16
+ # Extract metadata and content
17
+ metadata = quiz.get("metadata", {})
18
+ quiz_data = quiz.get("content", [])
19
+
20
+ # Save the metadata and content to session state
21
+ st.session_state.metadata = metadata
22
+ st.session_state.quiz_data = quiz_data
23
+
24
+ # Return the extracted data
25
+ return metadata, quiz_data
26
+ except json.JSONDecodeError:
27
+ st.error("Invalid JSON file. Please upload a properly formatted JSON file.")
28
+ return None, None
29
 
30
  def main():
31
  st.title("Load Exam")
32
+ metadata, quiz_data = load_quiz_from_file()
33
+
34
 
35
+ if metadata and quiz_data:
36
+
37
+ quiz_app = _QuizGUI(quiz_data)
38
+ quiz_app.load_quiz(metadata)
39
+
40
+ # update the session state
41
+ st.session_state.quiz_loaded = True
42
  st.session_state.page_access["Take Exam"] = True
43
  st.session_state.page_access["Load Exam"] = False
44
  st.success("Exam loaded successfully!")
45
 
46
+ st.write("You can now proceed to the 'Take Exam' page to start the quiz.")
47
+
48
+ else:
49
+ st.info("Please upload a quiz JSON file to begin.")
50
+
51
+
52
  if __name__ == "__main__":
53
  main()
pages/3_Take_Exam.py CHANGED
@@ -1,23 +1,55 @@
1
  import streamlit as st
 
 
2
 
 
 
 
 
 
 
 
 
3
 
4
- def main():
5
- st.title("Take Exam")
6
 
7
- if not st.session_state.exam_loaded:
 
8
  st.error("Please load an exam first!")
9
  else:
10
- st.write("Start taking the exam.")
11
-
12
- if st.button("Finish Exam"):
13
- st.session_state.exam_completed = True
14
- st.session_state.page_access["View Results"] = True
15
- st.session_state.page_access["Take Exam"] = False
16
- st.success("Exam completed successfully!")
17
- elif st.button("End Exam Early"):
18
- st.session_state.page_access["View Results"] = True
19
- st.session_state.page_access["Take Exam"] = False
20
- st.warning("Exam ended early.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  if __name__ == "__main__":
23
  main()
 
1
  import streamlit as st
2
+ import json
3
+ from quiz_gui_helper import _QuizGUI
4
 
5
+ if "current_question" not in st.session_state:
6
+ st.session_state.current_question = 0
7
+
8
+ if "quiz_started" not in st.session_state:
9
+ st.session_state.quiz_started = False
10
+
11
+ if "quiz_loaded" not in st.session_state:
12
+ st.session_state.quiz_loaded = False
13
 
 
 
14
 
15
+ def main():
16
+ if not st.session_state.quiz_loaded:
17
  st.error("Please load an exam first!")
18
  else:
19
+
20
+ if st.session_state.quiz_started:
21
+ st.write("You are currently taking the exam.")
22
+
23
+ else:
24
+ #read the metadata and quiz_data from the session state
25
+ metadata = st.session_state.metadata
26
+ quiz_data = st.session_state.quiz_data
27
+
28
+ quiz_app = _QuizGUI(quiz_data)
29
+ quiz_app.load_quiz(metadata)
30
+
31
+ st.write("Start taking the exam.")
32
+ if st.button("Start Exam"):
33
+ st.session_state.page_access["Take Exam"] = True
34
+ st.session_state.page_access["View Results"] = False
35
+ st.session_state.current_question = 0
36
+ st.session_state.user_answers = []
37
+ st.session_state.completed = False
38
+ st.session_state.score = 0
39
+
40
+ _QuizGUI(quiz_data).start_quiz()
41
+ st.session_state.quiz_started = True
42
+
43
+
44
+ if st.button("Finish Exam"):
45
+ st.session_state.quiz_completed = True
46
+ st.session_state.page_access["View Results"] = True
47
+ st.session_state.page_access["Take Exam"] = False
48
+ st.success("Exam completed successfully!")
49
+ elif st.button("End Exam Early"):
50
+ st.session_state.page_access["View Results"] = True
51
+ st.session_state.page_access["Take Exam"] = False
52
+ st.warning("Exam ended early.")
53
 
54
  if __name__ == "__main__":
55
  main()
pages/4_View_Result.py CHANGED
@@ -9,14 +9,14 @@ def main():
9
  st.write("Displaying results...")
10
 
11
  if st.button("Retake Exam"):
12
- st.session_state.exam_completed = False
13
- st.session_state.exam_loaded = True
14
  st.session_state.page_access["Take Exam"] = True
15
  st.session_state.page_access["View Results"] = False
16
  st.success("You can retake the exam.")
17
  elif st.button("Load Another Exam"):
18
- st.session_state.exam_loaded = False
19
- st.session_state.exam_completed = False
20
  st.session_state.page_access = {"About": True, "Load Exam": True, "Take Exam": False, "View Results": False}
21
  st.success("Ready to load another exam.")
22
 
 
9
  st.write("Displaying results...")
10
 
11
  if st.button("Retake Exam"):
12
+ st.session_state.quiz_completed = False
13
+ st.session_state.quiz_loaded = True
14
  st.session_state.page_access["Take Exam"] = True
15
  st.session_state.page_access["View Results"] = False
16
  st.success("You can retake the exam.")
17
  elif st.button("Load Another Exam"):
18
+ st.session_state.quiz_loaded = False
19
+ st.session_state.quiz_completed = False
20
  st.session_state.page_access = {"About": True, "Load Exam": True, "Take Exam": False, "View Results": False}
21
  st.success("Ready to load another exam.")
22
 
quiz_gui_helper.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ class _QuizGUI:
4
+ def __init__(self, quiz_data):
5
+ self.quiz_data = quiz_data
6
+ if "current_question" not in st.session_state:
7
+ st.session_state.current_question = 0
8
+ if "user_answers" not in st.session_state:
9
+ st.session_state.user_answers = []
10
+ if "completed" not in st.session_state:
11
+ st.session_state.completed = False
12
+ if "score" not in st.session_state:
13
+ st.session_state.score = 0
14
+
15
+ def check_answer(self):
16
+ selected_option = st.session_state[f"selected_option_{st.session_state.current_question}"]
17
+ question_data = self.quiz_data[st.session_state.current_question]
18
+
19
+ if selected_option == "---" or selected_option is None:
20
+ st.warning("Please select an option before proceeding.")
21
+ return
22
+
23
+ st.session_state.user_answers.append(selected_option)
24
+
25
+ if selected_option == question_data["correct_answer"]:
26
+ st.session_state.score += 1
27
+
28
+ if st.session_state.current_question + 1 < len(self.quiz_data):
29
+ st.session_state.current_question += 1
30
+ else:
31
+ st.session_state.completed = True
32
+
33
+ def load_quiz(self, metadata):
34
+ if metadata:
35
+ st.title(f"Subject: {metadata.get("subject", "Unknown Subject")}")
36
+ st.header(f"Topic: {metadata.get("topic", "Unknown Topic")}")
37
+ st.markdown(f"""
38
+ **Quiz Type: {metadata.get("exam_type")}**
39
+ Number of Questions: {metadata.get("num_questions")}
40
+ "Timestamp: {metadata.get("timestamp")}
41
+ """)
42
+ else:
43
+ st.warning("No metadata was found.")
44
+
45
+ def start_quiz(self):
46
+ self.quiz_data = st.session_state.quiz_data
47
+
48
+ if not st.session_state.completed:
49
+ question_data = self.quiz_data[st.session_state.current_question]
50
+ st.write(f"**Question {st.session_state.current_question + 1}:** {question_data['question']}")
51
+
52
+ st.selectbox(
53
+ "Select an option:",
54
+ ["---"] + question_data["options"],
55
+ key=f"selected_option_{st.session_state.current_question}"
56
+ )
57
+
58
+ st.button("Next", on_click=self.check_answer)
59
+ else:
60
+ st.success(f"Quiz completed! Your final score is {st.session_state.score}/{len(self.quiz_data)}.")
61
+
62
+ st.write("### Quiz Summary")
63
+ for i, question_data in enumerate(self.quiz_data):
64
+ st.write(f"**Question {i + 1}:** {question_data['question']}")
65
+ st.write(f"- **Options:** {', '.join(question_data['options'])}")
66
+ st.write(f"- **Correct Answer:** {question_data['correct_answer']}")
67
+ st.write(f"- **Your Answer:** {st.session_state.user_answers[i]}")
68
+ if st.session_state.user_answers[i] == question_data["correct_answer"]:
69
+ st.write("- **Result:** ✅ Correct")
70
+ else:
71
+ st.write("- **Result:** ❌ Incorrect")