Sina Media Lab
commited on
Commit
·
a2ef5f6
1
Parent(s):
6749d37
Updates
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
import os
|
| 3 |
from fpdf import FPDF
|
| 4 |
import uuid
|
|
|
|
| 5 |
|
| 6 |
# Initialize session state variables
|
| 7 |
if 'session_id' not in st.session_state:
|
|
@@ -25,6 +26,8 @@ if 'selected_answer' not in st.session_state:
|
|
| 25 |
st.session_state.selected_answer = None
|
| 26 |
if 'button_label' not in st.session_state:
|
| 27 |
st.session_state.button_label = "Submit/New"
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def reset_pdf_cache():
|
| 30 |
st.session_state.pdf_data = None
|
|
@@ -52,6 +55,10 @@ def generate_pdf_report():
|
|
| 52 |
pdf.set_font("Arial", style='B', size=10)
|
| 53 |
pdf.multi_cell(0, 10, f"Q{i+1}: {entry['question']}", border=1, fill=True)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
# Options
|
| 56 |
pdf.set_font("Arial", size=10)
|
| 57 |
options = ['a', 'b', 'c', 'd']
|
|
@@ -94,6 +101,7 @@ def generate_new_question(module_name, module):
|
|
| 94 |
question_data['module'] = module_name # Add the module name to the question data
|
| 95 |
question_data['module_title'] = module['title'] # Add the module title to the question data
|
| 96 |
question_data['selected'] = None # Initialize 'selected' to None
|
|
|
|
| 97 |
# Ensure there are exactly 4 options
|
| 98 |
if len(question_data['options']) != 4:
|
| 99 |
st.warning(f"Question in module '{module_name}' does not have 4 options. Found {len(question_data['options'])}.")
|
|
@@ -117,6 +125,7 @@ if selected_module != st.session_state.current_module:
|
|
| 117 |
st.session_state.current_index = len(st.session_state.questions) # Continue numbering from previous questions
|
| 118 |
st.session_state.selected_answer = None
|
| 119 |
st.session_state.button_label = "Submit/New"
|
|
|
|
| 120 |
# Initialize question count and correct count if not already done
|
| 121 |
if selected_module not in st.session_state.module_question_count:
|
| 122 |
st.session_state.module_question_count[selected_module] = 0
|
|
@@ -127,9 +136,27 @@ if selected_module != st.session_state.current_module:
|
|
| 127 |
|
| 128 |
current_question = st.session_state.current_question
|
| 129 |
|
| 130 |
-
# Display
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
st.markdown(f"### {modules[selected_module]['title']}")
|
| 132 |
-
st.markdown(f"<span style='font-size:
|
| 133 |
|
| 134 |
# Display the current question with larger font
|
| 135 |
st.markdown(f"<span style='font-size: 18px;'><b>Q{st.session_state.current_index + 1}: {current_question['question']}</b></span>", unsafe_allow_html=True)
|
|
@@ -151,6 +178,9 @@ if submit_button:
|
|
| 151 |
if selected_answer is None:
|
| 152 |
st.warning("Please select an option before submitting.", icon="⚠️")
|
| 153 |
else:
|
|
|
|
|
|
|
|
|
|
| 154 |
# Process the answer
|
| 155 |
selected_answer_text = selected_answer.split(". ", 1)[1] # Extract the text part
|
| 156 |
current_question['selected'] = selected_answer_text
|
|
@@ -162,10 +192,6 @@ if submit_button:
|
|
| 162 |
st.session_state.module_correct_count[selected_module] += 1
|
| 163 |
|
| 164 |
# Show correct/incorrect feedback, explanation, and step-by-step solution
|
| 165 |
-
st.markdown(
|
| 166 |
-
"""
|
| 167 |
-
<div style='padding: 10px;'>
|
| 168 |
-
""", unsafe_allow_html=True)
|
| 169 |
for i, option in enumerate(current_question['options']):
|
| 170 |
option_text = f"{options[i]}. {option}"
|
| 171 |
if option == current_question['correct_answer']:
|
|
@@ -175,11 +201,10 @@ if submit_button:
|
|
| 175 |
else:
|
| 176 |
st.markdown(f"{option_text}", unsafe_allow_html=True)
|
| 177 |
|
| 178 |
-
st.markdown(f"<span style='font-size:
|
| 179 |
-
st.markdown(f"<span style='font-size:
|
| 180 |
for step in current_question['step_by_step_solution']:
|
| 181 |
-
st.markdown(f"<span style='font-size:
|
| 182 |
-
st.markdown("</div>", unsafe_allow_html=True)
|
| 183 |
|
| 184 |
# Add the question to the answered list only after submission
|
| 185 |
st.session_state.questions.append(current_question)
|
|
@@ -187,14 +212,4 @@ if submit_button:
|
|
| 187 |
|
| 188 |
# Generate a new question after submission
|
| 189 |
st.session_state.current_question = generate_new_question(selected_module, modules[selected_module])
|
| 190 |
-
|
| 191 |
-
# Generate PDF report only if there is at least one answered question
|
| 192 |
-
if any(q['answered'] for q in st.session_state.questions):
|
| 193 |
-
pdf = generate_pdf_report()
|
| 194 |
-
st.session_state.pdf_data = pdf # Reset PDF cache
|
| 195 |
-
st.download_button(
|
| 196 |
-
label="Download PDF Report 📄",
|
| 197 |
-
data=st.session_state.pdf_data,
|
| 198 |
-
file_name="quiz_report.pdf",
|
| 199 |
-
mime="application/pdf"
|
| 200 |
-
)
|
|
|
|
| 2 |
import os
|
| 3 |
from fpdf import FPDF
|
| 4 |
import uuid
|
| 5 |
+
import time
|
| 6 |
|
| 7 |
# Initialize session state variables
|
| 8 |
if 'session_id' not in st.session_state:
|
|
|
|
| 26 |
st.session_state.selected_answer = None
|
| 27 |
if 'button_label' not in st.session_state:
|
| 28 |
st.session_state.button_label = "Submit/New"
|
| 29 |
+
if 'start_time' not in st.session_state:
|
| 30 |
+
st.session_state.start_time = time.time()
|
| 31 |
|
| 32 |
def reset_pdf_cache():
|
| 33 |
st.session_state.pdf_data = None
|
|
|
|
| 55 |
pdf.set_font("Arial", style='B', size=10)
|
| 56 |
pdf.multi_cell(0, 10, f"Q{i+1}: {entry['question']}", border=1, fill=True)
|
| 57 |
|
| 58 |
+
# Time Taken
|
| 59 |
+
pdf.set_font("Arial", size=10)
|
| 60 |
+
pdf.multi_cell(0, 10, f"Time Taken: {entry['time_taken']} seconds", border=1, fill=True)
|
| 61 |
+
|
| 62 |
# Options
|
| 63 |
pdf.set_font("Arial", size=10)
|
| 64 |
options = ['a', 'b', 'c', 'd']
|
|
|
|
| 101 |
question_data['module'] = module_name # Add the module name to the question data
|
| 102 |
question_data['module_title'] = module['title'] # Add the module title to the question data
|
| 103 |
question_data['selected'] = None # Initialize 'selected' to None
|
| 104 |
+
question_data['time_taken'] = 0 # Initialize time taken to 0
|
| 105 |
# Ensure there are exactly 4 options
|
| 106 |
if len(question_data['options']) != 4:
|
| 107 |
st.warning(f"Question in module '{module_name}' does not have 4 options. Found {len(question_data['options'])}.")
|
|
|
|
| 125 |
st.session_state.current_index = len(st.session_state.questions) # Continue numbering from previous questions
|
| 126 |
st.session_state.selected_answer = None
|
| 127 |
st.session_state.button_label = "Submit/New"
|
| 128 |
+
st.session_state.start_time = time.time() # Start the timer for the new question
|
| 129 |
# Initialize question count and correct count if not already done
|
| 130 |
if selected_module not in st.session_state.module_question_count:
|
| 131 |
st.session_state.module_question_count[selected_module] = 0
|
|
|
|
| 136 |
|
| 137 |
current_question = st.session_state.current_question
|
| 138 |
|
| 139 |
+
# Display the PDF report button and clock on the same horizontal level
|
| 140 |
+
col1, col2 = st.columns([1, 3])
|
| 141 |
+
|
| 142 |
+
with col1:
|
| 143 |
+
st.write("⏱️")
|
| 144 |
+
st.write(f"{int(time.time() - st.session_state.start_time)} seconds")
|
| 145 |
+
with col2:
|
| 146 |
+
# Generate PDF report only if there is at least one answered question
|
| 147 |
+
if any(q['answered'] for q in st.session_state.questions):
|
| 148 |
+
pdf = generate_pdf_report()
|
| 149 |
+
st.session_state.pdf_data = pdf # Reset PDF cache
|
| 150 |
+
st.download_button(
|
| 151 |
+
label="Download PDF Report 📄",
|
| 152 |
+
data=st.session_state.pdf_data,
|
| 153 |
+
file_name="quiz_report.pdf",
|
| 154 |
+
mime="application/pdf"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
# Display module title and description with a larger font for description
|
| 158 |
st.markdown(f"### {modules[selected_module]['title']}")
|
| 159 |
+
st.markdown(f"<span style='font-size: 14px;'>{modules[selected_module]['description']}</span>", unsafe_allow_html=True)
|
| 160 |
|
| 161 |
# Display the current question with larger font
|
| 162 |
st.markdown(f"<span style='font-size: 18px;'><b>Q{st.session_state.current_index + 1}: {current_question['question']}</b></span>", unsafe_allow_html=True)
|
|
|
|
| 178 |
if selected_answer is None:
|
| 179 |
st.warning("Please select an option before submitting.", icon="⚠️")
|
| 180 |
else:
|
| 181 |
+
# Calculate time taken to answer the question
|
| 182 |
+
current_question['time_taken'] = int(time.time() - st.session_state.start_time)
|
| 183 |
+
|
| 184 |
# Process the answer
|
| 185 |
selected_answer_text = selected_answer.split(". ", 1)[1] # Extract the text part
|
| 186 |
current_question['selected'] = selected_answer_text
|
|
|
|
| 192 |
st.session_state.module_correct_count[selected_module] += 1
|
| 193 |
|
| 194 |
# Show correct/incorrect feedback, explanation, and step-by-step solution
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
for i, option in enumerate(current_question['options']):
|
| 196 |
option_text = f"{options[i]}. {option}"
|
| 197 |
if option == current_question['correct_answer']:
|
|
|
|
| 201 |
else:
|
| 202 |
st.markdown(f"{option_text}", unsafe_allow_html=True)
|
| 203 |
|
| 204 |
+
st.markdown(f"<span style='font-size: 14px;'><b>Explanation:</b> {current_question['explanation']}</span>", unsafe_allow_html=True)
|
| 205 |
+
st.markdown(f"<span style='font-size: 14px;'><b>Step-by-Step Solution:</b></span>", unsafe_allow_html=True)
|
| 206 |
for step in current_question['step_by_step_solution']:
|
| 207 |
+
st.markdown(f"<span style='font-size: 14px;'>{step}</span>", unsafe_allow_html=True)
|
|
|
|
| 208 |
|
| 209 |
# Add the question to the answered list only after submission
|
| 210 |
st.session_state.questions.append(current_question)
|
|
|
|
| 212 |
|
| 213 |
# Generate a new question after submission
|
| 214 |
st.session_state.current_question = generate_new_question(selected_module, modules[selected_module])
|
| 215 |
+
st.session_state.start_time = time.time() # Reset the timer for the new question
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|