Sina Media Lab commited on
Commit
516f536
Β·
1 Parent(s): ed05a56
Files changed (1) hide show
  1. app.py +77 -73
app.py CHANGED
@@ -27,6 +27,8 @@ if 'answered' not in st.session_state:
27
  st.session_state.answered = False
28
  if 'submit_click_count' not in st.session_state:
29
  st.session_state.submit_click_count = 0
 
 
30
 
31
  def reset_pdf_cache():
32
  st.session_state.pdf_data = None
@@ -136,6 +138,7 @@ if selected_module != st.session_state.current_module:
136
  st.session_state.selected_answer = None
137
  st.session_state.answered = False
138
  st.session_state.submit_click_count = 0 # Reset the submit click count
 
139
 
140
  # Load the current module's question
141
  current_question = st.session_state.questions[st.session_state.current_index]
@@ -165,76 +168,77 @@ with col3:
165
 
166
  st.write(current_question["question"])
167
 
168
- # Determine the button label based on click count
169
- button_label = "Submit/Next" if st.session_state.submit_click_count == 0 else "Next"
170
-
171
- # Create the form for the question
172
- with st.form(key=f'question_form_{st.session_state.current_index}'):
173
- selected_answer = st.radio(
174
- "Choose an answer:",
175
- options=current_question['options'],
176
- index=current_question['options'].index(current_question['selected']) if current_question['answered'] else None,
177
- key=f"question_{st.session_state.current_index}_options"
178
- )
179
-
180
- submit_button = st.form_submit_button(label=button_label)
181
-
182
- # Handle button state and answer submission
183
- if submit_button:
184
- if selected_answer is None:
185
- st.warning("Please select an option before submitting.")
186
- else:
187
- st.session_state.submit_click_count += 1
188
-
189
- # Show the number of times the Submit/Next button is clicked
190
- st.write(f"Submit/Next button clicked {st.session_state.submit_click_count} time(s).")
191
-
192
- if st.session_state.submit_click_count == 1:
193
- # First click: show solution
194
- if not current_question['answered']:
195
- if selected_answer is not None:
196
- # Process the answer
197
- current_question['selected'] = selected_answer
198
- current_question['answered'] = True
199
- st.session_state.module_question_count[selected_module] += 1
200
-
201
- if selected_answer == current_question['correct_answer']:
202
- st.session_state.correct_count += 1
203
- st.session_state.module_correct_count[selected_module] += 1
204
-
205
- # Set answered to true to disable options
206
- st.session_state.questions[st.session_state.current_index]['answered'] = True
207
- st.session_state.selected_answer = selected_answer
208
-
209
- # Show correct/incorrect feedback after submission
210
- for option in current_question['options']:
211
- if option == current_question['correct_answer']:
212
- st.markdown(f"<span style='color:green;'>{option} βœ…</span>", unsafe_allow_html=True)
213
- elif option == current_question['selected']:
214
- st.markdown(f"<span style='color:red;'>{option} ❌</span>", unsafe_allow_html=True)
215
- else:
216
- st.markdown(f"{option}")
217
-
218
- # Show explanation and step-by-step solution with improved visuals
219
- st.write(f"**Explanation:** {current_question['explanation']}")
220
- st.markdown("""
221
- <div style='border: 2px solid #4CAF50; padding: 10px; margin-top: 20px; background-color: #f9f9f9;'>
222
- <h4 style='color: #4CAF50;'>Step-by-Step Solution:</h4>
223
- <ol>
224
- """, unsafe_allow_html=True)
225
-
226
- for step in current_question['step_by_step_solution']:
227
- st.markdown(f"<li style='margin-bottom: 10px;'>{step}</li>", unsafe_allow_html=True)
228
-
229
- st.markdown("</ol></div>", unsafe_allow_html=True)
230
-
231
- elif st.session_state.submit_click_count == 2:
232
- # Second click: generate a new question
233
- new_question = generate_new_question(selected_module, modules[selected_module])
234
- st.session_state.questions.append(new_question)
235
- st.session_state.current_index = len(st.session_state.questions) - 1
236
- st.session_state.answered = False
237
- st.session_state.submit_click_count = 0 # Reset the click count
238
- st.session_state.selected_answer = None # Reset the selected answer
239
- # Force a re-render by modifying a state variable
240
- st.session_state['last_updated'] = str(uuid.uuid4())
 
 
27
  st.session_state.answered = False
28
  if 'submit_click_count' not in st.session_state:
29
  st.session_state.submit_click_count = 0
30
+ if 'show_next_button' not in st.session_state:
31
+ st.session_state.show_next_button = False
32
 
33
  def reset_pdf_cache():
34
  st.session_state.pdf_data = None
 
138
  st.session_state.selected_answer = None
139
  st.session_state.answered = False
140
  st.session_state.submit_click_count = 0 # Reset the submit click count
141
+ st.session_state.show_next_button = False # Reset the next button visibility
142
 
143
  # Load the current module's question
144
  current_question = st.session_state.questions[st.session_state.current_index]
 
168
 
169
  st.write(current_question["question"])
170
 
171
+ # Show Submit/Next button or Next button depending on the state
172
+ if not st.session_state.show_next_button:
173
+ # Create the form for the question
174
+ with st.form(key=f'question_form_{st.session_state.current_index}'):
175
+ selected_answer = st.radio(
176
+ "Choose an answer:",
177
+ options=current_question['options'],
178
+ index=current_question['options'].index(current_question['selected']) if current_question['answered'] else None,
179
+ key=f"question_{st.session_state.current_index}_options"
180
+ )
181
+
182
+ submit_button = st.form_submit_button(label="Submit/Next")
183
+
184
+ # Handle button state and answer submission
185
+ if submit_button:
186
+ if selected_answer is None:
187
+ st.warning("Please select an option before submitting.")
188
+ else:
189
+ st.session_state.submit_click_count += 1
190
+
191
+ if st.session_state.submit_click_count == 1:
192
+ # First click: show solution and replace Submit/Next with Next
193
+ if not current_question['answered']:
194
+ if selected_answer is not None:
195
+ # Process the answer
196
+ current_question['selected'] = selected_answer
197
+ current_question['answered'] = True
198
+ st.session_state.module_question_count[selected_module] += 1
199
+
200
+ if selected_answer == current_question['correct_answer']:
201
+ st.session_state.correct_count += 1
202
+ st.session_state.module_correct_count[selected_module] += 1
203
+
204
+ # Set answered to true to disable options
205
+ st.session_state.questions[st.session_state.current_index]['answered'] = True
206
+ st.session_state.selected_answer = selected_answer
207
+
208
+ # Show correct/incorrect feedback after submission
209
+ for option in current_question['options']:
210
+ if option == current_question['correct_answer']:
211
+ st.markdown(f"<span style='color:green;'>{option} βœ…</span>", unsafe_allow_html=True)
212
+ elif option == current_question['selected']:
213
+ st.markdown(f"<span style='color:red;'>{option} ❌</span>", unsafe_allow_html=True)
214
+ else:
215
+ st.markdown(f"{option}")
216
+
217
+ # Show explanation and step-by-step solution with improved visuals
218
+ st.write(f"**Explanation:** {current_question['explanation']}")
219
+ st.markdown("""
220
+ <div style='border: 2px solid #4CAF50; padding: 10px; margin-top: 20px; background-color: #f9f9f9;'>
221
+ <h4 style='color: #4CAF50;'>Step-by-Step Solution:</h4>
222
+ <ol>
223
+ """, unsafe_allow_html=True)
224
+
225
+ for step in current_question['step_by_step_solution']:
226
+ st.markdown(f"<li style='margin-bottom: 10px;'>{step}</li>", unsafe_allow_html=True)
227
+
228
+ st.markdown("</ol></div>", unsafe_allow_html=True)
229
+
230
+ # Replace Submit/Next with Next
231
+ st.session_state.show_next_button = True
232
+ else:
233
+ # Show the Next button
234
+ if st.button("Next"):
235
+ # Generate a new question
236
+ new_question = generate_new_question(selected_module, modules[selected_module])
237
+ st.session_state.questions.append(new_question)
238
+ st.session_state.current_index = len(st.session_state.questions) - 1
239
+ st.session_state.answered = False
240
+ st.session_state.submit_click_count = 0 # Reset the click count
241
+ st.session_state.selected_answer = None # Reset the selected answer
242
+ st.session_state.show_next_button = False # Hide the Next button
243
+ # Force a re-render by modifying a state variable
244
+ st.session_state['last_updated'] = str(uuid.uuid4())