Sina Media Lab commited on
Commit
bf6b2ac
Β·
1 Parent(s): a743789
Files changed (1) hide show
  1. app.py +66 -58
app.py CHANGED
@@ -23,12 +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 = False
28
- if 'new_enabled' not in st.session_state:
29
- st.session_state.new_enabled = False
30
- if 'action' not in st.session_state:
31
- st.session_state.action = None
32
 
33
  def reset_pdf_cache():
34
  st.session_state.pdf_data = None
@@ -102,9 +98,8 @@ def generate_new_question(module_name, module):
102
  question_data['answered'] = False
103
  question_data['module'] = module_name
104
  question_data['selected'] = None
105
- st.session_state.submit_enabled = False # Disable submit initially
106
- st.session_state.new_enabled = False # Disable new initially
107
- st.session_state.action = None # Reset action
108
  return question_data
109
 
110
  def navigate_question(direction):
@@ -137,6 +132,7 @@ if selected_module != st.session_state.current_module:
137
  st.session_state.module_question_count[selected_module] = 0
138
  st.session_state.module_correct_count[selected_module] = 0
139
  st.session_state.selected_answer = None
 
140
 
141
  # Load the current module's question
142
  current_question = st.session_state.questions[st.session_state.current_index]
@@ -166,7 +162,7 @@ with col3:
166
 
167
  st.write(current_question["question"])
168
 
169
- # Create the form for the question
170
  selected_answer = st.radio(
171
  "Choose an answer:",
172
  options=current_question['options'],
@@ -174,52 +170,64 @@ selected_answer = st.radio(
174
  index=None # Ensure no option is pre-selected
175
  )
176
 
177
- # Enable submit if an option is selected
178
  if selected_answer:
179
- st.session_state.submit_enabled = True
180
-
181
- # Show Submit/New actions as disabled if no option is selected
182
- action = st.radio(
183
- "Choose an action:",
184
- ("Submit", "New"),
185
- index=None,
186
- key="action_radio",
187
- disabled=(not st.session_state.submit_enabled and not st.session_state.new_enabled),
188
- horizontal=True
189
- )
190
-
191
- # Handle the selected action
192
- if action == "Submit" and st.session_state.submit_enabled:
193
- # Process the answer
194
- current_question['selected'] = selected_answer
195
- current_question['answered'] = True
196
- st.session_state.module_question_count[selected_module] += 1
197
-
198
- if selected_answer == current_question['correct_answer']:
199
- st.session_state.correct_count += 1
200
- st.session_state.module_correct_count[selected_module] += 1
201
-
202
- # Display correct/incorrect feedback
203
- for option in current_question['options']:
204
- if option == current_question['correct_answer']:
205
- st.markdown(f"<span style='color:green;'>{option} βœ…</span>", unsafe_allow_html=True)
206
- elif option == current_question['selected']:
207
- st.markdown(f"<span style='color:red;'>{option} ❌</span>", unsafe_allow_html=True)
208
- else:
209
- st.markdown(f"{option}")
210
-
211
- st.write(f"**Explanation:** {current_question['explanation']}")
212
- st.write("**Step-by-Step Solution:**")
213
- for step in current_question['step_by_step_solution']:
214
- st.write(step)
215
-
216
- # Disable submit and enable new
217
- st.session_state.submit_enabled = False
218
- st.session_state.new_enabled = True
219
- st.session_state.action = "New"
220
-
221
- elif action == "New" and st.session_state.new_enabled:
222
- navigate_question("new")
223
- st.session_state.submit_enabled = False
224
- st.session_state.new_enabled = False
225
- st.session_state.action = None
 
 
 
 
 
 
 
 
 
 
 
 
 
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 'state' not in st.session_state:
27
+ st.session_state.state = "NewQuestion" # Initial state is NewQuestion
 
 
 
 
28
 
29
  def reset_pdf_cache():
30
  st.session_state.pdf_data = None
 
98
  question_data['answered'] = False
99
  question_data['module'] = module_name
100
  question_data['selected'] = None
101
+ st.session_state.selected_answer = None
102
+ st.session_state.state = "NewQuestion" # Set state to NewQuestion
 
103
  return question_data
104
 
105
  def navigate_question(direction):
 
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.state = "NewQuestion"
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
+ # Display the question options
166
  selected_answer = st.radio(
167
  "Choose an answer:",
168
  options=current_question['options'],
 
170
  index=None # Ensure no option is pre-selected
171
  )
172
 
173
+ # Transition to State 2 if an option is selected
174
  if selected_answer:
175
+ st.session_state.selected_answer = selected_answer
176
+ st.session_state.state = "OptionSelected"
177
+
178
+ # Display Submit/New actions based on state
179
+ if st.session_state.state == "NewQuestion":
180
+ action = st.radio(
181
+ "Choose an action:",
182
+ ("Submit", "New"),
183
+ index=None, # Do not pre-select any action
184
+ key="action_radio_new",
185
+ horizontal=True
186
+ )
187
+
188
+ # Handle Submit/New actions
189
+ if action == "Submit":
190
+ st.warning("Please select an option before submitting.", icon="⚠️")
191
+ st.session_state.action = None # Reset action
192
+ elif action == "New":
193
+ navigate_question("new")
194
+
195
+ elif st.session_state.state == "OptionSelected":
196
+ action = st.radio(
197
+ "Choose an action:",
198
+ ("Submit", "New"),
199
+ index=None, # Do not pre-select any action
200
+ key="action_radio_submit",
201
+ horizontal=True
202
+ )
203
+
204
+ # Handle Submit/New actions
205
+ if action == "Submit":
206
+ # Process the answer
207
+ current_question['selected'] = st.session_state.selected_answer
208
+ current_question['answered'] = True
209
+ st.session_state.module_question_count[selected_module] += 1
210
+
211
+ if st.session_state.selected_answer == current_question['correct_answer']:
212
+ st.session_state.correct_count += 1
213
+ st.session_state.module_correct_count[selected_module] += 1
214
+
215
+ # Display correct/incorrect feedback
216
+ for option in current_question['options']:
217
+ if option == current_question['correct_answer']:
218
+ st.markdown(f"<span style='color:green;'>{option} βœ…</span>", unsafe_allow_html=True)
219
+ elif option == current_question['selected']:
220
+ st.markdown(f"<span style='color:red;'>{option} ❌</span>", unsafe_allow_html=True)
221
+ else:
222
+ st.markdown(f"{option}")
223
+
224
+ st.write(f"**Explanation:** {current_question['explanation']}")
225
+ st.write("**Step-by-Step Solution:**")
226
+ for step in current_question['step_by_step_solution']:
227
+ st.write(step)
228
+
229
+ st.session_state.state = "NewQuestion" # Transition back to NewQuestion after submission
230
+
231
+ elif action == "New":
232
+ navigate_question("new")
233
+ st.session_state.state = "NewQuestion" # Transition to NewQuestion