Sina Media Lab
commited on
Commit
·
12e8aed
1
Parent(s):
fcee975
Updates
Browse files
app.py
CHANGED
@@ -171,14 +171,13 @@ st.sidebar.title("Quiz Categories")
|
|
171 |
selected_category = st.sidebar.selectbox("Choose a category:", list(modules.keys()))
|
172 |
|
173 |
# Get category description for tooltip
|
174 |
-
category_description = modules[selected_category]["description"] if selected_category else ""
|
175 |
|
176 |
if selected_category:
|
177 |
module_options = [modules[selected_category][module]["title"] for module in modules[selected_category]]
|
178 |
selected_module = st.sidebar.radio(
|
179 |
"Choose a module:",
|
180 |
-
module_options
|
181 |
-
help=modules[selected_category]["modules"][list(modules[selected_category]["modules"].keys())[module_options.index(selected_module)]]["description"]
|
182 |
)
|
183 |
|
184 |
for module_name, module_data in modules[selected_category].items():
|
@@ -191,109 +190,89 @@ if selected_category:
|
|
191 |
with col1:
|
192 |
st.markdown(
|
193 |
f"""
|
194 |
-
<div style="background-color: #333; padding: 10px; border-radius:
|
195 |
-
|
|
|
196 |
</div>
|
197 |
-
""",
|
198 |
-
unsafe_allow_html=True
|
199 |
)
|
200 |
-
|
201 |
with col2:
|
202 |
-
|
203 |
-
|
204 |
-
# Show PDF report button, initially disabled until a question is answered
|
205 |
-
pdf_disabled = len(st.session_state.questions) == 0
|
206 |
-
if st.session_state.pdf_data:
|
207 |
st.download_button(
|
208 |
-
label="
|
209 |
data=st.session_state.pdf_data,
|
210 |
-
file_name="
|
211 |
-
mime="application/pdf"
|
212 |
-
disabled=False,
|
213 |
-
key="pdf_download_button"
|
214 |
)
|
215 |
|
216 |
-
if
|
217 |
-
st.session_state.
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
#
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
else:
|
250 |
-
# Calculate time taken to answer the question
|
251 |
-
current_question['time_taken'] = int(time.time() - st.session_state.start_time)
|
252 |
-
|
253 |
-
# Process the answer
|
254 |
-
selected_answer_text = selected_answer.split(". ", 1)[1] # Extract the text part
|
255 |
-
current_question['selected'] = selected_answer_text
|
256 |
-
current_question['answered'] = True
|
257 |
-
st.session_state.module_question_count[selected_module] += 1
|
258 |
-
|
259 |
-
if selected_answer_text == current_question['correct_answer']:
|
260 |
-
st.session_state.correct_count += 1
|
261 |
-
st.session_state.module_correct_count[selected_module] += 1
|
262 |
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
|
|
|
|
267 |
option_text = f"{options[i]}. {option}"
|
268 |
-
if
|
269 |
-
|
270 |
-
|
271 |
-
|
|
|
|
|
|
|
272 |
else:
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
# Generate the PDF report data after each submission
|
286 |
-
st.session_state.pdf_data = generate_pdf_report()
|
287 |
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
)
|
297 |
-
|
298 |
-
# Generate a new question for the next round
|
299 |
-
st.session_state.current_question = generate_new_question(selected_category, selected_module, selected_module_data)
|
|
|
171 |
selected_category = st.sidebar.selectbox("Choose a category:", list(modules.keys()))
|
172 |
|
173 |
# Get category description for tooltip
|
174 |
+
category_description = modules[selected_category]["description"] if selected_category and selected_category in modules else "No description available."
|
175 |
|
176 |
if selected_category:
|
177 |
module_options = [modules[selected_category][module]["title"] for module in modules[selected_category]]
|
178 |
selected_module = st.sidebar.radio(
|
179 |
"Choose a module:",
|
180 |
+
module_options
|
|
|
181 |
)
|
182 |
|
183 |
for module_name, module_data in modules[selected_category].items():
|
|
|
190 |
with col1:
|
191 |
st.markdown(
|
192 |
f"""
|
193 |
+
<div style="background-color: #333; color: white; padding: 10px; border-radius: 10px;">
|
194 |
+
<h2 style="margin: 0;">{selected_category} > {selected_module}</h2>
|
195 |
+
<p>{selected_module_data['description']}</p>
|
196 |
</div>
|
197 |
+
""", unsafe_allow_html=True
|
|
|
198 |
)
|
|
|
199 |
with col2:
|
200 |
+
if st.button("Download PDF Report", key="download_pdf"):
|
201 |
+
st.session_state.pdf_data = generate_pdf_report()
|
|
|
|
|
|
|
202 |
st.download_button(
|
203 |
+
label="Download PDF Report",
|
204 |
data=st.session_state.pdf_data,
|
205 |
+
file_name=f"quiz_report_{st.session_state.session_id}.pdf",
|
206 |
+
mime="application/pdf"
|
|
|
|
|
207 |
)
|
208 |
|
209 |
+
if selected_module_data:
|
210 |
+
if st.button(st.session_state.button_label):
|
211 |
+
if st.session_state.button_label == "Submit":
|
212 |
+
if st.session_state.selected_answer is not None:
|
213 |
+
question = st.session_state.questions[st.session_state.current_index]
|
214 |
+
question['answered'] = True
|
215 |
+
if st.session_state.selected_answer == question['correct_answer']:
|
216 |
+
st.session_state.correct_count += 1
|
217 |
+
st.session_state.module_correct_count[selected_module] = st.session_state.module_correct_count.get(selected_module, 0) + 1
|
218 |
+
else:
|
219 |
+
st.session_state.module_correct_count[selected_module] = st.session_state.module_correct_count.get(selected_module, 0)
|
220 |
+
|
221 |
+
st.session_state.questions[st.session_state.current_index] = question
|
222 |
+
st.session_state.current_index += 1
|
223 |
+
st.session_state.selected_answer = None
|
224 |
+
st.session_state.button_label = "Next Question"
|
225 |
+
st.session_state.pdf_data = None # Reset PDF cache when moving to the next question
|
226 |
+
else:
|
227 |
+
st.warning("Please select an answer before submitting.")
|
228 |
+
elif st.session_state.button_label == "Next Question":
|
229 |
+
if st.session_state.current_index < len(st.session_state.questions):
|
230 |
+
question = st.session_state.questions[st.session_state.current_index]
|
231 |
+
st.session_state.selected_answer = None
|
232 |
+
st.session_state.button_label = "Submit"
|
233 |
+
else:
|
234 |
+
st.session_state.button_label = "Submit/New"
|
235 |
+
st.session_state.current_index = 0
|
236 |
+
st.session_state.questions = []
|
237 |
+
st.session_state.correct_count = 0
|
238 |
+
st.session_state.module_correct_count = {}
|
239 |
+
st.session_state.pdf_data = generate_pdf_report()
|
240 |
+
|
241 |
+
st.experimental_rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
242 |
|
243 |
+
if st.session_state.questions:
|
244 |
+
question = st.session_state.questions[st.session_state.current_index]
|
245 |
+
st.write(f"**Q{st.session_state.current_index + 1}:** {question['question']}")
|
246 |
+
options = ['a', 'b', 'c', 'd']
|
247 |
+
for i, option in enumerate(question['options']):
|
248 |
+
is_selected = st.session_state.selected_answer == options[i]
|
249 |
option_text = f"{options[i]}. {option}"
|
250 |
+
if question['answered']:
|
251 |
+
if options[i] == question['correct_answer']:
|
252 |
+
st.markdown(f"<p style='color: green;'>{option_text}</p>", unsafe_allow_html=True)
|
253 |
+
elif is_selected:
|
254 |
+
st.markdown(f"<p style='color: red;'>{option_text}</p>", unsafe_allow_html=True)
|
255 |
+
else:
|
256 |
+
st.markdown(f"<p>{option_text}</p>", unsafe_allow_html=True)
|
257 |
else:
|
258 |
+
if is_selected:
|
259 |
+
st.markdown(f"<p style='color: blue;'>{option_text}</p>", unsafe_allow_html=True)
|
260 |
+
else:
|
261 |
+
st.markdown(f"<p>{option_text}</p>", unsafe_allow_html=True)
|
262 |
+
|
263 |
+
if not question['answered']:
|
264 |
+
if st.radio("Options", options, key=question['question'], index=options.index(st.session_state.selected_answer) if st.session_state.selected_answer else -1) == options[i]:
|
265 |
+
st.session_state.selected_answer = options[i]
|
266 |
+
else:
|
267 |
+
st.write("No questions available.")
|
268 |
+
else:
|
269 |
+
st.write("Please select a category and module.")
|
|
|
|
|
270 |
|
271 |
+
# Footer
|
272 |
+
st.markdown(
|
273 |
+
"""
|
274 |
+
<div style='text-align: center; color: grey; font-size: 12px;'>
|
275 |
+
<p>By Ghassem Tofighi</p>
|
276 |
+
</div>
|
277 |
+
""", unsafe_allow_html=True
|
278 |
+
)
|
|
|
|
|
|
|
|