Sina Media Lab commited on
Commit
169cec0
·
1 Parent(s): 27d1943
Files changed (2) hide show
  1. app.py +75 -51
  2. modules/valid_invalid_numbers.py +1 -1
app.py CHANGED
@@ -23,6 +23,10 @@ if 'correct_count' not in st.session_state:
23
  st.session_state.correct_count = 0
24
  if 'question_count' not in st.session_state:
25
  st.session_state.question_count = 0
 
 
 
 
26
  if 'current_module' not in st.session_state:
27
  st.session_state.current_module = None
28
  if 'question_queue' not in st.session_state:
@@ -44,22 +48,39 @@ def generate_pdf_report():
44
  pdf = FPDF()
45
  pdf.add_page()
46
  pdf.set_font("Arial", size=12)
47
-
48
  pdf.cell(200, 10, txt="Quiz Report", ln=True, align="C")
49
  pdf.ln(10)
50
 
51
- for idx, entry in enumerate(st.session_state.question_queue):
52
- question, options, selected, correct, explanation = entry
53
- pdf.multi_cell(0, 10, f"Q{idx+1}: {question}")
54
- for option in options:
55
- if option == correct:
56
- pdf.multi_cell(0, 10, f"Correct: {option}")
57
- elif option == selected:
58
- pdf.multi_cell(0, 10, f"Your Choice: {option}")
59
- else:
60
- pdf.multi_cell(0, 10, f" {option}")
61
- pdf.multi_cell(0, 10, f"Explanation: {explanation}")
62
- pdf.ln(10)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  return pdf.output(dest='S').encode('latin1', 'replace')
65
 
@@ -110,26 +131,59 @@ if module_name:
110
  correct_percentage = (st.session_state.correct_count / st.session_state.question_count) * 100
111
  st.write(f"**Correct Answers:** {st.session_state.correct_count}/{st.session_state.question_count} ({correct_percentage:.2f}%)")
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  st.write(f"**Question {st.session_state.question_count + 1}:** {st.session_state.current_question}")
114
  st.session_state.selected_answer = st.radio("Choose an answer:", st.session_state.options, key=st.session_state.question_count)
115
 
116
  if st.button("Submit"):
117
  st.session_state.submitted = True
118
  st.session_state.question_count += 1
119
- st.session_state.question_queue.append((
120
- st.session_state.current_question,
121
- st.session_state.options,
122
- st.session_state.selected_answer,
123
- st.session_state.correct_answer,
124
- st.session_state.explanation
125
- ))
126
-
127
  if st.session_state.selected_answer == st.session_state.correct_answer:
128
  st.session_state.correct_count += 1
 
129
  st.success("Correct!")
130
  else:
131
  st.error("Incorrect.")
132
 
 
 
 
 
 
 
 
 
 
133
  logging.info("Answer submitted.")
134
  logging.info(f"Selected Answer: {st.session_state.selected_answer}")
135
  logging.info(f"Correct Answer: {st.session_state.correct_answer}")
@@ -144,36 +198,6 @@ if module_name:
144
  unsafe_allow_html=True
145
  )
146
 
147
- # Display the "Next", "Prev", and "Download PDF Report" buttons above the question
148
- col1, col2, col3 = st.columns([1, 1, 2])
149
- with col1:
150
- if st.session_state.submitted:
151
- if st.button("➡️ Next"):
152
- st.session_state.current_question = None
153
- logging.info("Navigated to next question.")
154
- with col2:
155
- if st.session_state.question_count > 1:
156
- if st.button("⬅️ Prev"):
157
- st.session_state.question_count -= 1
158
- st.session_state.submitted = False
159
- question_data = st.session_state.question_queue[st.session_state.question_count - 1]
160
- st.session_state.current_question = question_data[0]
161
- st.session_state.options = question_data[1]
162
- st.session_state.selected_answer = question_data[2]
163
- st.session_state.correct_answer = question_data[3]
164
- st.session_state.explanation = question_data[4]
165
- logging.info("Navigated to previous question.")
166
- with col3:
167
- if st.session_state.question_count > 0:
168
- pdf = generate_pdf_report()
169
- st.download_button(
170
- label="Download PDF Report 📄",
171
- data=pdf,
172
- file_name="quiz_report.pdf",
173
- mime="application/pdf"
174
- )
175
- logging.info("PDF report generated.")
176
-
177
  except ModuleNotFoundError as e:
178
  st.error(f"The module '{module_name}' was not found. Please select another module.")
179
  logging.error(f"Error: {e}")
 
23
  st.session_state.correct_count = 0
24
  if 'question_count' not in st.session_state:
25
  st.session_state.question_count = 0
26
+ if 'module_correct_count' not in st.session_state:
27
+ st.session_state.module_correct_count = {name: 0 for name in module_names}
28
+ if 'module_question_count' not in st.session_state:
29
+ st.session_state.module_question_count = {name: 0 for name in module_names}
30
  if 'current_module' not in st.session_state:
31
  st.session_state.current_module = None
32
  if 'question_queue' not in st.session_state:
 
48
  pdf = FPDF()
49
  pdf.add_page()
50
  pdf.set_font("Arial", size=12)
51
+
52
  pdf.cell(200, 10, txt="Quiz Report", ln=True, align="C")
53
  pdf.ln(10)
54
 
55
+ for module in module_names.keys():
56
+ pdf.cell(200, 10, txt=f"Module: {module}", ln=True, align="L")
57
+ pdf.ln(5)
58
+ correct_count = st.session_state.module_correct_count[module]
59
+ total_count = st.session_state.module_question_count[module]
60
+ pdf.cell(200, 10, txt=f"Correct Answers: {correct_count}/{total_count}", ln=True, align="L")
61
+ pdf.ln(5)
62
+
63
+ for entry in st.session_state.question_queue:
64
+ if entry['module'] == module:
65
+ question, options, selected, correct, explanation = (
66
+ entry['question'],
67
+ entry['options'],
68
+ entry['selected'],
69
+ entry['correct'],
70
+ entry['explanation']
71
+ )
72
+ pdf.multi_cell(0, 10, f"Q: {question}")
73
+ for option in options:
74
+ if option == correct:
75
+ pdf.multi_cell(0, 10, f"Correct: {option}")
76
+ elif option == selected:
77
+ pdf.multi_cell(0, 10, f"Your Choice: {option}")
78
+ else:
79
+ pdf.multi_cell(0, 10, f" {option}")
80
+ pdf.multi_cell(0, 10, f"Explanation: {explanation}")
81
+ pdf.ln(10)
82
+
83
+ pdf.ln(10) # Add space after each module
84
 
85
  return pdf.output(dest='S').encode('latin1', 'replace')
86
 
 
131
  correct_percentage = (st.session_state.correct_count / st.session_state.question_count) * 100
132
  st.write(f"**Correct Answers:** {st.session_state.correct_count}/{st.session_state.question_count} ({correct_percentage:.2f}%)")
133
 
134
+ # Button Row: Prev, Next, and PDF Download
135
+ col1, col2, col3 = st.columns([1, 1, 2])
136
+ with col1:
137
+ if st.session_state.question_count > 1:
138
+ if st.button("⬅️ Prev"):
139
+ st.session_state.question_count -= 1
140
+ st.session_state.submitted = False
141
+ question_data = st.session_state.question_queue[st.session_state.question_count - 1]
142
+ st.session_state.current_question = question_data['question']
143
+ st.session_state.options = question_data['options']
144
+ st.session_state.selected_answer = question_data['selected']
145
+ st.session_state.correct_answer = question_data['correct']
146
+ st.session_state.explanation = question_data['explanation']
147
+ logging.info("Navigated to previous question.")
148
+ with col2:
149
+ if st.session_state.submitted:
150
+ if st.button("➡️ Next"):
151
+ st.session_state.current_question = None
152
+ logging.info("Navigated to next question.")
153
+ with col3:
154
+ if st.session_state.question_count > 0:
155
+ pdf = generate_pdf_report()
156
+ st.download_button(
157
+ label="Download PDF Report 📄",
158
+ data=pdf,
159
+ file_name="quiz_report.pdf",
160
+ mime="application/pdf"
161
+ )
162
+ logging.info("PDF report generated.")
163
+
164
  st.write(f"**Question {st.session_state.question_count + 1}:** {st.session_state.current_question}")
165
  st.session_state.selected_answer = st.radio("Choose an answer:", st.session_state.options, key=st.session_state.question_count)
166
 
167
  if st.button("Submit"):
168
  st.session_state.submitted = True
169
  st.session_state.question_count += 1
170
+ st.session_state.module_question_count[module_name] += 1
 
 
 
 
 
 
 
171
  if st.session_state.selected_answer == st.session_state.correct_answer:
172
  st.session_state.correct_count += 1
173
+ st.session_state.module_correct_count[module_name] += 1
174
  st.success("Correct!")
175
  else:
176
  st.error("Incorrect.")
177
 
178
+ st.session_state.question_queue.append({
179
+ 'module': module_name,
180
+ 'question': st.session_state.current_question,
181
+ 'options': st.session_state.options,
182
+ 'selected': st.session_state.selected_answer,
183
+ 'correct': st.session_state.correct_answer,
184
+ 'explanation': st.session_state.explanation
185
+ })
186
+
187
  logging.info("Answer submitted.")
188
  logging.info(f"Selected Answer: {st.session_state.selected_answer}")
189
  logging.info(f"Correct Answer: {st.session_state.correct_answer}")
 
198
  unsafe_allow_html=True
199
  )
200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  except ModuleNotFoundError as e:
202
  st.error(f"The module '{module_name}' was not found. Please select another module.")
203
  logging.error(f"Error: {e}")
modules/valid_invalid_numbers.py CHANGED
@@ -12,7 +12,7 @@ def generate_question():
12
 
13
  def generate_invalid_number():
14
  valid_digits = ''.join([str(random.randint(0, base - 1)) for _ in range(length - 1)])
15
- invalid_digit = str(random.randint(base, 9))
16
  return valid_digits + invalid_digit
17
 
18
  correct_answer = generate_invalid_number()
 
12
 
13
  def generate_invalid_number():
14
  valid_digits = ''.join([str(random.randint(0, base - 1)) for _ in range(length - 1)])
15
+ invalid_digit = str(random.randint(base, base + 1)) if base < 10 else chr(random.randint(ord('A'), ord('F')))
16
  return valid_digits + invalid_digit
17
 
18
  correct_answer = generate_invalid_number()