Sina Media Lab commited on
Commit
97a8822
Β·
1 Parent(s): 9124e0f
Files changed (1) hide show
  1. app.py +51 -44
app.py CHANGED
@@ -25,6 +25,14 @@ if 'question_queue' not in st.session_state:
25
  st.session_state.question_queue = []
26
  if 'submitted' not in st.session_state:
27
  st.session_state.submitted = False
 
 
 
 
 
 
 
 
28
  if 'selected_answer' not in st.session_state:
29
  st.session_state.selected_answer = None
30
 
@@ -37,10 +45,15 @@ def generate_pdf_report():
37
  pdf.ln(10)
38
 
39
  for idx, entry in enumerate(st.session_state.question_queue):
40
- question, selected, correct, explanation = entry
41
  pdf.multi_cell(0, 10, f"Q{idx+1}: {question}")
42
- pdf.multi_cell(0, 10, f"Selected Answer: {selected}")
43
- pdf.multi_cell(0, 10, f"Correct Answer: {correct}")
 
 
 
 
 
44
  pdf.multi_cell(0, 10, f"Explanation: {explanation}")
45
  pdf.ln(10)
46
 
@@ -62,60 +75,54 @@ if module_name:
62
  st.title(title)
63
  st.write(description)
64
 
65
- if st.session_state.submitted:
 
66
  st.session_state.submitted = False
67
- st.session_state.question_count += 1
68
- st.session_state.question_queue.append((
69
- st.session_state.current_question,
70
- st.session_state.selected_answer,
71
- st.session_state.correct_answer,
72
- st.session_state.explanation
73
- ))
74
-
75
- if st.session_state.selected_answer == st.session_state.correct_answer:
76
- st.session_state.correct_count += 1
77
- st.success("βœ… Correct!")
78
- else:
79
- st.error("❌ Incorrect.")
80
- st.write(f"**Explanation:** {st.session_state.explanation}")
81
 
82
  if st.session_state.question_count > 0:
83
  correct_percentage = (st.session_state.correct_count / st.session_state.question_count) * 100
84
  st.write(f"**Correct Answers:** {st.session_state.correct_count}/{st.session_state.question_count} ({correct_percentage:.2f}%)")
85
 
86
- if not st.session_state.submitted:
87
- st.session_state.current_question, options, st.session_state.correct_answer, st.session_state.explanation = generate_question()
88
- st.write(f"**Question {st.session_state.question_count + 1}:** {st.session_state.current_question}")
89
- st.session_state.selected_answer = st.radio("Choose an answer:", options)
90
 
91
- col1, col2 = st.columns([1, 5])
 
92
  with col1:
93
  if st.button("⬅️ Prev"):
94
  # Logic for previous question (if applicable)
95
  pass
96
- with col2:
97
- if st.button("Submit"):
98
- st.session_state.submitted = True
99
- st.experimental_rerun()
 
 
 
 
 
 
 
100
 
101
- if st.session_state.submitted:
102
- col1, col2, col3 = st.columns([1, 1, 3])
103
- with col1:
104
- if st.button("⬅️ Prev"):
105
- # Logic for previous question (if applicable)
106
- pass
107
- with col2:
108
- if st.button("➑️ Next"):
109
- st.experimental_rerun()
 
110
  with col3:
111
- if st.session_state.question_count > 0:
112
- pdf = generate_pdf_report()
113
- st.download_button(
114
- label="Download PDF Report πŸ“„",
115
- data=pdf,
116
- file_name="quiz_report.pdf",
117
- mime="application/pdf"
118
- )
119
 
120
  except ModuleNotFoundError:
121
  st.error(f"The module '{module_name}' was not found. Please select another module.")
 
25
  st.session_state.question_queue = []
26
  if 'submitted' not in st.session_state:
27
  st.session_state.submitted = False
28
+ if 'current_question' not in st.session_state:
29
+ st.session_state.current_question = None
30
+ if 'options' not in st.session_state:
31
+ st.session_state.options = []
32
+ if 'correct_answer' not in st.session_state:
33
+ st.session_state.correct_answer = None
34
+ if 'explanation' not in st.session_state:
35
+ st.session_state.explanation = None
36
  if 'selected_answer' not in st.session_state:
37
  st.session_state.selected_answer = None
38
 
 
45
  pdf.ln(10)
46
 
47
  for idx, entry in enumerate(st.session_state.question_queue):
48
+ question, options, selected, correct, explanation = entry
49
  pdf.multi_cell(0, 10, f"Q{idx+1}: {question}")
50
+ for option in options:
51
+ if option == correct:
52
+ pdf.multi_cell(0, 10, f"βœ… {option} (Correct)")
53
+ elif option == selected:
54
+ pdf.multi_cell(0, 10, f"❌ {option} (Your Choice)")
55
+ else:
56
+ pdf.multi_cell(0, 10, f" {option}")
57
  pdf.multi_cell(0, 10, f"Explanation: {explanation}")
58
  pdf.ln(10)
59
 
 
75
  st.title(title)
76
  st.write(description)
77
 
78
+ if st.session_state.current_question is None or st.session_state.submitted:
79
+ st.session_state.current_question, st.session_state.options, st.session_state.correct_answer, st.session_state.explanation = generate_question()
80
  st.session_state.submitted = False
81
+ st.session_state.selected_answer = None
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
  if st.session_state.question_count > 0:
84
  correct_percentage = (st.session_state.correct_count / st.session_state.question_count) * 100
85
  st.write(f"**Correct Answers:** {st.session_state.correct_count}/{st.session_state.question_count} ({correct_percentage:.2f}%)")
86
 
87
+ st.write(f"**Question {st.session_state.question_count + 1}:** {st.session_state.current_question}")
88
+ st.session_state.selected_answer = st.radio("Choose an answer:", st.session_state.options, index=0)
 
 
89
 
90
+ col1, col2, col3 = st.columns([1, 1, 3])
91
+ if st.session_state.question_count > 0:
92
  with col1:
93
  if st.button("⬅️ Prev"):
94
  # Logic for previous question (if applicable)
95
  pass
96
+ with col2:
97
+ if st.button("Submit"):
98
+ st.session_state.submitted = True
99
+ st.session_state.question_count += 1
100
+ st.session_state.question_queue.append((
101
+ st.session_state.current_question,
102
+ st.session_state.options,
103
+ st.session_state.selected_answer,
104
+ st.session_state.correct_answer,
105
+ st.session_state.explanation
106
+ ))
107
 
108
+ if st.session_state.selected_answer == st.session_state.correct_answer:
109
+ st.session_state.correct_count += 1
110
+ st.success("βœ… Correct!")
111
+ else:
112
+ st.error("❌ Incorrect.")
113
+ st.write(f"**Explanation:** {st.session_state.explanation}")
114
+ st.session_state.current_question = None
115
+ st.experimental_rerun()
116
+
117
+ if st.session_state.submitted and st.session_state.question_count > 0:
118
  with col3:
119
+ pdf = generate_pdf_report()
120
+ st.download_button(
121
+ label="Download PDF Report πŸ“„",
122
+ data=pdf,
123
+ file_name="quiz_report.pdf",
124
+ mime="application/pdf"
125
+ )
 
126
 
127
  except ModuleNotFoundError:
128
  st.error(f"The module '{module_name}' was not found. Please select another module.")