Sina Media Lab commited on
Commit
d015779
·
1 Parent(s): 217e8f9
Files changed (1) hide show
  1. app.py +33 -41
app.py CHANGED
@@ -132,28 +132,26 @@ if module_name != st.session_state.current_module:
132
  # Load the current module's question
133
  current_question = st.session_state.questions[st.session_state.current_index]
134
 
 
 
 
 
 
 
 
 
 
 
135
  # Display module title and description
136
  st.title(module_name)
137
  st.write(current_question["question"])
138
 
139
- # Option highlighting logic
140
- def get_option_style(option):
141
- if current_question.get('answered', False): # Use .get() with default False
142
- if option == current_question['correct_answer']:
143
- return "background-color:#d4edda;padding:10px;border-radius:5px;" # Green background for correct
144
- elif option == current_question['selected']:
145
- return "background-color:#f8d7da;padding:10px;border-radius:5px;" # Red background for incorrect
146
- return "background-color:#f0f0f0;padding:10px;border-radius:5px;" # Gray background by default
147
-
148
- # Ensure each radio button has a unique key
149
- unique_key = f"question_{st.session_state.current_index}"
150
-
151
- # Render options with custom styling
152
  if not current_question.get('answered', False):
153
  selected_answer = st.radio(
154
  "Choose an answer:",
155
- options=[f"{option}" for option in current_question['options']],
156
- key=unique_key
157
  )
158
 
159
  if st.button("Submit"):
@@ -165,36 +163,30 @@ if not current_question.get('answered', False):
165
  st.session_state.correct_count += 1
166
  st.session_state.module_correct_count[module_name] += 1
167
 
168
- # Instead of st.experimental_rerun(), force a refresh by updating a dummy session state variable
169
- st.session_state.dummy = not st.session_state.get('dummy', False)
170
- else:
171
- for option in current_question['options']:
172
- st.markdown(f"<div style='{get_option_style(option)}'>{option}</div>", unsafe_allow_html=True)
173
 
174
- # Show explanation and step-by-step solution
175
  if current_question.get('answered', False):
 
 
 
 
 
 
 
176
  st.write(f"**Explanation:** {current_question['explanation']}")
177
  st.write("**Step-by-Step Solution:**")
178
  for step in current_question['step_by_step_solution']:
179
  st.write(step)
180
 
181
- # Navigation buttons
182
- col1, col2, col3 = st.columns([1, 1, 2])
183
- with col1:
184
- if st.button("⬅️ Prev", disabled=st.session_state.current_index == 0):
185
- navigate_question("prev")
186
-
187
- with col2:
188
- if st.button("➡️ Next"):
189
- navigate_question("next")
190
-
191
- with col3:
192
- if len(st.session_state.questions) > 0:
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
- )
 
132
  # Load the current module's question
133
  current_question = st.session_state.questions[st.session_state.current_index]
134
 
135
+ # Navigation buttons (placed on top)
136
+ col1, col2 = st.columns([1, 1])
137
+ with col1:
138
+ if st.button("⬅️ Prev", disabled=st.session_state.current_index == 0):
139
+ navigate_question("prev")
140
+
141
+ with col2:
142
+ if st.button("➡️ Next"):
143
+ navigate_question("next")
144
+
145
  # Display module title and description
146
  st.title(module_name)
147
  st.write(current_question["question"])
148
 
149
+ # Render options without background
 
 
 
 
 
 
 
 
 
 
 
 
150
  if not current_question.get('answered', False):
151
  selected_answer = st.radio(
152
  "Choose an answer:",
153
+ options=current_question['options'],
154
+ key=f"question_{st.session_state.current_index}"
155
  )
156
 
157
  if st.button("Submit"):
 
163
  st.session_state.correct_count += 1
164
  st.session_state.module_correct_count[module_name] += 1
165
 
166
+ # Trigger a UI update
167
+ st.experimental_set_query_params(dummy=not st.session_state.get('dummy', False))
 
 
 
168
 
169
+ # Show correct/incorrect feedback after submission
170
  if current_question.get('answered', False):
171
+ for option in current_question['options']:
172
+ if option == current_question['correct_answer']:
173
+ st.write(f"**Correct Answer:** {option}")
174
+ elif option == current_question['selected']:
175
+ st.write(f"**Your Answer:** {option} (Incorrect)")
176
+
177
+ # Show explanation and step-by-step solution
178
  st.write(f"**Explanation:** {current_question['explanation']}")
179
  st.write("**Step-by-Step Solution:**")
180
  for step in current_question['step_by_step_solution']:
181
  st.write(step)
182
 
183
+ # PDF Download Button (at the bottom)
184
+ if len(st.session_state.questions) > 0:
185
+ pdf = generate_pdf_report()
186
+ st.session_state.pdf_data = pdf # Reset PDF cache
187
+ st.download_button(
188
+ label="Download PDF Report 📄",
189
+ data=st.session_state.pdf_data,
190
+ file_name="quiz_report.pdf",
191
+ mime="application/pdf"
192
+ )