Sina Media Lab
commited on
Commit
·
8a51ec9
1
Parent(s):
4f6051a
Updates
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ if 'session_id' not in st.session_state:
|
|
8 |
st.session_state.session_id = str(uuid.uuid4())
|
9 |
|
10 |
if 'questions' not in st.session_state:
|
11 |
-
st.session_state.questions = []
|
12 |
if 'current_index' not in st.session_state:
|
13 |
st.session_state.current_index = 0
|
14 |
if 'current_module' not in st.session_state:
|
@@ -25,8 +25,8 @@ 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 '
|
29 |
-
st.session_state.
|
30 |
|
31 |
def reset_pdf_cache():
|
32 |
st.session_state.pdf_data = None
|
@@ -105,9 +105,6 @@ def generate_new_question(module_name, module):
|
|
105 |
if len(question_data['options']) != 4:
|
106 |
st.warning(f"Question in module '{module_name}' does not have 4 options. Found {len(question_data['options'])}.")
|
107 |
|
108 |
-
# Reset the Submit/Next click counter when generating a new question
|
109 |
-
st.session_state.submit_click_count = 0
|
110 |
-
|
111 |
return question_data
|
112 |
|
113 |
def navigate_question(direction):
|
@@ -131,17 +128,20 @@ for module in modules:
|
|
131 |
selected_module = module
|
132 |
break
|
133 |
|
|
|
134 |
if selected_module != st.session_state.current_module:
|
135 |
st.session_state.current_module = selected_module
|
136 |
st.session_state.current_index = 0
|
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 |
|
143 |
# Load the current module's question
|
144 |
-
|
|
|
|
|
|
|
145 |
|
146 |
# Display module title and description
|
147 |
st.title(modules[selected_module]["title"])
|
@@ -153,8 +153,12 @@ with col1:
|
|
153 |
if st.button("⬅️ Prev", disabled=st.session_state.current_index == 0):
|
154 |
navigate_question("prev")
|
155 |
with col2:
|
156 |
-
if st.button("➡️ Next"
|
157 |
-
|
|
|
|
|
|
|
|
|
158 |
with col3:
|
159 |
if len(st.session_state.questions) > 0:
|
160 |
pdf = generate_pdf_report()
|
@@ -168,12 +172,6 @@ with col3:
|
|
168 |
|
169 |
st.write(current_question["question"])
|
170 |
|
171 |
-
# Display the Submit/Next click count
|
172 |
-
st.write(f"Submit/Next button clicked {st.session_state.submit_click_count} time(s).")
|
173 |
-
|
174 |
-
# Determine the button label based on the click count
|
175 |
-
button_label = "Next" if st.session_state.submit_click_count > 1 else "Submit/Next"
|
176 |
-
|
177 |
# Create the form for the question
|
178 |
with st.form(key=f'question_form_{st.session_state.current_index}'):
|
179 |
selected_answer = st.radio(
|
@@ -183,16 +181,13 @@ with st.form(key=f'question_form_{st.session_state.current_index}'):
|
|
183 |
key=f"question_{st.session_state.current_index}_options"
|
184 |
)
|
185 |
|
186 |
-
submit_button = st.form_submit_button(label=
|
187 |
|
188 |
# Handle button state and answer submission
|
189 |
if submit_button:
|
190 |
if selected_answer is None:
|
191 |
st.warning("Please select an option before submitting.")
|
192 |
else:
|
193 |
-
# Increment the click counter as soon as the button is clicked
|
194 |
-
st.session_state.submit_click_count += 1
|
195 |
-
|
196 |
if not current_question['answered']:
|
197 |
# Process the answer
|
198 |
current_question['selected'] = selected_answer
|
@@ -207,17 +202,8 @@ if submit_button:
|
|
207 |
st.session_state.questions[st.session_state.current_index]['answered'] = True
|
208 |
st.session_state.selected_answer = selected_answer
|
209 |
|
210 |
-
else:
|
211 |
-
# If already answered, move to the next question
|
212 |
-
new_question = generate_new_question(selected_module, modules[selected_module])
|
213 |
-
st.session_state.questions.append(new_question)
|
214 |
-
st.session_state.current_index = len(st.session_state.questions) - 1
|
215 |
-
st.session_state.answered = False
|
216 |
-
|
217 |
# Show correct/incorrect feedback after submission
|
218 |
if current_question.get('answered', False):
|
219 |
-
st.session_state.submit_click_count += 1 # Increment click count when showing the solution
|
220 |
-
|
221 |
for option in current_question['options']:
|
222 |
if option == current_question['correct_answer']:
|
223 |
st.markdown(f"<span style='color:green;'>{option} ✅</span>", unsafe_allow_html=True)
|
|
|
8 |
st.session_state.session_id = str(uuid.uuid4())
|
9 |
|
10 |
if 'questions' not in st.session_state:
|
11 |
+
st.session_state.questions = [] # To store generated questions
|
12 |
if 'current_index' not in st.session_state:
|
13 |
st.session_state.current_index = 0
|
14 |
if 'current_module' 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 'question_list' not in st.session_state:
|
29 |
+
st.session_state.question_list = [None] * 100 # Placeholder for 100 questions
|
30 |
|
31 |
def reset_pdf_cache():
|
32 |
st.session_state.pdf_data = None
|
|
|
105 |
if len(question_data['options']) != 4:
|
106 |
st.warning(f"Question in module '{module_name}' does not have 4 options. Found {len(question_data['options'])}.")
|
107 |
|
|
|
|
|
|
|
108 |
return question_data
|
109 |
|
110 |
def navigate_question(direction):
|
|
|
128 |
selected_module = module
|
129 |
break
|
130 |
|
131 |
+
# Ensure the current module is tracked correctly
|
132 |
if selected_module != st.session_state.current_module:
|
133 |
st.session_state.current_module = selected_module
|
134 |
st.session_state.current_index = 0
|
|
|
135 |
st.session_state.module_question_count[selected_module] = 0
|
136 |
st.session_state.module_correct_count[selected_module] = 0
|
137 |
st.session_state.selected_answer = None
|
138 |
st.session_state.answered = False
|
139 |
|
140 |
# Load the current module's question
|
141 |
+
if st.session_state.question_list[st.session_state.current_index] is None:
|
142 |
+
st.session_state.question_list[st.session_state.current_index] = generate_new_question(selected_module, modules[selected_module])
|
143 |
+
|
144 |
+
current_question = st.session_state.question_list[st.session_state.current_index]
|
145 |
|
146 |
# Display module title and description
|
147 |
st.title(modules[selected_module]["title"])
|
|
|
153 |
if st.button("⬅️ Prev", disabled=st.session_state.current_index == 0):
|
154 |
navigate_question("prev")
|
155 |
with col2:
|
156 |
+
if st.button("➡️ Next"):
|
157 |
+
if st.session_state.current_index < len(st.session_state.question_list) - 1:
|
158 |
+
st.session_state.current_index += 1
|
159 |
+
if st.session_state.question_list[st.session_state.current_index] is None:
|
160 |
+
st.session_state.question_list[st.session_state.current_index] = generate_new_question(selected_module, modules[selected_module])
|
161 |
+
st.session_state.answered = False
|
162 |
with col3:
|
163 |
if len(st.session_state.questions) > 0:
|
164 |
pdf = generate_pdf_report()
|
|
|
172 |
|
173 |
st.write(current_question["question"])
|
174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
# Create the form for the question
|
176 |
with st.form(key=f'question_form_{st.session_state.current_index}'):
|
177 |
selected_answer = st.radio(
|
|
|
181 |
key=f"question_{st.session_state.current_index}_options"
|
182 |
)
|
183 |
|
184 |
+
submit_button = st.form_submit_button(label="Submit")
|
185 |
|
186 |
# Handle button state and answer submission
|
187 |
if submit_button:
|
188 |
if selected_answer is None:
|
189 |
st.warning("Please select an option before submitting.")
|
190 |
else:
|
|
|
|
|
|
|
191 |
if not current_question['answered']:
|
192 |
# Process the answer
|
193 |
current_question['selected'] = selected_answer
|
|
|
202 |
st.session_state.questions[st.session_state.current_index]['answered'] = True
|
203 |
st.session_state.selected_answer = selected_answer
|
204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
# Show correct/incorrect feedback after submission
|
206 |
if current_question.get('answered', False):
|
|
|
|
|
207 |
for option in current_question['options']:
|
208 |
if option == current_question['correct_answer']:
|
209 |
st.markdown(f"<span style='color:green;'>{option} ✅</span>", unsafe_allow_html=True)
|