Sina Media Lab commited on
Commit
a26033f
·
1 Parent(s): 337bde4
Files changed (1) hide show
  1. app.py +22 -15
app.py CHANGED
@@ -1,12 +1,8 @@
1
  import streamlit as st
2
  import importlib
3
- import logging
4
  from fpdf import FPDF
5
  import uuid
6
 
7
- # Configure logging
8
- logging.basicConfig(level=logging.INFO)
9
-
10
  # List of available modules with shorter names and icons
11
  module_names = {
12
  "Bases": "presentation_bases",
@@ -48,8 +44,6 @@ if 'module_correct_count' not in st.session_state:
48
  st.session_state.module_correct_count = {name: 0 for name in module_names}
49
  if 'module_question_count' not in st.session_state:
50
  st.session_state.module_question_count = {name: 0 for name in module_names}
51
- if 'selected_answer' not in st.session_state:
52
- st.session_state.selected_answer = None
53
  if 'pdf_data' not in st.session_state:
54
  st.session_state.pdf_data = None
55
 
@@ -78,7 +72,7 @@ def generate_pdf_report():
78
  question, options, selected, correct, explanation, step_by_step_solution = (
79
  entry['question'],
80
  entry['options'],
81
- entry['selected'] if entry['selected'] is not None else "Not Answered", # Handle case where selected is None
82
  entry['correct_answer'],
83
  entry['explanation'],
84
  entry['step_by_step_solution']
@@ -118,6 +112,9 @@ def generate_new_question(module_name):
118
  question_data['answered'] = False
119
  question_data['module'] = module_name # Add the module name to the question data
120
  question_data['selected'] = None # Initialize 'selected' to None
 
 
 
121
  return question_data
122
 
123
  def navigate_question(direction):
@@ -142,23 +139,33 @@ if module_name != st.session_state.current_module:
142
  st.session_state.questions = [generate_new_question(module_name)]
143
 
144
  # Load the current module's question
145
- current_question = st.session_state.questions[st.session_state.current_index]
 
 
 
 
 
146
 
147
  # Display module title and description
148
  st.title(module_name)
149
  st.write(module_descriptions.get(module_name, "No description available for this module."))
150
  st.write(current_question["question"])
151
 
152
- # Render options without pre-selection
153
- if not current_question.get('answered', False):
154
  selected_answer = st.radio(
155
  "Choose an answer:",
156
  options=current_question['options'],
157
- key=f"question_{st.session_state.current_index}",
158
  index=None # No pre-selection
159
  )
 
160
 
161
- if st.button("Submit"):
 
 
 
 
162
  current_question['selected'] = selected_answer
163
  current_question['answered'] = True
164
  st.session_state.module_question_count[module_name] += 1
@@ -176,7 +183,7 @@ if current_question.get('answered', False):
176
  st.markdown(f"<span style='color:red;'>{option} ❌</span>", unsafe_allow_html=True)
177
  else:
178
  st.markdown(f"{option}")
179
-
180
  # Show explanation and step-by-step solution
181
  st.write(f"**Explanation:** {current_question['explanation']}")
182
  st.write("**Step-by-Step Solution:**")
@@ -186,11 +193,11 @@ if current_question.get('answered', False):
186
  # Navigation buttons (placed after question and options)
187
  col1, col2 = st.columns([1, 1])
188
  with col1:
189
- if st.button("⬅️ Prev", disabled=st.session_state.current_index == 0):
190
  navigate_question("prev")
191
 
192
  with col2:
193
- if st.button("➡️ Next"):
194
  navigate_question("next")
195
 
196
  # PDF Download Button (at the bottom)
 
1
  import streamlit as st
2
  import importlib
 
3
  from fpdf import FPDF
4
  import uuid
5
 
 
 
 
6
  # List of available modules with shorter names and icons
7
  module_names = {
8
  "Bases": "presentation_bases",
 
44
  st.session_state.module_correct_count = {name: 0 for name in module_names}
45
  if 'module_question_count' not in st.session_state:
46
  st.session_state.module_question_count = {name: 0 for name in module_names}
 
 
47
  if 'pdf_data' not in st.session_state:
48
  st.session_state.pdf_data = None
49
 
 
72
  question, options, selected, correct, explanation, step_by_step_solution = (
73
  entry['question'],
74
  entry['options'],
75
+ entry['selected'] if entry['selected'] is not None else "Not Answered",
76
  entry['correct_answer'],
77
  entry['explanation'],
78
  entry['step_by_step_solution']
 
112
  question_data['answered'] = False
113
  question_data['module'] = module_name # Add the module name to the question data
114
  question_data['selected'] = None # Initialize 'selected' to None
115
+ # Ensure there are 5 options
116
+ if len(question_data['options']) != 5:
117
+ st.warning(f"Question does not have 5 options. Found {len(question_data['options'])}.")
118
  return question_data
119
 
120
  def navigate_question(direction):
 
139
  st.session_state.questions = [generate_new_question(module_name)]
140
 
141
  # Load the current module's question
142
+ if st.session_state.current_index < len(st.session_state.questions):
143
+ current_question = st.session_state.questions[st.session_state.current_index]
144
+ else:
145
+ current_question = generate_new_question(st.session_state.current_module)
146
+ st.session_state.questions.append(current_question)
147
+ st.session_state.current_index = len(st.session_state.questions) - 1
148
 
149
  # Display module title and description
150
  st.title(module_name)
151
  st.write(module_descriptions.get(module_name, "No description available for this module."))
152
  st.write(current_question["question"])
153
 
154
+ # Use a form to prevent rerun on option selection
155
+ with st.form(key=f'question_form_{st.session_state.current_index}'):
156
  selected_answer = st.radio(
157
  "Choose an answer:",
158
  options=current_question['options'],
159
+ key=f"question_{st.session_state.current_index}_options",
160
  index=None # No pre-selection
161
  )
162
+ submit_button = st.form_submit_button(label="Submit")
163
 
164
+ if submit_button and not current_question.get('answered', False):
165
+ if selected_answer is None:
166
+ st.warning("Please select an answer before submitting.")
167
+ else:
168
+ # Process the answer
169
  current_question['selected'] = selected_answer
170
  current_question['answered'] = True
171
  st.session_state.module_question_count[module_name] += 1
 
183
  st.markdown(f"<span style='color:red;'>{option} ❌</span>", unsafe_allow_html=True)
184
  else:
185
  st.markdown(f"{option}")
186
+
187
  # Show explanation and step-by-step solution
188
  st.write(f"**Explanation:** {current_question['explanation']}")
189
  st.write("**Step-by-Step Solution:**")
 
193
  # Navigation buttons (placed after question and options)
194
  col1, col2 = st.columns([1, 1])
195
  with col1:
196
+ if st.button("⬅️ Prev", key=f'prev_button_{st.session_state.current_index}', disabled=st.session_state.current_index == 0):
197
  navigate_question("prev")
198
 
199
  with col2:
200
+ if st.button("➡️ Next", key=f'next_button_{st.session_state.current_index}'):
201
  navigate_question("next")
202
 
203
  # PDF Download Button (at the bottom)