Sina Media Lab commited on
Commit Β·
852a54f
1
Parent(s): f0f573f
Updates
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
pdf.ln(10)
|
| 42 |
|
| 43 |
-
for
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 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
|
| 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 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
)
|