Sina Media Lab commited on
Commit
1086036
·
1 Parent(s): d8bd1c9
Files changed (1) hide show
  1. app.py +46 -37
app.py CHANGED
@@ -23,8 +23,8 @@ if 'pdf_data' not in st.session_state:
23
  st.session_state.pdf_data = None
24
  if 'selected_answer' not in st.session_state:
25
  st.session_state.selected_answer = None
26
- if 'answered' not in st.session_state:
27
- st.session_state.answered = False
28
 
29
  def reset_pdf_cache():
30
  st.session_state.pdf_data = None
@@ -107,10 +107,10 @@ def generate_new_question(module_name, module):
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.answered = True
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.answered = True
114
 
115
  # Load all modules dynamically
116
  modules = load_modules()
@@ -132,7 +132,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.answered = False
136
 
137
  # Load the current module's question
138
  current_question = st.session_state.questions[st.session_state.current_index]
@@ -162,29 +162,35 @@ with col3:
162
 
163
  st.write(current_question["question"])
164
 
165
- # Handle the selection of an option and check the answer immediately
166
- selected_answer = st.radio(
167
- "Choose an answer:",
168
- options=current_question['options'],
169
- index=current_question['options'].index(current_question['selected']) if current_question['answered'] else None,
170
- key=f"question_{st.session_state.current_index}_options"
171
- )
172
-
173
- if selected_answer is not None and not current_question['answered']:
174
- # Process the answer
175
- current_question['selected'] = selected_answer
176
- current_question['answered'] = True
177
- st.session_state.module_question_count[selected_module] += 1
178
-
179
- if selected_answer == current_question['correct_answer']:
180
- st.session_state.correct_count += 1
181
- st.session_state.module_correct_count[selected_module] += 1
182
-
183
- # Set answered to true to disable options
184
- st.session_state.questions[st.session_state.current_index]['answered'] = True
185
- st.session_state.selected_answer = selected_answer
186
-
187
- # Show correct/incorrect feedback immediately
 
 
 
 
 
 
188
  for option in current_question['options']:
189
  if option == current_question['correct_answer']:
190
  st.markdown(f"<span style='color:green;'>{option} ✅</span>", unsafe_allow_html=True)
@@ -193,15 +199,18 @@ if selected_answer is not None and not current_question['answered']:
193
  else:
194
  st.markdown(f"{option}")
195
 
196
- # Show explanation and step-by-step solution with improved visuals
197
  st.write(f"**Explanation:** {current_question['explanation']}")
198
- st.markdown("""
199
- <div style='border: 2px solid #4CAF50; padding: 10px; margin-top: 20px; background-color: #f9f9f9;'>
200
- <h4 style='color: #4CAF50;'>Step-by-Step Solution:</h4>
201
- <ol>
202
- """, unsafe_allow_html=True)
203
-
204
  for step in current_question['step_by_step_solution']:
205
- st.markdown(f"<li style='margin-bottom: 10px;'>{step}</li>", unsafe_allow_html=True)
206
 
207
- st.markdown("</ol></div>", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
23
  st.session_state.pdf_data = None
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
 
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()
 
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]
 
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(
168
+ "Choose an answer:",
169
+ options=current_question['options'],
170
+ key=f"question_{st.session_state.current_index}_options",
171
+ index=current_question['options'].index(current_question['selected']) if current_question['answered'] else None,
172
+ disabled=current_question['answered'] # Disable if the question has been answered
173
+ )
174
+
175
+ submit_button = st.form_submit_button(label=st.session_state.button_label)
176
+
177
+ # Handle button state and answer submission
178
+ if submit_button and st.session_state.button_label == "Submit":
179
+ if selected_answer is not None:
180
+ # Process the answer
181
+ current_question['selected'] = selected_answer
182
+ current_question['answered'] = True
183
+ st.session_state.module_question_count[selected_module] += 1
184
+
185
+ if selected_answer == current_question['correct_answer']:
186
+ st.session_state.correct_count += 1
187
+ st.session_state.module_correct_count[selected_module] += 1
188
+
189
+ # Show correct/incorrect feedback and explanation
190
+ st.session_state.button_label = "Next Question"
191
+
192
+ # Show correct/incorrect feedback after submission
193
+ if current_question.get('answered', False):
194
  for option in current_question['options']:
195
  if option == current_question['correct_answer']:
196
  st.markdown(f"<span style='color:green;'>{option} ✅</span>", unsafe_allow_html=True)
 
199
  else:
200
  st.markdown(f"{option}")
201
 
202
+ # Show explanation and step-by-step solution
203
  st.write(f"**Explanation:** {current_question['explanation']}")
204
+ st.write("**Step-by-Step Solution:**")
 
 
 
 
 
205
  for step in current_question['step_by_step_solution']:
206
+ st.write(step)
207
 
208
+ # Change the button label immediately after showing the solution
209
+ st.session_state.button_label = "Next Question"
210
+
211
+ # Handle switching to the next question on button click
212
+ if submit_button and st.session_state.button_label == "Next Question":
213
+ new_question = generate_new_question(selected_module, modules[selected_module])
214
+ st.session_state.questions.append(new_question)
215
+ st.session_state.current_index = len(st.session_state.questions) - 1
216
+ st.session_state.button_label = "Submit"