Sina Media Lab commited on
Commit
5866ecf
·
1 Parent(s): 976f4d1
Files changed (1) hide show
  1. app.py +49 -99
app.py CHANGED
@@ -21,16 +21,8 @@ if 'module_question_count' not in st.session_state:
21
  st.session_state.module_question_count = {}
22
  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
- 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
- if 'show_report' not in st.session_state:
33
- st.session_state.show_report = False
34
 
35
  def reset_pdf_cache():
36
  st.session_state.pdf_data = None
@@ -113,10 +105,12 @@ def generate_new_question(module_name, module):
113
  def navigate_question(direction):
114
  if direction == "prev" and st.session_state.current_index > 0:
115
  st.session_state.current_index -= 1
116
- st.session_state.answered = True
117
- elif direction == "next" and st.session_state.current_index < len(st.session_state.questions) - 1:
118
- st.session_state.current_index += 1
119
- st.session_state.answered = True
 
 
120
 
121
  # Load all modules dynamically
122
  modules = load_modules()
@@ -137,11 +131,8 @@ if selected_module != st.session_state.current_module:
137
  st.session_state.questions = [generate_new_question(selected_module, modules[selected_module])]
138
  st.session_state.module_question_count[selected_module] = 0
139
  st.session_state.module_correct_count[selected_module] = 0
 
140
  st.session_state.selected_answer = None
141
- st.session_state.answered = False
142
- st.session_state.submit_click_count = 0 # Reset the submit click count
143
- st.session_state.show_next_button = False # Reset the next button visibility
144
- st.session_state.show_report = False # Reset the report visibility
145
 
146
  # Load the current module's question
147
  current_question = st.session_state.questions[st.session_state.current_index]
@@ -156,7 +147,7 @@ with col1:
156
  if st.button("⬅️ Prev", disabled=st.session_state.current_index == 0):
157
  navigate_question("prev")
158
  with col2:
159
- if st.button("➡️ Next", disabled=st.session_state.current_index >= len(st.session_state.questions) - 1):
160
  navigate_question("next")
161
  with col3:
162
  if len(st.session_state.questions) > 0:
@@ -169,84 +160,43 @@ with col3:
169
  mime="application/pdf"
170
  )
171
 
172
- # Show the question only if the "Next" button hasn't been clicked yet
173
- if not st.session_state.show_next_button:
174
- st.write(current_question["question"])
175
-
176
- # Create the form for the question
177
- with st.form(key=f'question_form_{st.session_state.current_index}'):
178
- selected_answer = st.radio(
179
- "Choose an answer:",
180
- options=current_question['options'],
181
- index=current_question['options'].index(current_question['selected']) if current_question['answered'] else None,
182
- key=f"question_{st.session_state.current_index}_options"
183
- )
184
-
185
- submit_button = st.form_submit_button(label="Submit/Next")
186
-
187
- # Handle button state and answer submission
188
- if submit_button:
189
- if selected_answer is None:
190
- st.warning("Please select an option before submitting.")
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  else:
192
- st.session_state.submit_click_count += 1
193
-
194
- if st.session_state.submit_click_count == 1:
195
- # First click: show solution and remove question
196
- if not current_question['answered']:
197
- if selected_answer is not None:
198
- # Process the answer
199
- current_question['selected'] = selected_answer
200
- current_question['answered'] = True
201
- st.session_state.module_question_count[selected_module] += 1
202
-
203
- if selected_answer == current_question['correct_answer']:
204
- st.session_state.correct_count += 1
205
- st.session_state.module_correct_count[selected_module] += 1
206
-
207
- # Set answered to true to disable options
208
- st.session_state.questions[st.session_state.current_index]['answered'] = True
209
- st.session_state.selected_answer = selected_answer
210
-
211
- # Show correct/incorrect feedback after submission
212
- for option in current_question['options']:
213
- if option == current_question['correct_answer']:
214
- st.markdown(f"<span style='color:green;'>{option} ✅</span>", unsafe_allow_html=True)
215
- elif option == current_question['selected']:
216
- st.markdown(f"<span style='color:red;'>{option} ❌</span>", unsafe_allow_html=True)
217
- else:
218
- st.markdown(f"{option}")
219
-
220
- # Show explanation and step-by-step solution with improved visuals
221
- st.write(f"**Explanation:** {current_question['explanation']}")
222
- st.markdown("""
223
- <div style='border: 2px solid #4CAF50; padding: 10px; margin-top: 20px; background-color: #f9f9f9;'>
224
- <h4 style='color: #4CAF50;'>Step-by-Step Solution:</h4>
225
- <ol>
226
- """, unsafe_allow_html=True)
227
-
228
- for step in current_question['step_by_step_solution']:
229
- st.markdown(f"<li style='margin-bottom: 10px;'>{step}</li>", unsafe_allow_html=True)
230
-
231
- st.markdown("</ol></div>", unsafe_allow_html=True)
232
-
233
- # Replace question and Submit/Next with Next button
234
- st.session_state.show_next_button = True
235
-
236
- # Show the Next button to generate a new question
237
- if st.session_state.show_next_button:
238
- if st.button("Next"):
239
- st.session_state.show_report = True
240
- st.session_state.show_next_button = False # Hide the Next button
241
-
242
- # Show the report after clicking Next
243
- if st.session_state.show_report:
244
- st.write("### Report")
245
- for module in modules:
246
- total_questions = st.session_state.module_question_count.get(module, 0)
247
- correct_answers = st.session_state.module_correct_count.get(module, 0)
248
- if total_questions > 0:
249
- percentage = (correct_answers / total_questions) * 100
250
- st.write(f"**{modules[module]['title']}**: {correct_answers}/{total_questions} correct ({percentage:.2f}%)")
251
- else:
252
- st.write(f"**{modules[module]['title']}**: No questions answered yet.")
 
21
  st.session_state.module_question_count = {}
22
  if 'pdf_data' not in st.session_state:
23
  st.session_state.pdf_data = None
24
+ if 'next_disabled' not in st.session_state:
25
+ st.session_state.next_disabled = True
 
 
 
 
 
 
 
 
26
 
27
  def reset_pdf_cache():
28
  st.session_state.pdf_data = None
 
105
  def navigate_question(direction):
106
  if direction == "prev" and st.session_state.current_index > 0:
107
  st.session_state.current_index -= 1
108
+ elif direction == "next":
109
+ new_question = generate_new_question(st.session_state.current_module, modules[st.session_state.current_module])
110
+ st.session_state.questions.append(new_question)
111
+ st.session_state.current_index = len(st.session_state.questions) - 1
112
+ st.session_state.next_disabled = True
113
+ st.session_state.selected_answer = None
114
 
115
  # Load all modules dynamically
116
  modules = load_modules()
 
131
  st.session_state.questions = [generate_new_question(selected_module, modules[selected_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.next_disabled = True
135
  st.session_state.selected_answer = None
 
 
 
 
136
 
137
  # Load the current module's question
138
  current_question = st.session_state.questions[st.session_state.current_index]
 
147
  if st.button("⬅️ Prev", disabled=st.session_state.current_index == 0):
148
  navigate_question("prev")
149
  with col2:
150
+ if st.button("➡️ Next", disabled=st.session_state.next_disabled):
151
  navigate_question("next")
152
  with col3:
153
  if len(st.session_state.questions) > 0:
 
160
  mime="application/pdf"
161
  )
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
+ )
172
+ # Enable submit button when an answer is selected
173
+ submit_button = st.form_submit_button(label="Submit", disabled=selected_answer is None)
174
+
175
+ # Handle button state and answer submission
176
+ if submit_button and selected_answer:
177
+ # Process the answer
178
+ current_question['selected'] = selected_answer
179
+ current_question['answered'] = True
180
+ st.session_state.module_question_count[selected_module] += 1
181
+
182
+ if selected_answer == current_question['correct_answer']:
183
+ st.session_state.correct_count += 1
184
+ st.session_state.module_correct_count[selected_module] += 1
185
+
186
+ st.session_state.next_disabled = False
187
+
188
+ # Show correct/incorrect feedback after submission
189
+ if current_question.get('answered', False):
190
+ for option in current_question['options']:
191
+ if option == current_question['correct_answer']:
192
+ st.markdown(f"<span style='color:green;'>{option} ✅</span>", unsafe_allow_html=True)
193
+ elif option == current_question['selected']:
194
+ st.markdown(f"<span style='color:red;'>{option} ❌</span>", unsafe_allow_html=True)
195
  else:
196
+ st.markdown(f"{option}")
197
+
198
+ # Show explanation and step-by-step solution
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)