Sina Media Lab
commited on
Commit
Β·
a086503
1
Parent(s):
67f54db
Updates
Browse files
app.py
CHANGED
|
@@ -24,7 +24,7 @@ if 'pdf_data' not in st.session_state:
|
|
| 24 |
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"
|
| 28 |
|
| 29 |
def reset_pdf_cache():
|
| 30 |
st.session_state.pdf_data = None
|
|
@@ -104,14 +104,6 @@ def generate_new_question(module_name, module):
|
|
| 104 |
st.warning(f"Question in module '{module_name}' does not have 4 options. Found {len(question_data['options'])}.")
|
| 105 |
return question_data
|
| 106 |
|
| 107 |
-
def navigate_question(direction):
|
| 108 |
-
if direction == "prev" and st.session_state.current_index > 0:
|
| 109 |
-
st.session_state.current_index -= 1
|
| 110 |
-
st.session_state.button_label = "Next Question"
|
| 111 |
-
elif direction == "next" and st.session_state.current_index < len(st.session_state.questions) - 1:
|
| 112 |
-
st.session_state.current_index += 1
|
| 113 |
-
st.session_state.button_label = "Next Question"
|
| 114 |
-
|
| 115 |
# Load all modules dynamically
|
| 116 |
modules = load_modules()
|
| 117 |
|
|
@@ -132,7 +124,7 @@ if selected_module != st.session_state.current_module:
|
|
| 132 |
st.session_state.module_question_count[selected_module] = 0
|
| 133 |
st.session_state.module_correct_count[selected_module] = 0
|
| 134 |
st.session_state.selected_answer = None
|
| 135 |
-
st.session_state.button_label = "Submit"
|
| 136 |
|
| 137 |
# Load the current module's question
|
| 138 |
current_question = st.session_state.questions[st.session_state.current_index]
|
|
@@ -141,27 +133,6 @@ current_question = st.session_state.questions[st.session_state.current_index]
|
|
| 141 |
st.title(modules[selected_module]["title"])
|
| 142 |
st.write(modules[selected_module]["description"])
|
| 143 |
|
| 144 |
-
# Navigation and PDF report buttons
|
| 145 |
-
col1, col2, col3 = st.columns([1, 1, 2])
|
| 146 |
-
with col1:
|
| 147 |
-
if st.button("β¬
οΈ Prev", disabled=st.session_state.current_index == 0):
|
| 148 |
-
navigate_question("prev")
|
| 149 |
-
with col2:
|
| 150 |
-
if st.button("β‘οΈ Next", disabled=st.session_state.current_index >= len(st.session_state.questions) - 1):
|
| 151 |
-
navigate_question("next")
|
| 152 |
-
with col3:
|
| 153 |
-
if len(st.session_state.questions) > 0:
|
| 154 |
-
pdf = generate_pdf_report()
|
| 155 |
-
st.session_state.pdf_data = pdf # Reset PDF cache
|
| 156 |
-
st.download_button(
|
| 157 |
-
label="Download PDF Report π",
|
| 158 |
-
data=st.session_state.pdf_data,
|
| 159 |
-
file_name="quiz_report.pdf",
|
| 160 |
-
mime="application/pdf"
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
st.write(current_question["question"])
|
| 164 |
-
|
| 165 |
# Create the form for the question
|
| 166 |
with st.form(key=f'question_form_{st.session_state.current_index}'):
|
| 167 |
selected_answer = st.radio(
|
|
@@ -171,11 +142,13 @@ with st.form(key=f'question_form_{st.session_state.current_index}'):
|
|
| 171 |
index=None # Ensure no option is pre-selected
|
| 172 |
)
|
| 173 |
|
| 174 |
-
submit_button = st.form_submit_button(label=
|
| 175 |
|
| 176 |
# Handle button state and answer submission
|
| 177 |
-
if submit_button
|
| 178 |
-
if selected_answer is
|
|
|
|
|
|
|
| 179 |
# Process the answer
|
| 180 |
current_question['selected'] = selected_answer
|
| 181 |
current_question['answered'] = True
|
|
@@ -186,30 +159,20 @@ if submit_button and st.session_state.button_label == "Submit":
|
|
| 186 |
st.session_state.module_correct_count[selected_module] += 1
|
| 187 |
|
| 188 |
# Show correct/incorrect feedback and explanation
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
st.
|
| 206 |
-
|
| 207 |
-
# Change the button label immediately after showing the solution
|
| 208 |
-
st.session_state.button_label = "Next Question"
|
| 209 |
-
|
| 210 |
-
# This part handles switching to the next question on button click
|
| 211 |
-
if submit_button and st.session_state.button_label == "Next Question":
|
| 212 |
-
new_question = generate_new_question(selected_module, modules[selected_module])
|
| 213 |
-
st.session_state.questions.append(new_question)
|
| 214 |
-
st.session_state.current_index = len(st.session_state.questions) - 1
|
| 215 |
-
st.session_state.button_label = "Submit"
|
|
|
|
| 24 |
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
|
|
|
|
| 104 |
st.warning(f"Question in module '{module_name}' does not have 4 options. Found {len(question_data['options'])}.")
|
| 105 |
return question_data
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
# Load all modules dynamically
|
| 108 |
modules = load_modules()
|
| 109 |
|
|
|
|
| 124 |
st.session_state.module_question_count[selected_module] = 0
|
| 125 |
st.session_state.module_correct_count[selected_module] = 0
|
| 126 |
st.session_state.selected_answer = None
|
| 127 |
+
st.session_state.button_label = "Submit/New"
|
| 128 |
|
| 129 |
# Load the current module's question
|
| 130 |
current_question = st.session_state.questions[st.session_state.current_index]
|
|
|
|
| 133 |
st.title(modules[selected_module]["title"])
|
| 134 |
st.write(modules[selected_module]["description"])
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
# Create the form for the question
|
| 137 |
with st.form(key=f'question_form_{st.session_state.current_index}'):
|
| 138 |
selected_answer = st.radio(
|
|
|
|
| 142 |
index=None # Ensure no option is pre-selected
|
| 143 |
)
|
| 144 |
|
| 145 |
+
submit_button = st.form_submit_button(label="Submit/New")
|
| 146 |
|
| 147 |
# Handle button state and answer submission
|
| 148 |
+
if submit_button:
|
| 149 |
+
if selected_answer is None:
|
| 150 |
+
st.warning("Please select an option before submitting.", icon="β οΈ")
|
| 151 |
+
else:
|
| 152 |
# Process the answer
|
| 153 |
current_question['selected'] = selected_answer
|
| 154 |
current_question['answered'] = True
|
|
|
|
| 159 |
st.session_state.module_correct_count[selected_module] += 1
|
| 160 |
|
| 161 |
# Show correct/incorrect feedback and explanation
|
| 162 |
+
for option in current_question['options']:
|
| 163 |
+
if option == current_question['correct_answer']:
|
| 164 |
+
st.markdown(f"<span style='color:green;'>{option} β
</span>", unsafe_allow_html=True)
|
| 165 |
+
elif option == current_question['selected']:
|
| 166 |
+
st.markdown(f"<span style='color:red;'>{option} β</span>", unsafe_allow_html=True)
|
| 167 |
+
else:
|
| 168 |
+
st.markdown(f"{option}")
|
| 169 |
+
|
| 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)
|
| 174 |
+
|
| 175 |
+
# Generate a new question after submission
|
| 176 |
+
new_question = generate_new_question(selected_module, modules[selected_module])
|
| 177 |
+
st.session_state.questions.append(new_question)
|
| 178 |
+
st.session_state.current_index = len(st.session_state.questions) - 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|