Sina Media Lab commited on
Commit
852a54f
Β·
1 Parent(s): f0f573f
Files changed (1) hide show
  1. app.py +63 -50
app.py CHANGED
@@ -35,49 +35,52 @@ def reset_pdf_cache():
35
  def generate_pdf_report():
36
  pdf = FPDF()
37
  pdf.add_page()
38
- pdf.set_font("Arial", size=12)
39
 
40
- pdf.cell(200, 10, txt="Quiz Report", ln=True, align="C")
 
 
 
 
 
41
  pdf.ln(10)
42
 
43
- for module, data in st.session_state.module_question_count.items():
44
- correct_count = st.session_state.module_correct_count.get(module, 0)
45
- total_count = data
46
- pdf.cell(200, 10, txt=f"Module: {module}", ln=True, align="L")
47
- pdf.ln(5)
48
- pdf.cell(200, 10, txt=f"Correct Answers: {correct_count}/{total_count}", ln=True, align="L")
49
- pdf.ln(5)
50
-
51
- for entry in st.session_state.questions:
52
- if entry['module'] == module:
53
- question, options, selected, correct, explanation, step_by_step_solution = (
54
- entry['question'],
55
- entry['options'],
56
- entry['selected'] if entry['selected'] is not None else "Not Answered",
57
- entry['correct_answer'],
58
- entry['explanation'],
59
- entry['step_by_step_solution']
60
- )
61
- pdf.multi_cell(0, 10, f"Q: {question}")
62
- for option in options:
63
- if option == correct:
64
- pdf.set_text_color(0, 128, 0) # Green for correct
65
- pdf.multi_cell(0, 10, f"{option}")
66
- elif option == selected:
67
- pdf.set_text_color(255, 0, 0) # Red for incorrect
68
- pdf.multi_cell(0, 10, f"{option}")
69
- else:
70
- pdf.set_text_color(0, 0, 0) # Default color for others
71
- pdf.multi_cell(0, 10, f" {option}")
72
- pdf.set_text_color(0, 0, 0) # Reset color
73
- pdf.multi_cell(0, 10, f"Explanation: {explanation}")
74
- pdf.ln(5)
75
- pdf.multi_cell(0, 10, "Step-by-Step Solution:")
76
- for step in step_by_step_solution:
77
- pdf.multi_cell(0, 10, step)
78
- pdf.ln(10)
79
-
80
- pdf.ln(10) # Add space after each module
81
 
82
  return pdf.output(dest='S').encode('latin1', 'replace')
83
 
@@ -129,7 +132,7 @@ modules = load_modules()
129
  st.sidebar.markdown(
130
  """
131
  <div style='background-color: white; padding: 10px; border-radius: 10px; text-align: center; color: black;'>
132
- <h1 style='margin: 0;'>Magic Math Quiz!</h1>
133
  <a href="https://ghassem.com" target="_blank">
134
  <img src="https://i.imgur.com/dF3KWgY.png" alt="Logo" style="width:40%; border-radius: 5px;">
135
  </a>
@@ -148,7 +151,7 @@ if selected_category:
148
  selected_module_data = module_data
149
  break
150
 
151
- # Display black background title with PDF report button, but only show the button when at least one question is answered
152
  st.markdown(
153
  f"""
154
  <div style="background-color: #333; padding: 10px; border-radius: 5px; margin-top: 20px;">
@@ -158,10 +161,15 @@ if selected_category:
158
  unsafe_allow_html=True
159
  )
160
 
161
- if len(st.session_state.questions) > 0:
162
- if st.button("πŸ“„ Download PDF Report"):
163
- pdf_data = generate_pdf_report()
164
- st.download_button(label="Click to download", data=pdf_data, file_name="quiz_report.pdf", mime="application/pdf")
 
 
 
 
 
165
 
166
  if selected_module != st.session_state.current_module:
167
  st.session_state.current_module = selected_module
@@ -234,9 +242,14 @@ if submit_button:
234
  st.session_state.questions.append(current_question)
235
  st.session_state.current_index = len(st.session_state.questions)
236
 
237
- # Generate a new question after submission
238
- st.session_state.current_question = generate_new_question(selected_category, selected_module, selected_module_data)
239
- st.session_state.start_time = time.time() # Reset the timer for the new question
240
-
241
  # Generate the PDF report data after each submission
242
  st.session_state.pdf_data = generate_pdf_report()
 
 
 
 
 
 
 
 
 
 
35
  def generate_pdf_report():
36
  pdf = FPDF()
37
  pdf.add_page()
38
+ pdf.set_font("Arial", style='B', size=12)
39
 
40
+ # Header for PDF report
41
+ pdf.set_fill_color(211, 211, 211) # Light gray
42
+ pdf.cell(0, 10, txt="πŸͺ„ Magic Math Quiz!", ln=True, align="C", fill=True)
43
+ pdf.ln(5)
44
+ pdf.set_font("Arial", size=8)
45
+ pdf.cell(0, 10, txt="by Ghassem Tofighi", ln=True, align="C", link="https://ghassem.com")
46
  pdf.ln(10)
47
 
48
+ for i, entry in enumerate(st.session_state.questions):
49
+ # Zebra background
50
+ if i % 2 == 0:
51
+ pdf.set_fill_color(245, 245, 245) # Light gray
52
+ else:
53
+ pdf.set_fill_color(255, 255, 255) # White
54
+
55
+ # Module Title
56
+ pdf.set_font("Arial", style='B', size=10)
57
+ pdf.multi_cell(0, 10, f"Module: {entry['module_title']}", border=1, fill=True)
58
+
59
+ # Question
60
+ pdf.set_font("Arial", style='B', size=10)
61
+ pdf.multi_cell(0, 10, f"Q{i+1}: {entry['question']}", border=1, fill=True)
62
+
63
+ # Time Taken
64
+ pdf.set_font("Arial", size=10)
65
+ pdf.multi_cell(0, 10, f"Time Taken: {entry['time_taken']} seconds", border=1, fill=True)
66
+
67
+ # Options
68
+ pdf.set_font("Arial", size=10)
69
+ options = ['a', 'b', 'c', 'd']
70
+ for j, option in enumerate(entry['options']):
71
+ if option == entry['correct_answer']:
72
+ pdf.set_text_color(0, 128, 0) # Green for correct
73
+ elif option == entry['selected']:
74
+ pdf.set_text_color(255, 0, 0) # Red for incorrect
75
+ else:
76
+ pdf.set_text_color(0, 0, 0) # Default color for others
77
+
78
+ pdf.multi_cell(0, 10, f"{options[j]}. {option}", border=1, fill=True)
79
+
80
+ pdf.set_text_color(0, 0, 0) # Reset color
81
+ pdf.multi_cell(0, 10, f"Explanation: {entry['explanation']}", border=1, fill=True)
82
+ pdf.multi_cell(0, 10, f"Step-by-Step Solution: {', '.join(entry['step_by_step_solution'])}", border=1, fill=True)
83
+ pdf.ln(10)
 
 
84
 
85
  return pdf.output(dest='S').encode('latin1', 'replace')
86
 
 
132
  st.sidebar.markdown(
133
  """
134
  <div style='background-color: white; padding: 10px; border-radius: 10px; text-align: center; color: black;'>
135
+ <h1 style='margin: 0;'>πŸͺ„ Magic Math Quiz!</h1>
136
  <a href="https://ghassem.com" target="_blank">
137
  <img src="https://i.imgur.com/dF3KWgY.png" alt="Logo" style="width:40%; border-radius: 5px;">
138
  </a>
 
151
  selected_module_data = module_data
152
  break
153
 
154
+ # Display black background title with PDF report button
155
  st.markdown(
156
  f"""
157
  <div style="background-color: #333; padding: 10px; border-radius: 5px; margin-top: 20px;">
 
161
  unsafe_allow_html=True
162
  )
163
 
164
+ # Show PDF report button, initially disabled until a question is answered
165
+ pdf_disabled = len(st.session_state.questions) == 0
166
+ pdf_button = st.download_button(
167
+ label="πŸ“„ Download PDF Report",
168
+ data=st.session_state.pdf_data if not pdf_disabled else b'',
169
+ file_name="quiz_report.pdf",
170
+ mime="application/pdf",
171
+ disabled=pdf_disabled
172
+ )
173
 
174
  if selected_module != st.session_state.current_module:
175
  st.session_state.current_module = selected_module
 
242
  st.session_state.questions.append(current_question)
243
  st.session_state.current_index = len(st.session_state.questions)
244
 
 
 
 
 
245
  # Generate the PDF report data after each submission
246
  st.session_state.pdf_data = generate_pdf_report()
247
+
248
+ # Refresh the PDF button state
249
+ pdf_button = st.download_button(
250
+ label="πŸ“„ Download PDF Report",
251
+ data=st.session_state.pdf_data,
252
+ file_name="quiz_report.pdf",
253
+ mime="application/pdf",
254
+ disabled=False
255
+ )