Sina Media Lab commited on
Commit
12e8aed
·
1 Parent(s): fcee975
Files changed (1) hide show
  1. app.py +77 -98
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: 5px; margin-top: 20px;" title="{category_description} > {selected_module_data['description']}">
195
- <span style='font-size: 18px; color: white;'>{selected_category} > {selected_module_data['title']}</span>
 
196
  </div>
197
- """,
198
- unsafe_allow_html=True
199
  )
200
-
201
  with col2:
202
- # Add some spacing above the button by placing it inside an empty container
203
- st.markdown("<div style='margin-top: 25px;'></div>", unsafe_allow_html=True) # Increased margin by 5px
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="Quiz Report",
209
  data=st.session_state.pdf_data,
210
- file_name="quiz_report.pdf",
211
- mime="application/pdf",
212
- disabled=False,
213
- key="pdf_download_button"
214
  )
215
 
216
- if selected_module != st.session_state.current_module:
217
- st.session_state.current_module = selected_module
218
- st.session_state.current_index = len(st.session_state.questions) # Continue numbering from previous questions
219
- st.session_state.selected_answer = None
220
- st.session_state.button_label = "Submit/New"
221
- st.session_state.start_time = time.time() # Start the timer for the new question
222
- # Initialize question count and correct count if not already done
223
- if selected_module not in st.session_state.module_question_count:
224
- st.session_state.module_question_count[selected_module] = 0
225
- if selected_module not in st.session_state.module_correct_count:
226
- st.session_state.module_correct_count[selected_module] = 0
227
- # Generate a new question without adding it to the answered list yet
228
- st.session_state.current_question = generate_new_question(selected_category, selected_module, selected_module_data)
229
-
230
- current_question = st.session_state.current_question
231
-
232
- # Display the current question inside the options box
233
- with st.form(key=f'question_form_{st.session_state.current_index}'):
234
- options = ['a', 'b', 'c', 'd']
235
- st.markdown(f"<b>Q{st.session_state.current_index + 1}: {current_question['question']}</b>", unsafe_allow_html=True)
236
- selected_answer = st.radio(
237
- "", # Empty label to put the question inside the options box
238
- options=[f"{options[i]}. {opt}" for i, opt in enumerate(current_question['options'])],
239
- key=f"question_{st.session_state.current_index}_options",
240
- index=None,
241
- )
242
-
243
- submit_button = st.form_submit_button(label="Submit/New")
244
-
245
- # Handle button state and answer submission
246
- if submit_button:
247
- if selected_answer is None:
248
- st.warning("Please select an option before submitting.", icon="⚠️")
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
- # Show correct/incorrect feedback, explanation, and step-by-step solution
264
- col1, col2 = st.columns(2)
265
- with col1:
266
- for i, option in enumerate(current_question['options']):
 
 
267
  option_text = f"{options[i]}. {option}"
268
- if option == current_question['correct_answer']:
269
- st.markdown(f"<span style='color:green;'>{option_text} ✅</span>", unsafe_allow_html=True)
270
- elif option == selected_answer_text:
271
- st.markdown(f"<span style='color:red;'>{option_text} ❌</span>", unsafe_allow_html=True)
 
 
 
272
  else:
273
- st.markdown(f"{option_text}", unsafe_allow_html=True)
274
-
275
- with col2:
276
- st.markdown(f"<span style='font-size: 14px;'><b>Explanation:</b> {current_question['explanation']}</span>", unsafe_allow_html=True)
277
- st.markdown(f"<span style='font-size: 14px;'><b>Step-by-Step Solution:</b></span>", unsafe_allow_html=True)
278
- for step in current_question['step_by_step_solution']:
279
- st.markdown(f"<span style='font-size: 14px;'>{step}</span>", unsafe_allow_html=True)
280
-
281
- # Add the question to the answered list only after submission
282
- st.session_state.questions.append(current_question)
283
- st.session_state.current_index = len(st.session_state.questions)
284
-
285
- # Generate the PDF report data after each submission
286
- st.session_state.pdf_data = generate_pdf_report()
287
 
288
- # Refresh the PDF button state
289
- st.download_button(
290
- label="Quiz Report",
291
- data=st.session_state.pdf_data,
292
- file_name="quiz_report.pdf",
293
- mime="application/pdf",
294
- disabled=False,
295
- key="pdf_download_button_updated"
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
+ )