Sina Media Lab
commited on
Commit
·
b09d39c
1
Parent(s):
46a3abd
Updates
Browse files
app.py
CHANGED
@@ -9,8 +9,6 @@ if 'session_id' not in st.session_state:
|
|
9 |
|
10 |
if 'questions' not in st.session_state:
|
11 |
st.session_state.questions = []
|
12 |
-
if 'answered_questions' not in st.session_state:
|
13 |
-
st.session_state.answered_questions = []
|
14 |
if 'current_index' not in st.session_state:
|
15 |
st.session_state.current_index = 0
|
16 |
if 'current_module' not in st.session_state:
|
@@ -25,8 +23,8 @@ if 'pdf_data' not in st.session_state:
|
|
25 |
st.session_state.pdf_data = None
|
26 |
if 'selected_answer' not in st.session_state:
|
27 |
st.session_state.selected_answer = None
|
28 |
-
if '
|
29 |
-
st.session_state.
|
30 |
|
31 |
def reset_pdf_cache():
|
32 |
st.session_state.pdf_data = None
|
@@ -47,7 +45,7 @@ def generate_pdf_report():
|
|
47 |
pdf.cell(200, 10, txt=f"Correct Answers: {correct_count}/{total_count}", ln=True, align="L")
|
48 |
pdf.ln(5)
|
49 |
|
50 |
-
for entry in st.session_state.
|
51 |
if entry['module'] == module:
|
52 |
question, options, selected, correct, explanation, step_by_step_solution = (
|
53 |
entry['question'],
|
@@ -101,20 +99,19 @@ def generate_new_question(module_name, module):
|
|
101 |
question_data['module'] = module_name
|
102 |
question_data['selected'] = None
|
103 |
st.session_state.selected_answer = None
|
104 |
-
st.session_state.
|
105 |
return question_data
|
106 |
|
107 |
def navigate_question(direction):
|
108 |
if direction == "prev" and st.session_state.current_index > 0:
|
109 |
st.session_state.current_index -= 1
|
110 |
-
elif direction == "next" and st.session_state.current_index < len(st.session_state.
|
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 |
-
#
|
117 |
-
st.session_state.refresh = not st.session_state.get('refresh', False)
|
118 |
|
119 |
# Load all modules dynamically
|
120 |
modules = load_modules()
|
@@ -136,7 +133,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.
|
140 |
|
141 |
# Load the current module's question
|
142 |
current_question = st.session_state.questions[st.session_state.current_index]
|
@@ -145,38 +142,16 @@ current_question = st.session_state.questions[st.session_state.current_index]
|
|
145 |
st.title(modules[selected_module]["title"])
|
146 |
st.write(modules[selected_module]["description"])
|
147 |
|
148 |
-
# Disable options and submit if the question is answered
|
149 |
-
option_disabled = current_question['answered']
|
150 |
-
|
151 |
-
# Display the question options
|
152 |
-
selected_answer = st.radio(
|
153 |
-
"Choose an answer:",
|
154 |
-
options=current_question['options'],
|
155 |
-
index=current_question['options'].index(current_question['selected']) if current_question['answered'] else None,
|
156 |
-
disabled=option_disabled,
|
157 |
-
key=f"question_{st.session_state.current_index}_options"
|
158 |
-
)
|
159 |
-
|
160 |
-
# Radio buttons for selecting the action
|
161 |
-
action = st.radio(
|
162 |
-
"Choose an action:",
|
163 |
-
("Submit", "New"),
|
164 |
-
index=None, # Do not pre-select any action
|
165 |
-
key="action_radio",
|
166 |
-
horizontal=True,
|
167 |
-
disabled=option_disabled and (st.session_state.action != "New") # Disable submit when the question is already answered, but not "New"
|
168 |
-
)
|
169 |
-
|
170 |
# Navigation and PDF report buttons
|
171 |
col1, col2, col3 = st.columns([1, 1, 2])
|
172 |
with col1:
|
173 |
if st.button("⬅️ Prev", disabled=st.session_state.current_index == 0):
|
174 |
navigate_question("prev")
|
175 |
with col2:
|
176 |
-
if st.button("➡️ Next", disabled=st.session_state.current_index >= len(st.session_state.
|
177 |
navigate_question("next")
|
178 |
with col3:
|
179 |
-
if len(st.session_state.
|
180 |
pdf = generate_pdf_report()
|
181 |
st.session_state.pdf_data = pdf # Reset PDF cache
|
182 |
st.download_button(
|
@@ -188,38 +163,72 @@ with col3:
|
|
188 |
|
189 |
st.write(current_question["question"])
|
190 |
|
191 |
-
#
|
192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
if action == "Submit":
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
st.session_state.
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
st.write(f"**Explanation:** {current_question['explanation']}")
|
219 |
-
st.write("**Step-by-Step Solution:**")
|
220 |
-
for step in current_question['step_by_step_solution']:
|
221 |
-
st.write(step)
|
222 |
|
223 |
elif action == "New":
|
224 |
navigate_question("new")
|
225 |
-
st.session_state.
|
|
|
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:
|
|
|
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
|
|
|
45 |
pdf.cell(200, 10, txt=f"Correct Answers: {correct_count}/{total_count}", ln=True, align="L")
|
46 |
pdf.ln(5)
|
47 |
|
48 |
+
for entry in st.session_state.questions:
|
49 |
if entry['module'] == module:
|
50 |
question, options, selected, correct, explanation, step_by_step_solution = (
|
51 |
entry['question'],
|
|
|
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):
|
106 |
if direction == "prev" and st.session_state.current_index > 0:
|
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 |
elif direction == "new":
|
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 |
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]
|
|
|
142 |
st.title(modules[selected_module]["title"])
|
143 |
st.write(modules[selected_module]["description"])
|
144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
# Navigation and PDF report buttons
|
146 |
col1, col2, col3 = st.columns([1, 1, 2])
|
147 |
with col1:
|
148 |
if st.button("⬅️ Prev", disabled=st.session_state.current_index == 0):
|
149 |
navigate_question("prev")
|
150 |
with col2:
|
151 |
+
if st.button("➡️ Next", disabled=st.session_state.current_index >= len(st.session_state.questions) - 1):
|
152 |
navigate_question("next")
|
153 |
with col3:
|
154 |
+
if len(st.session_state.questions) > 0:
|
155 |
pdf = generate_pdf_report()
|
156 |
st.session_state.pdf_data = pdf # Reset PDF cache
|
157 |
st.download_button(
|
|
|
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'],
|
170 |
+
key=f"question_{st.session_state.current_index}_options",
|
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']:
|
219 |
+
st.markdown(f"<span style='color:green;'>{option} ✅</span>", unsafe_allow_html=True)
|
220 |
+
elif option == current_question['selected']:
|
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
|