Sina Media Lab commited on
Commit
c5fe3bc
·
1 Parent(s): 07ba05d
Files changed (1) hide show
  1. app.py +42 -24
app.py CHANGED
@@ -136,33 +136,51 @@ st.write(current_question["question"])
136
 
137
  # Option highlighting logic
138
  def get_option_style(option):
139
- if current_question.get('answered', False): # Use .get() with default False
140
  if option == current_question['correct_answer']:
141
  return "background-color:#d4edda;padding:10px;border-radius:5px;" # Green background for correct
142
  elif option == current_question['selected']:
143
  return "background-color:#f8d7da;padding:10px;border-radius:5px;" # Red background for incorrect
144
  return "background-color:#f0f0f0;padding:10px;border-radius:5px;" # Gray background by default
145
 
146
- def format_option(option, option_style):
147
- return f"<label style='{option_style}'>{option}</label>"
148
-
149
- # Display options with custom formatting
150
- selected_answer = st.radio(
151
- "Choose an answer:",
152
- options=[format_option(option, get_option_style(option)) for option in current_question['options']],
153
- format_func=lambda x: x,
154
- key=st.session_state.current_index,
155
- label_visibility="collapsed",
156
- )
157
-
158
- if st.button("Submit"):
159
- if not current_question.get('answered', False): # Check if the question has already been answered
160
- current_question['selected'] = selected_answer
161
- current_question['answered'] = True
162
- st.session_state.module_question_count[module_name] += 1
163
-
164
- if selected_answer == current_question['correct_answer']:
165
- st.session_state.correct_count += 1
166
- st.session_state.module_correct_count[module_name] += 1
167
-
168
- # Since we're not adding an additional section, remove any content below the options.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
  # Option highlighting logic
138
  def get_option_style(option):
139
+ if current_question.get('answered', False):
140
  if option == current_question['correct_answer']:
141
  return "background-color:#d4edda;padding:10px;border-radius:5px;" # Green background for correct
142
  elif option == current_question['selected']:
143
  return "background-color:#f8d7da;padding:10px;border-radius:5px;" # Red background for incorrect
144
  return "background-color:#f0f0f0;padding:10px;border-radius:5px;" # Gray background by default
145
 
146
+ # Display each option with custom styles
147
+ selected_answer = None
148
+ for i, option in enumerate(current_question['options']):
149
+ option_html = f"<div style='{get_option_style(option)}'>{option}</div>"
150
+ if st.button(option_html, key=f"option_{i}"):
151
+ selected_answer = option
152
+
153
+ if selected_answer:
154
+ current_question['selected'] = selected_answer
155
+ current_question['answered'] = True
156
+ st.session_state.module_question_count[module_name] += 1
157
+
158
+ if selected_answer == current_question['correct_answer']:
159
+ st.session_state.correct_count += 1
160
+ st.session_state.module_correct_count[module_name] += 1
161
+
162
+ # Refresh the display after submission to show the correct/incorrect styles
163
+ st.experimental_rerun()
164
+
165
+ # Navigation buttons
166
+ col1, col2, col3 = st.columns([1, 1, 2])
167
+ with col1:
168
+ if st.button("⬅️ Prev", disabled=st.session_state.current_index == 0):
169
+ navigate_question("prev")
170
+ st.experimental_rerun()
171
+
172
+ with col2:
173
+ if st.button("➡️ Next"):
174
+ navigate_question("next")
175
+ st.experimental_rerun()
176
+
177
+ with col3:
178
+ if len(st.session_state.questions) > 0:
179
+ pdf = generate_pdf_report()
180
+ st.session_state.pdf_data = pdf # Reset PDF cache
181
+ st.download_button(
182
+ label="Download PDF Report 📄",
183
+ data=st.session_state.pdf_data,
184
+ file_name="quiz_report.pdf",
185
+ mime="application/pdf"
186
+ )