Sina Media Lab
commited on
Commit
·
81e422f
1
Parent(s):
199933f
Updates
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import importlib
|
|
3 |
import logging
|
4 |
from fpdf import FPDF
|
5 |
import uuid
|
|
|
6 |
|
7 |
# Configure logging
|
8 |
logging.basicConfig(level=logging.INFO)
|
@@ -63,12 +64,13 @@ def generate_pdf_report():
|
|
63 |
|
64 |
for entry in st.session_state.questions:
|
65 |
if entry['module'] == module:
|
66 |
-
question, options, selected, correct, explanation = (
|
67 |
entry['question'],
|
68 |
entry['options'],
|
69 |
entry['selected'],
|
70 |
-
entry['
|
71 |
-
entry['explanation']
|
|
|
72 |
)
|
73 |
pdf.multi_cell(0, 10, f"Q: {question}")
|
74 |
for option in options:
|
@@ -83,6 +85,10 @@ def generate_pdf_report():
|
|
83 |
pdf.multi_cell(0, 10, f" {option}")
|
84 |
pdf.set_text_color(0, 0, 0) # Reset color
|
85 |
pdf.multi_cell(0, 10, f"Explanation: {explanation}")
|
|
|
|
|
|
|
|
|
86 |
pdf.ln(10)
|
87 |
|
88 |
pdf.ln(10) # Add space after each module
|
@@ -96,16 +102,10 @@ def load_module(module_name):
|
|
96 |
|
97 |
def generate_new_question(module_name):
|
98 |
module = load_module(module_name)
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
'options': options,
|
104 |
-
'correct': correct_answer,
|
105 |
-
'explanation': explanation,
|
106 |
-
'selected': None,
|
107 |
-
'answered': False
|
108 |
-
}
|
109 |
|
110 |
def navigate_question(direction):
|
111 |
if direction == "prev" and st.session_state.current_index > 0:
|
@@ -128,38 +128,17 @@ if module_name != st.session_state.current_module:
|
|
128 |
st.session_state.current_index = 0
|
129 |
st.session_state.questions = [generate_new_question(module_name)]
|
130 |
|
131 |
-
# Load the current module
|
132 |
-
current_module = load_module(st.session_state.current_module)
|
133 |
current_question = st.session_state.questions[st.session_state.current_index]
|
134 |
|
135 |
# Display module title and description
|
136 |
-
st.title(
|
137 |
-
st.write(
|
138 |
-
|
139 |
-
# Button Row: Prev, Next, and PDF Download
|
140 |
-
col1, col2, col3 = st.columns([1, 1, 2])
|
141 |
-
with col1:
|
142 |
-
st.button("⬅️ Prev", disabled=st.session_state.current_index == 0, on_click=lambda: navigate_question("prev"))
|
143 |
-
with col2:
|
144 |
-
st.button("➡️ Next", on_click=lambda: navigate_question("next"))
|
145 |
-
with col3:
|
146 |
-
if len(st.session_state.questions) > 0:
|
147 |
-
pdf = generate_pdf_report()
|
148 |
-
st.session_state.pdf_data = pdf # Reset PDF cache
|
149 |
-
st.download_button(
|
150 |
-
label="Download PDF Report 📄",
|
151 |
-
data=st.session_state.pdf_data,
|
152 |
-
file_name="quiz_report.pdf",
|
153 |
-
mime="application/pdf"
|
154 |
-
)
|
155 |
-
|
156 |
-
# Display the current question
|
157 |
-
st.write(f"**Question {st.session_state.current_index + 1}:** {current_question['question']}")
|
158 |
|
159 |
# Option highlighting logic
|
160 |
def get_option_style(option):
|
161 |
-
if current_question
|
162 |
-
if option == current_question['
|
163 |
return "background-color:#d4edda;padding:10px;border-radius:5px;" # Green background for correct
|
164 |
elif option == current_question['selected']:
|
165 |
return "background-color:#f8d7da;padding:10px;border-radius:5px;" # Red background for incorrect
|
@@ -168,21 +147,27 @@ def get_option_style(option):
|
|
168 |
selected_answer = st.radio(
|
169 |
"Choose an answer:",
|
170 |
current_question['options'],
|
171 |
-
index=current_question['options'].index(current_question
|
172 |
-
key=st.session_state.current_index
|
173 |
-
format_func=lambda x: f"{x}"
|
174 |
)
|
175 |
|
176 |
if st.button("Submit"):
|
177 |
-
if not current_question
|
178 |
current_question['selected'] = selected_answer
|
179 |
current_question['answered'] = True
|
180 |
st.session_state.module_question_count[module_name] += 1
|
181 |
|
182 |
-
if selected_answer == current_question['
|
183 |
st.session_state.correct_count += 1
|
184 |
st.session_state.module_correct_count[module_name] += 1
|
185 |
|
186 |
# Retain and highlight the options after submission
|
187 |
for option in current_question['options']:
|
188 |
st.markdown(f"<div style='{get_option_style(option)}'>{option}</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import logging
|
4 |
from fpdf import FPDF
|
5 |
import uuid
|
6 |
+
import json
|
7 |
|
8 |
# Configure logging
|
9 |
logging.basicConfig(level=logging.INFO)
|
|
|
64 |
|
65 |
for entry in st.session_state.questions:
|
66 |
if entry['module'] == module:
|
67 |
+
question, options, selected, correct, explanation, step_by_step_solution = (
|
68 |
entry['question'],
|
69 |
entry['options'],
|
70 |
entry['selected'],
|
71 |
+
entry['correct_answer'],
|
72 |
+
entry['explanation'],
|
73 |
+
entry['step_by_step_solution']
|
74 |
)
|
75 |
pdf.multi_cell(0, 10, f"Q: {question}")
|
76 |
for option in options:
|
|
|
85 |
pdf.multi_cell(0, 10, f" {option}")
|
86 |
pdf.set_text_color(0, 0, 0) # Reset color
|
87 |
pdf.multi_cell(0, 10, f"Explanation: {explanation}")
|
88 |
+
pdf.ln(5)
|
89 |
+
pdf.multi_cell(0, 10, "Step-by-Step Solution:")
|
90 |
+
for step in step_by_step_solution:
|
91 |
+
pdf.multi_cell(0, 10, step)
|
92 |
pdf.ln(10)
|
93 |
|
94 |
pdf.ln(10) # Add space after each module
|
|
|
102 |
|
103 |
def generate_new_question(module_name):
|
104 |
module = load_module(module_name)
|
105 |
+
question_data = module.generate_question()
|
106 |
+
# Ensure 'answered' is initialized to False
|
107 |
+
question_data['answered'] = False
|
108 |
+
return question_data
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
def navigate_question(direction):
|
111 |
if direction == "prev" and st.session_state.current_index > 0:
|
|
|
128 |
st.session_state.current_index = 0
|
129 |
st.session_state.questions = [generate_new_question(module_name)]
|
130 |
|
131 |
+
# Load the current module's question
|
|
|
132 |
current_question = st.session_state.questions[st.session_state.current_index]
|
133 |
|
134 |
# Display module title and description
|
135 |
+
st.title(module_name)
|
136 |
+
st.write(current_question["question"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
# Option highlighting logic
|
139 |
def get_option_style(option):
|
140 |
+
if current_question.get('answered', False): # Use .get() with default False
|
141 |
+
if option == current_question['correct_answer']:
|
142 |
return "background-color:#d4edda;padding:10px;border-radius:5px;" # Green background for correct
|
143 |
elif option == current_question['selected']:
|
144 |
return "background-color:#f8d7da;padding:10px;border-radius:5px;" # Red background for incorrect
|
|
|
147 |
selected_answer = st.radio(
|
148 |
"Choose an answer:",
|
149 |
current_question['options'],
|
150 |
+
index=current_question['options'].index(current_question.get('selected', '')) if current_question.get('selected') else None,
|
151 |
+
key=st.session_state.current_index
|
|
|
152 |
)
|
153 |
|
154 |
if st.button("Submit"):
|
155 |
+
if not current_question.get('answered', False): # Check if the question has already been answered
|
156 |
current_question['selected'] = selected_answer
|
157 |
current_question['answered'] = True
|
158 |
st.session_state.module_question_count[module_name] += 1
|
159 |
|
160 |
+
if selected_answer == current_question['correct_answer']:
|
161 |
st.session_state.correct_count += 1
|
162 |
st.session_state.module_correct_count[module_name] += 1
|
163 |
|
164 |
# Retain and highlight the options after submission
|
165 |
for option in current_question['options']:
|
166 |
st.markdown(f"<div style='{get_option_style(option)}'>{option}</div>", unsafe_allow_html=True)
|
167 |
+
|
168 |
+
# Show explanation and step-by-step solution
|
169 |
+
if current_question.get('answered', False):
|
170 |
+
st.write(f"**Explanation:** {current_question['explanation']}")
|
171 |
+
st.write("**Step-by-Step Solution:**")
|
172 |
+
for step in current_question['step_by_step_solution']:
|
173 |
+
st.write(step)
|