Sina Media Lab commited on
Commit
92fe395
·
1 Parent(s): 700a11e
Files changed (1) hide show
  1. app.py +28 -42
app.py CHANGED
@@ -46,7 +46,7 @@ def generate_pdf_report():
46
 
47
  # Module Title
48
  pdf.set_font("Arial", style='B', size=10)
49
- pdf.multi_cell(0, 10, f"Module: {entry['module']}", border=1, fill=True)
50
 
51
  # Question
52
  pdf.set_font("Arial", style='B', size=10)
@@ -81,7 +81,7 @@ def load_modules():
81
  # Dynamically import the module only when needed
82
  module = __import__(f"{module_dir}.{module_name}", fromlist=[''])
83
  modules[module_name] = {
84
- "title": getattr(module, "title", module_name),
85
  "description": getattr(module, "description", "No description available."),
86
  "generate_question": module.generate_question # Access the generate_question function
87
  }
@@ -92,18 +92,13 @@ def generate_new_question(module_name, module):
92
  # Ensure 'answered' is initialized to False and add the 'module' and 'selected' keys
93
  question_data['answered'] = False
94
  question_data['module'] = module_name # Add the module name to the question data
 
95
  question_data['selected'] = None # Initialize 'selected' to None
96
  # Ensure there are exactly 4 options
97
  if len(question_data['options']) != 4:
98
  st.warning(f"Question in module '{module_name}' does not have 4 options. Found {len(question_data['options'])}.")
99
  return question_data
100
 
101
- def navigate_question(direction):
102
- if direction == "prev" and st.session_state.current_index > 0:
103
- st.session_state.current_index -= 1
104
- elif direction == "next" and st.session_state.current_index < len(st.session_state.questions) - 1:
105
- st.session_state.current_index += 1
106
-
107
  # Load all modules dynamically
108
  modules = load_modules()
109
 
@@ -120,38 +115,17 @@ for module in modules:
120
  if selected_module != st.session_state.current_module:
121
  st.session_state.current_module = selected_module
122
  st.session_state.current_index = len(st.session_state.questions) # Continue numbering from previous questions
123
- st.session_state.questions.append(generate_new_question(selected_module, modules[selected_module]))
124
- st.session_state.module_question_count[selected_module] = st.session_state.module_question_count.get(selected_module, 0)
125
- st.session_state.module_correct_count[selected_module] = st.session_state.module_correct_count.get(selected_module, 0)
126
  st.session_state.selected_answer = None
127
  st.session_state.button_label = "Submit/New"
 
 
128
 
129
- # Load the current module's question
130
- current_question = st.session_state.questions[st.session_state.current_index]
131
 
132
  # Display module title and description with smaller header and font
133
  st.markdown(f"### {modules[selected_module]['title']}")
134
  st.markdown(f"<span style='font-size: 12px;'>{modules[selected_module]['description']}</span>", unsafe_allow_html=True)
135
 
136
- # Navigation and PDF report buttons
137
- col1, col2, col3 = st.columns([1, 1, 2])
138
- with col1:
139
- if st.button("⬅️ Prev", disabled=st.session_state.current_index == 0):
140
- navigate_question("prev")
141
- with col2:
142
- if st.button("➡️ Next", disabled=st.session_state.current_index >= len(st.session_state.questions) - 1):
143
- navigate_question("next")
144
- with col3:
145
- if any(q['answered'] for q in st.session_state.questions):
146
- pdf = generate_pdf_report()
147
- st.session_state.pdf_data = pdf # Reset PDF cache
148
- st.download_button(
149
- label="Download PDF Report 📄",
150
- data=st.session_state.pdf_data,
151
- file_name="quiz_report.pdf",
152
- mime="application/pdf"
153
- )
154
-
155
  # Display the current question with larger font
156
  st.markdown(f"<span style='font-size: 18px;'><b>Q{st.session_state.current_index + 1}: {current_question['question']}</b></span>", unsafe_allow_html=True)
157
 
@@ -162,11 +136,10 @@ with st.form(key=f'question_form_{st.session_state.current_index}'):
162
  "Choose an answer:",
163
  options=[f"{options[i]}. {opt}" for i, opt in enumerate(current_question['options'])],
164
  key=f"question_{st.session_state.current_index}_options",
165
- index=None if not current_question['answered'] else current_question['options'].index(current_question['selected']),
166
- disabled=current_question['answered']
167
  )
168
 
169
- submit_button = st.form_submit_button(label="Submit/New", disabled=current_question['answered'])
170
 
171
  # Handle button state and answer submission
172
  if submit_button:
@@ -186,7 +159,7 @@ if submit_button:
186
  # Show correct/incorrect feedback, explanation, and step-by-step solution
187
  st.markdown(
188
  """
189
- <div style='background-color:#f0f0f0; padding: 10px; border-radius: 10px;'>
190
  """, unsafe_allow_html=True)
191
  for i, option in enumerate(current_question['options']):
192
  option_text = f"{options[i]}. {option}"
@@ -197,13 +170,26 @@ if submit_button:
197
  else:
198
  st.markdown(f"{option_text}", unsafe_allow_html=True)
199
 
200
- st.write(f"**Explanation:** {current_question['explanation']}")
201
- st.write("**Step-by-Step Solution:**")
202
  for step in current_question['step_by_step_solution']:
203
- st.write(step)
204
  st.markdown("</div>", unsafe_allow_html=True)
205
 
 
 
 
 
206
  # Generate a new question after submission
207
- new_question = generate_new_question(selected_module, modules[selected_module])
208
- st.session_state.questions.append(new_question)
209
- st.session_state.current_index = len(st.session_state.questions) - 1
 
 
 
 
 
 
 
 
 
 
46
 
47
  # Module Title
48
  pdf.set_font("Arial", style='B', size=10)
49
+ pdf.multi_cell(0, 10, f"Module: {entry['module_title']}", border=1, fill=True)
50
 
51
  # Question
52
  pdf.set_font("Arial", style='B', size=10)
 
81
  # Dynamically import the module only when needed
82
  module = __import__(f"{module_dir}.{module_name}", fromlist=[''])
83
  modules[module_name] = {
84
+ "title": getattr(module, "title", module_name.replace("_", " ").title()),
85
  "description": getattr(module, "description", "No description available."),
86
  "generate_question": module.generate_question # Access the generate_question function
87
  }
 
92
  # Ensure 'answered' is initialized to False and add the 'module' and 'selected' keys
93
  question_data['answered'] = False
94
  question_data['module'] = module_name # Add the module name to the question data
95
+ question_data['module_title'] = module['title'] # Add the module title to the question data
96
  question_data['selected'] = None # Initialize 'selected' to None
97
  # Ensure there are exactly 4 options
98
  if len(question_data['options']) != 4:
99
  st.warning(f"Question in module '{module_name}' does not have 4 options. Found {len(question_data['options'])}.")
100
  return question_data
101
 
 
 
 
 
 
 
102
  # Load all modules dynamically
103
  modules = load_modules()
104
 
 
115
  if selected_module != st.session_state.current_module:
116
  st.session_state.current_module = selected_module
117
  st.session_state.current_index = len(st.session_state.questions) # Continue numbering from previous questions
 
 
 
118
  st.session_state.selected_answer = None
119
  st.session_state.button_label = "Submit/New"
120
+ # Generate a new question without adding it to the answered list yet
121
+ st.session_state.current_question = generate_new_question(selected_module, modules[selected_module])
122
 
123
+ current_question = st.session_state.current_question
 
124
 
125
  # Display module title and description with smaller header and font
126
  st.markdown(f"### {modules[selected_module]['title']}")
127
  st.markdown(f"<span style='font-size: 12px;'>{modules[selected_module]['description']}</span>", unsafe_allow_html=True)
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  # Display the current question with larger font
130
  st.markdown(f"<span style='font-size: 18px;'><b>Q{st.session_state.current_index + 1}: {current_question['question']}</b></span>", unsafe_allow_html=True)
131
 
 
136
  "Choose an answer:",
137
  options=[f"{options[i]}. {opt}" for i, opt in enumerate(current_question['options'])],
138
  key=f"question_{st.session_state.current_index}_options",
139
+ index=None,
 
140
  )
141
 
142
+ submit_button = st.form_submit_button(label="Submit/New")
143
 
144
  # Handle button state and answer submission
145
  if submit_button:
 
159
  # Show correct/incorrect feedback, explanation, and step-by-step solution
160
  st.markdown(
161
  """
162
+ <div style='padding: 10px;'>
163
  """, unsafe_allow_html=True)
164
  for i, option in enumerate(current_question['options']):
165
  option_text = f"{options[i]}. {option}"
 
170
  else:
171
  st.markdown(f"{option_text}", unsafe_allow_html=True)
172
 
173
+ st.markdown(f"<span style='font-size: 12px;'><b>Explanation:</b> {current_question['explanation']}</span>", unsafe_allow_html=True)
174
+ st.markdown(f"<span style='font-size: 12px;'><b>Step-by-Step Solution:</b></span>", unsafe_allow_html=True)
175
  for step in current_question['step_by_step_solution']:
176
+ st.markdown(f"<span style='font-size: 12px;'>{step}</span>", unsafe_allow_html=True)
177
  st.markdown("</div>", unsafe_allow_html=True)
178
 
179
+ # Add the question to the answered list only after submission
180
+ st.session_state.questions.append(current_question)
181
+ st.session_state.current_index = len(st.session_state.questions)
182
+
183
  # Generate a new question after submission
184
+ st.session_state.current_question = generate_new_question(selected_module, modules[selected_module])
185
+
186
+ # Generate PDF report only if there is at least one answered question
187
+ if any(q['answered'] for q in st.session_state.questions):
188
+ pdf = generate_pdf_report()
189
+ st.session_state.pdf_data = pdf # Reset PDF cache
190
+ st.download_button(
191
+ label="Download PDF Report 📄",
192
+ data=st.session_state.pdf_data,
193
+ file_name="quiz_report.pdf",
194
+ mime="application/pdf"
195
+ )