Sina Media Lab commited on
Commit
b2acba8
·
1 Parent(s): b09d39c
Files changed (1) hide show
  1. app.py +32 -47
app.py CHANGED
@@ -23,8 +23,10 @@ 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 '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,8 +100,8 @@ def generate_new_question(module_name, module):
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):
@@ -111,7 +113,8 @@ def navigate_question(direction):
111
  new_question = generate_new_question(st.session_state.current_module, modules[st.session_state.current_module])
112
  st.session_state.questions.append(new_question)
113
  st.session_state.current_index = len(st.session_state.questions) - 1
114
- # No need to call rerun, Streamlit will automatically update the UI
 
115
 
116
  # Load all modules dynamically
117
  modules = load_modules()
@@ -133,7 +136,8 @@ if selected_module != st.session_state.current_module:
133
  st.session_state.module_question_count[selected_module] = 0
134
  st.session_state.module_correct_count[selected_module] = 0
135
  st.session_state.selected_answer = None
136
- st.session_state.state = "NewQuestion"
 
137
 
138
  # Load the current module's question
139
  current_question = st.session_state.questions[st.session_state.current_index]
@@ -163,7 +167,7 @@ with col3:
163
 
164
  st.write(current_question["question"])
165
 
166
- # Display the question options
167
  selected_answer = st.radio(
168
  "Choose an answer:",
169
  options=current_question['options'],
@@ -171,48 +175,28 @@ selected_answer = st.radio(
171
  index=None # Ensure no option is pre-selected
172
  )
173
 
174
- # Transition to State 2 if an option is selected
175
- if selected_answer:
176
- st.session_state.selected_answer = selected_answer
177
- st.session_state.state = "OptionSelected"
178
-
179
- # Display Submit/New actions based on state
180
- if st.session_state.state == "NewQuestion":
181
- action = st.radio(
182
- "Choose an action:",
183
- ("Submit", "New"),
184
- index=None, # Do not pre-select any action
185
- key="action_radio_new",
186
- horizontal=True
187
- )
188
-
189
- # Handle Submit/New actions
190
- if action == "Submit":
191
- st.warning("Please select an option before submitting.", icon="⚠️")
192
- st.session_state.action = None # Reset action
193
- elif action == "New":
194
- navigate_question("new")
195
-
196
- elif st.session_state.state == "OptionSelected":
197
- action = st.radio(
198
- "Choose an action:",
199
- ("Submit", "New"),
200
- index=None, # Do not pre-select any action
201
- key="action_radio_submit",
202
- horizontal=True
203
- )
204
-
205
- # Handle Submit/New actions
206
- if action == "Submit":
207
  # Process the answer
208
- current_question['selected'] = st.session_state.selected_answer
209
  current_question['answered'] = True
210
  st.session_state.module_question_count[selected_module] += 1
211
 
212
- if st.session_state.selected_answer == current_question['correct_answer']:
213
  st.session_state.correct_count += 1
214
  st.session_state.module_correct_count[selected_module] += 1
215
 
 
 
 
 
216
  # Display correct/incorrect feedback
217
  for option in current_question['options']:
218
  if option == current_question['correct_answer']:
@@ -221,14 +205,15 @@ elif st.session_state.state == "OptionSelected":
221
  st.markdown(f"<span style='color:red;'>{option} ❌</span>", unsafe_allow_html=True)
222
  else:
223
  st.markdown(f"{option}")
224
-
225
  st.write(f"**Explanation:** {current_question['explanation']}")
226
  st.write("**Step-by-Step Solution:**")
227
  for step in current_question['step_by_step_solution']:
228
  st.write(step)
229
 
230
- st.session_state.state = "NewQuestion" # Transition back to NewQuestion after submission
 
231
 
232
- elif action == "New":
233
- navigate_question("new")
234
- st.session_state.state = "NewQuestion" # Transition to NewQuestion
 
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
 
100
  question_data['answered'] = False
101
  question_data['module'] = module_name
102
  question_data['selected'] = None
103
+ if len(question_data['options']) != 4:
104
+ st.warning(f"Question in module '{module_name}' does not have 4 options. Found {len(question_data['options'])}.")
105
  return question_data
106
 
107
  def navigate_question(direction):
 
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
  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]
 
167
 
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'],
 
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
189
  current_question['answered'] = True
190
  st.session_state.module_question_count[selected_module] += 1
191
 
192
+ if selected_answer == current_question['correct_answer']:
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']:
 
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")