Sina Media Lab commited on
Commit
15d45d1
·
1 Parent(s): b5d0d2c
Files changed (1) hide show
  1. app.py +33 -21
app.py CHANGED
@@ -43,19 +43,27 @@ def generate_pdf_report():
43
  pdf.set_fill_color(245, 245, 245) # Light gray
44
  else:
45
  pdf.set_fill_color(255, 255, 255) # White
46
-
 
 
 
 
 
 
47
  pdf.multi_cell(0, 8, f"Q{i+1}: {entry['question']}", border=1, fill=True)
48
 
49
- for option in entry['options']:
 
 
 
50
  if option == entry['correct_answer']:
51
  pdf.set_text_color(0, 128, 0) # Green for correct
52
- pdf.multi_cell(0, 8, f"{option}", border=1, fill=True)
53
  elif option == entry['selected']:
54
  pdf.set_text_color(255, 0, 0) # Red for incorrect
55
- pdf.multi_cell(0, 8, f"{option}", border=1, fill=True)
56
  else:
57
  pdf.set_text_color(0, 0, 0) # Default color for others
58
- pdf.multi_cell(0, 8, f" {option}", border=1, fill=True)
 
59
 
60
  pdf.set_text_color(0, 0, 0) # Reset color
61
  pdf.multi_cell(0, 8, f"Explanation: {entry['explanation']}", border=1, fill=True)
@@ -121,9 +129,9 @@ if selected_module != st.session_state.current_module:
121
  # Load the current module's question
122
  current_question = st.session_state.questions[st.session_state.current_index]
123
 
124
- # Display module title and description
125
- st.title(modules[selected_module]["title"])
126
- st.write(modules[selected_module]["description"])
127
 
128
  # Navigation and PDF report buttons
129
  col1, col2, col3 = st.columns([1, 1, 2])
@@ -134,7 +142,7 @@ with col2:
134
  if st.button("➡️ Next", disabled=st.session_state.current_index >= len(st.session_state.questions) - 1):
135
  navigate_question("next")
136
  with col3:
137
- if len(st.session_state.questions) > 0:
138
  pdf = generate_pdf_report()
139
  st.session_state.pdf_data = pdf # Reset PDF cache
140
  st.download_button(
@@ -144,19 +152,21 @@ with col3:
144
  mime="application/pdf"
145
  )
146
 
147
- # Display the current question
148
- st.markdown(f"<span style='font-size: 12px;'><b>Q{st.session_state.current_index + 1}: {current_question['question']}</b></span>", unsafe_allow_html=True)
149
 
150
  # Create the form for the question
151
  with st.form(key=f'question_form_{st.session_state.current_index}'):
 
152
  selected_answer = st.radio(
153
  "Choose an answer:",
154
- options=current_question['options'],
155
  key=f"question_{st.session_state.current_index}_options",
156
- index=None # Ensure no option is pre-selected
 
157
  )
158
 
159
- submit_button = st.form_submit_button(label="Submit/New")
160
 
161
  # Handle button state and answer submission
162
  if submit_button:
@@ -164,23 +174,25 @@ if submit_button:
164
  st.warning("Please select an option before submitting.", icon="⚠️")
165
  else:
166
  # Process the answer
167
- current_question['selected'] = selected_answer
 
168
  current_question['answered'] = True
169
  st.session_state.module_question_count[selected_module] += 1
170
 
171
- if selected_answer == current_question['correct_answer']:
172
  st.session_state.correct_count += 1
173
  st.session_state.module_correct_count[selected_module] += 1
174
 
175
  # Show correct/incorrect feedback, explanation, and step-by-step solution
176
  st.markdown("<div style='background-color:#f0f0f0; padding: 10px;'>", unsafe_allow_html=True)
177
- for option in current_question['options']:
 
178
  if option == current_question['correct_answer']:
179
- st.markdown(f"<span style='color:green;'>{option} ✅</span>", unsafe_allow_html=True)
180
- elif option == current_question['selected']:
181
- st.markdown(f"<span style='color:red;'>{option} ❌</span>", unsafe_allow_html=True)
182
  else:
183
- st.markdown(f"{option}", unsafe_allow_html=True)
184
 
185
  st.write(f"**Explanation:** {current_question['explanation']}")
186
  st.write("**Step-by-Step Solution:**")
 
43
  pdf.set_fill_color(245, 245, 245) # Light gray
44
  else:
45
  pdf.set_fill_color(255, 255, 255) # White
46
+
47
+ # Module Title
48
+ pdf.set_font("Arial", style='B', size=8)
49
+ pdf.multi_cell(0, 8, f"Module: {entry['module']}", border=1, fill=True)
50
+
51
+ # Question
52
+ pdf.set_font("Arial", style='B', size=8)
53
  pdf.multi_cell(0, 8, f"Q{i+1}: {entry['question']}", border=1, fill=True)
54
 
55
+ # Options
56
+ pdf.set_font("Arial", size=8)
57
+ options = ['a', 'b', 'c', 'd']
58
+ for j, option in enumerate(entry['options']):
59
  if option == entry['correct_answer']:
60
  pdf.set_text_color(0, 128, 0) # Green for correct
 
61
  elif option == entry['selected']:
62
  pdf.set_text_color(255, 0, 0) # Red for incorrect
 
63
  else:
64
  pdf.set_text_color(0, 0, 0) # Default color for others
65
+
66
+ pdf.multi_cell(0, 8, f"{options[j]}. {option}", border=1, fill=True)
67
 
68
  pdf.set_text_color(0, 0, 0) # Reset color
69
  pdf.multi_cell(0, 8, f"Explanation: {entry['explanation']}", border=1, fill=True)
 
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 font
133
+ st.markdown(f"<span style='font-size: 12px;'><b>{modules[selected_module]['title']}</b></span>", unsafe_allow_html=True)
134
+ st.markdown(f"<span style='font-size: 10px;'>{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])
 
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(
 
152
  mime="application/pdf"
153
  )
154
 
155
+ # Display the current question with larger font
156
+ st.markdown(f"<span style='font-size: 14px;'><b>Q{st.session_state.current_index + 1}: {current_question['question']}</b></span>", unsafe_allow_html=True)
157
 
158
  # Create the form for the question
159
  with st.form(key=f'question_form_{st.session_state.current_index}'):
160
+ options = ['a', 'b', 'c', 'd']
161
  selected_answer = st.radio(
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:
 
174
  st.warning("Please select an option before submitting.", icon="⚠️")
175
  else:
176
  # Process the answer
177
+ selected_answer_text = selected_answer.split(". ", 1)[1] # Extract the text part
178
+ current_question['selected'] = selected_answer_text
179
  current_question['answered'] = True
180
  st.session_state.module_question_count[selected_module] += 1
181
 
182
+ if selected_answer_text == current_question['correct_answer']:
183
  st.session_state.correct_count += 1
184
  st.session_state.module_correct_count[selected_module] += 1
185
 
186
  # Show correct/incorrect feedback, explanation, and step-by-step solution
187
  st.markdown("<div style='background-color:#f0f0f0; padding: 10px;'>", unsafe_allow_html=True)
188
+ for i, option in enumerate(current_question['options']):
189
+ option_text = f"{options[i]}. {option}"
190
  if option == current_question['correct_answer']:
191
+ st.markdown(f"<span style='color:green;'>{option_text} ✅</span>", unsafe_allow_html=True)
192
+ elif option == selected_answer_text:
193
+ st.markdown(f"<span style='color:red;'>{option_text} ❌</span>", unsafe_allow_html=True)
194
  else:
195
+ st.markdown(f"{option_text}", unsafe_allow_html=True)
196
 
197
  st.write(f"**Explanation:** {current_question['explanation']}")
198
  st.write("**Step-by-Step Solution:**")