Sina Media Lab commited on
Commit
c63a46d
·
1 Parent(s): b2acba8
Files changed (1) hide show
  1. app.py +42 -47
app.py CHANGED
@@ -23,10 +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 'submit_enabled' not in st.session_state:
27
- st.session_state.submit_enabled = True
28
- if 'new_enabled' not in st.session_state:
29
- st.session_state.new_enabled = False
30
 
31
  def reset_pdf_cache():
32
  st.session_state.pdf_data = None
@@ -109,12 +107,6 @@ def navigate_question(direction):
109
  st.session_state.current_index -= 1
110
  elif direction == "next" and st.session_state.current_index < len(st.session_state.questions) - 1:
111
  st.session_state.current_index += 1
112
- elif direction == "new":
113
- new_question = generate_new_question(st.session_state.current_module, modules[st.session_state.current_module])
114
- st.session_state.questions.append(new_question)
115
- st.session_state.current_index = len(st.session_state.questions) - 1
116
- st.session_state.submit_enabled = True
117
- st.session_state.new_enabled = False
118
 
119
  # Load all modules dynamically
120
  modules = load_modules()
@@ -136,8 +128,7 @@ if selected_module != st.session_state.current_module:
136
  st.session_state.module_question_count[selected_module] = 0
137
  st.session_state.module_correct_count[selected_module] = 0
138
  st.session_state.selected_answer = None
139
- st.session_state.submit_enabled = True
140
- st.session_state.new_enabled = False
141
 
142
  # Load the current module's question
143
  current_question = st.session_state.questions[st.session_state.current_index]
@@ -168,21 +159,18 @@ with col3:
168
  st.write(current_question["question"])
169
 
170
  # Create the form for the question
171
- selected_answer = st.radio(
172
- "Choose an answer:",
173
- options=current_question['options'],
174
- key=f"question_{st.session_state.current_index}_options",
175
- index=None # Ensure no option is pre-selected
176
- )
177
-
178
- col1, col2 = st.columns(2)
179
- with col1:
180
- submit_button = st.button(label="Submit", disabled=not st.session_state.submit_enabled)
181
- with col2:
182
- new_button = st.button(label="New", disabled=not st.session_state.new_enabled)
183
 
184
  # Handle button state and answer submission
185
- if submit_button:
186
  if selected_answer is not None:
187
  # Process the answer
188
  current_question['selected'] = selected_answer
@@ -193,27 +181,34 @@ if submit_button:
193
  st.session_state.correct_count += 1
194
  st.session_state.module_correct_count[selected_module] += 1
195
 
196
- # Immediately disable Submit and enable New
197
- st.session_state.submit_enabled = False
198
- st.session_state.new_enabled = True
199
-
200
- # Display correct/incorrect feedback
201
- for option in current_question['options']:
202
- if option == current_question['correct_answer']:
203
- st.markdown(f"<span style='color:green;'>{option} ✅</span>", unsafe_allow_html=True)
204
- elif option == current_question['selected']:
205
- st.markdown(f"<span style='color:red;'>{option} ❌</span>", unsafe_allow_html=True)
206
- else:
207
- st.markdown(f"{option}")
208
-
209
- st.write(f"**Explanation:** {current_question['explanation']}")
210
- st.write("**Step-by-Step Solution:**")
211
- for step in current_question['step_by_step_solution']:
212
- st.write(step)
213
-
214
  else:
215
  st.warning("Please select an option before submitting.", icon="⚠️")
216
-
217
- # Handle new question generation
218
- if new_button:
219
- navigate_question("new")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/New"
 
 
28
 
29
  def reset_pdf_cache():
30
  st.session_state.pdf_data = None
 
107
  st.session_state.current_index -= 1
108
  elif direction == "next" and st.session_state.current_index < len(st.session_state.questions) - 1:
109
  st.session_state.current_index += 1
 
 
 
 
 
 
110
 
111
  # Load all modules dynamically
112
  modules = load_modules()
 
128
  st.session_state.module_question_count[selected_module] = 0
129
  st.session_state.module_correct_count[selected_module] = 0
130
  st.session_state.selected_answer = None
131
+ st.session_state.button_label = "Submit/New"
 
132
 
133
  # Load the current module's question
134
  current_question = st.session_state.questions[st.session_state.current_index]
 
159
  st.write(current_question["question"])
160
 
161
  # Create the form for the question
162
+ with st.form(key=f'question_form_{st.session_state.current_index}'):
163
+ selected_answer = st.radio(
164
+ "Choose an answer:",
165
+ options=current_question['options'],
166
+ key=f"question_{st.session_state.current_index}_options",
167
+ index=None # Ensure no option is pre-selected
168
+ )
169
+
170
+ submit_button = st.form_submit_button(label=st.session_state.button_label)
 
 
 
171
 
172
  # Handle button state and answer submission
173
+ if submit_button and st.session_state.button_label == "Submit/New":
174
  if selected_answer is not None:
175
  # Process the answer
176
  current_question['selected'] = selected_answer
 
181
  st.session_state.correct_count += 1
182
  st.session_state.module_correct_count[selected_module] += 1
183
 
184
+ st.session_state.button_label = "Next Question"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  else:
186
  st.warning("Please select an option before submitting.", icon="⚠️")
187
+ st.stop()
188
+
189
+ # Show correct/incorrect feedback after submission
190
+ if current_question.get('answered', False):
191
+ for option in current_question['options']:
192
+ if option == current_question['correct_answer']:
193
+ st.markdown(f"<span style='color:green;'>{option} ✅</span>", unsafe_allow_html=True)
194
+ elif option == current_question['selected']:
195
+ st.markdown(f"<span style='color:red;'>{option} ❌</span>", unsafe_allow_html=True)
196
+ else:
197
+ st.markdown(f"{option}")
198
+
199
+ st.write(f"**Explanation:** {current_question['explanation']}")
200
+ st.write("**Step-by-Step Solution:**")
201
+ for step in current_question['step_by_step_solution']:
202
+ st.write(step)
203
+
204
+ # Disable options and button when navigating through previous questions
205
+ if st.session_state.current_index < len(st.session_state.questions) - 1:
206
+ st.session_state.button_label = "Next Question"
207
+ else:
208
+ st.session_state.button_label = "Submit/New"
209
+
210
+ if st.session_state.current_index > 0:
211
+ st.session_state.button_label = "Next Question"
212
+ st.session_state.selected_answer = None
213
+ st.form_submit_button(label=st.session_state.button_label, disabled=True)
214
+ st.radio("Choose an answer:", options=current_question['options'], index=None, disabled=True)