Sina Media Lab
commited on
Commit
·
e9df8e8
1
Parent(s):
ee53acf
Updates
Browse files
app.py
CHANGED
@@ -19,30 +19,18 @@ module_names = {
|
|
19 |
}
|
20 |
|
21 |
# Initialize session state variables
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
if 'correct_count' not in st.session_state:
|
23 |
st.session_state.correct_count = 0
|
24 |
-
if 'question_count' not in st.session_state:
|
25 |
-
st.session_state.question_count = 0
|
26 |
if 'module_correct_count' not in st.session_state:
|
27 |
st.session_state.module_correct_count = {name: 0 for name in module_names}
|
28 |
if 'module_question_count' not in st.session_state:
|
29 |
st.session_state.module_question_count = {name: 0 for name in module_names}
|
30 |
-
if 'current_module' not in st.session_state:
|
31 |
-
st.session_state.current_module = None
|
32 |
-
if 'question_queue' not in st.session_state:
|
33 |
-
st.session_state.question_queue = []
|
34 |
-
if 'submitted' not in st.session_state:
|
35 |
-
st.session_state.submitted = False
|
36 |
-
if 'current_question' not in st.session_state:
|
37 |
-
st.session_state.current_question = None
|
38 |
-
if 'options' not in st.session_state:
|
39 |
-
st.session_state.options = []
|
40 |
-
if 'correct_answer' not in st.session_state:
|
41 |
-
st.session_state.correct_answer = None
|
42 |
-
if 'explanation' not in st.session_state:
|
43 |
-
st.session_state.explanation = None
|
44 |
-
if 'selected_answer' not in st.session_state:
|
45 |
-
st.session_state.selected_answer = None
|
46 |
|
47 |
def generate_pdf_report():
|
48 |
pdf = FPDF()
|
@@ -60,7 +48,7 @@ def generate_pdf_report():
|
|
60 |
pdf.cell(200, 10, txt=f"Correct Answers: {correct_count}/{total_count}", ln=True, align="L")
|
61 |
pdf.ln(5)
|
62 |
|
63 |
-
for entry in st.session_state.
|
64 |
if entry['module'] == module:
|
65 |
question, options, selected, correct, explanation = (
|
66 |
entry['question'],
|
@@ -84,107 +72,93 @@ def generate_pdf_report():
|
|
84 |
|
85 |
return pdf.output(dest='S').encode('latin1', 'replace')
|
86 |
|
87 |
-
|
88 |
-
st.sidebar.title("Quiz Modules")
|
89 |
-
module_name = st.sidebar.radio("Choose a module:", list(module_names.keys()), index=0)
|
90 |
-
|
91 |
-
if module_name:
|
92 |
-
if st.session_state.current_module != module_name:
|
93 |
-
st.session_state.current_module = module_name
|
94 |
-
st.session_state.question_count = 0
|
95 |
-
st.session_state.correct_count = 0
|
96 |
-
st.session_state.question_queue = []
|
97 |
-
st.session_state.current_question = None
|
98 |
-
st.session_state.submitted = False
|
99 |
-
|
100 |
module_file = module_names[module_name]
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
with col2:
|
117 |
-
next_disabled = not st.session_state.submitted
|
118 |
-
st.button("➡️ Next", disabled=next_disabled, key='next_button', on_click=lambda: navigate_question("next"))
|
119 |
-
with col3:
|
120 |
-
pdf_disabled = st.session_state.question_count == 0
|
121 |
-
if not pdf_disabled:
|
122 |
-
pdf = generate_pdf_report()
|
123 |
-
st.download_button(
|
124 |
-
label="Download PDF Report 📄",
|
125 |
-
data=pdf,
|
126 |
-
file_name="quiz_report.pdf",
|
127 |
-
mime="application/pdf",
|
128 |
-
disabled=pdf_disabled
|
129 |
-
)
|
130 |
-
|
131 |
-
# Handle question generation and submission
|
132 |
-
if st.session_state.current_question is None or st.session_state.submitted:
|
133 |
-
st.session_state.current_question, st.session_state.options, st.session_state.correct_answer, st.session_state.explanation = generate_question()
|
134 |
-
st.session_state.submitted = False
|
135 |
-
st.session_state.selected_answer = None
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
-
|
142 |
-
|
|
|
143 |
|
144 |
-
|
145 |
-
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
st.session_state.module_question_count[module_name] += 1
|
148 |
-
|
|
|
149 |
st.session_state.correct_count += 1
|
150 |
st.session_state.module_correct_count[module_name] += 1
|
151 |
-
st.success("Correct!")
|
152 |
-
else:
|
153 |
-
st.error("Incorrect.")
|
154 |
-
|
155 |
-
st.session_state.question_queue.append({
|
156 |
-
'module': module_name,
|
157 |
-
'question': st.session_state.current_question,
|
158 |
-
'options': st.session_state.options,
|
159 |
-
'selected': st.session_state.selected_answer,
|
160 |
-
'correct': st.session_state.correct_answer,
|
161 |
-
'explanation': st.session_state.explanation
|
162 |
-
})
|
163 |
-
|
164 |
-
# Explanation with proper width
|
165 |
-
st.markdown(
|
166 |
-
f"""
|
167 |
-
<div style='border: 2px solid #ccc; padding: 15px;'>
|
168 |
-
<strong>Explanation:</strong> {st.session_state.explanation}
|
169 |
-
</div>
|
170 |
-
""",
|
171 |
-
unsafe_allow_html=True
|
172 |
-
)
|
173 |
-
|
174 |
-
except ModuleNotFoundError as e:
|
175 |
-
st.error(f"The module '{module_name}' was not found. Please select another module.")
|
176 |
-
except Exception as e:
|
177 |
-
st.error(f"An unexpected error occurred: {str(e)}")
|
178 |
|
179 |
-
|
180 |
-
if direction == "prev" and st.session_state.question_count > 1:
|
181 |
-
st.session_state.question_count -= 1
|
182 |
-
st.session_state.submitted = False
|
183 |
-
question_data = st.session_state.question_queue[st.session_state.question_count - 1]
|
184 |
-
st.session_state.current_question = question_data['question']
|
185 |
-
st.session_state.options = question_data['options']
|
186 |
-
st.session_state.selected_answer = question_data['selected']
|
187 |
-
st.session_state.correct_answer = question_data['correct']
|
188 |
-
st.session_state.explanation = question_data['explanation']
|
189 |
-
elif direction == "next" and st.session_state.submitted:
|
190 |
-
st.session_state.current_question = None
|
|
|
19 |
}
|
20 |
|
21 |
# Initialize session state variables
|
22 |
+
if 'questions' not in st.session_state:
|
23 |
+
st.session_state.questions = []
|
24 |
+
if 'current_index' not in st.session_state:
|
25 |
+
st.session_state.current_index = 0
|
26 |
+
if 'current_module' not in st.session_state:
|
27 |
+
st.session_state.current_module = None
|
28 |
if 'correct_count' not in st.session_state:
|
29 |
st.session_state.correct_count = 0
|
|
|
|
|
30 |
if 'module_correct_count' not in st.session_state:
|
31 |
st.session_state.module_correct_count = {name: 0 for name in module_names}
|
32 |
if 'module_question_count' not in st.session_state:
|
33 |
st.session_state.module_question_count = {name: 0 for name in module_names}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
def generate_pdf_report():
|
36 |
pdf = FPDF()
|
|
|
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 = (
|
54 |
entry['question'],
|
|
|
72 |
|
73 |
return pdf.output(dest='S').encode('latin1', 'replace')
|
74 |
|
75 |
+
def load_module(module_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
module_file = module_names[module_name]
|
77 |
+
module = importlib.import_module(f'modules.{module_file}')
|
78 |
+
return module
|
79 |
+
|
80 |
+
def generate_new_question(module_name):
|
81 |
+
module = load_module(module_name)
|
82 |
+
question, options, correct_answer, explanation = module.generate_question()
|
83 |
+
return {
|
84 |
+
'module': module_name,
|
85 |
+
'question': question,
|
86 |
+
'options': options,
|
87 |
+
'correct': correct_answer,
|
88 |
+
'explanation': explanation,
|
89 |
+
'selected': None,
|
90 |
+
'answered': False
|
91 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
+
def navigate_question(direction):
|
94 |
+
if direction == "prev" and st.session_state.current_index > 0:
|
95 |
+
st.session_state.current_index -= 1
|
96 |
+
elif direction == "next":
|
97 |
+
if st.session_state.current_index < len(st.session_state.questions) - 1:
|
98 |
+
st.session_state.current_index += 1
|
99 |
+
else:
|
100 |
+
# Generate a new question if at the end of the list
|
101 |
+
new_question = generate_new_question(st.session_state.current_module)
|
102 |
+
st.session_state.questions.append(new_question)
|
103 |
+
st.session_state.current_index += 1
|
104 |
|
105 |
+
# Streamlit interface
|
106 |
+
st.sidebar.title("Quiz Modules")
|
107 |
+
module_name = st.sidebar.radio("Choose a module:", list(module_names.keys()), index=0)
|
108 |
|
109 |
+
if module_name != st.session_state.current_module:
|
110 |
+
st.session_state.current_module = module_name
|
111 |
+
st.session_state.questions = []
|
112 |
+
st.session_state.current_index = 0
|
113 |
+
|
114 |
+
# Generate the first question for the new module
|
115 |
+
st.session_state.questions.append(generate_new_question(module_name))
|
116 |
+
|
117 |
+
current_question = st.session_state.questions[st.session_state.current_index]
|
118 |
+
|
119 |
+
# Button Row: Prev, Next, and PDF Download
|
120 |
+
col1, col2, col3 = st.columns([1, 1, 2])
|
121 |
+
with col1:
|
122 |
+
st.button("⬅️ Prev", disabled=st.session_state.current_index == 0, on_click=lambda: navigate_question("prev"))
|
123 |
+
with col2:
|
124 |
+
st.button("➡️ Next", on_click=lambda: navigate_question("next"))
|
125 |
+
with col3:
|
126 |
+
if st.session_state.current_index > 0:
|
127 |
+
pdf = generate_pdf_report()
|
128 |
+
st.download_button(
|
129 |
+
label="Download PDF Report 📄",
|
130 |
+
data=pdf,
|
131 |
+
file_name="quiz_report.pdf",
|
132 |
+
mime="application/pdf"
|
133 |
+
)
|
134 |
+
|
135 |
+
# Display the current question
|
136 |
+
st.write(f"**Question {st.session_state.current_index + 1}:** {current_question['question']}")
|
137 |
+
|
138 |
+
if current_question['answered']:
|
139 |
+
st.radio("Choose an answer:", current_question['options'], index=current_question['options'].index(current_question['selected']))
|
140 |
+
if current_question['selected'] == current_question['correct']:
|
141 |
+
st.success(f"Correct! Your answer: {current_question['selected']}")
|
142 |
+
else:
|
143 |
+
st.error(f"Incorrect. Your answer: {current_question['selected']}")
|
144 |
+
st.markdown(
|
145 |
+
f"""
|
146 |
+
<div style='border: 2px solid #ccc; padding: 15px;'>
|
147 |
+
<strong>Explanation:</strong> {current_question['explanation']}
|
148 |
+
</div>
|
149 |
+
""",
|
150 |
+
unsafe_allow_html=True
|
151 |
+
)
|
152 |
+
else:
|
153 |
+
selected_answer = st.radio("Choose an answer:", current_question['options'], key=st.session_state.current_index)
|
154 |
+
if st.button("Submit"):
|
155 |
+
if not current_question['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']:
|
161 |
st.session_state.correct_count += 1
|
162 |
st.session_state.module_correct_count[module_name] += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
+
st.experimental_rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|