Sina Media Lab commited on
Commit
199933f
·
1 Parent(s): c5fe3bc
Files changed (1) hide show
  1. app.py +54 -52
app.py CHANGED
@@ -63,13 +63,12 @@ def generate_pdf_report():
63
 
64
  for entry in st.session_state.questions:
65
  if entry['module'] == module:
66
- question, options, selected, correct, explanation, step_by_step_solution = (
67
  entry['question'],
68
  entry['options'],
69
  entry['selected'],
70
- entry['correct_answer'],
71
- entry['explanation'],
72
- entry['step_by_step_solution']
73
  )
74
  pdf.multi_cell(0, 10, f"Q: {question}")
75
  for option in options:
@@ -84,10 +83,6 @@ def generate_pdf_report():
84
  pdf.multi_cell(0, 10, f" {option}")
85
  pdf.set_text_color(0, 0, 0) # Reset color
86
  pdf.multi_cell(0, 10, f"Explanation: {explanation}")
87
- pdf.ln(5)
88
- pdf.multi_cell(0, 10, "Step-by-Step Solution:")
89
- for step in step_by_step_solution:
90
- pdf.multi_cell(0, 10, step)
91
  pdf.ln(10)
92
 
93
  pdf.ln(10) # Add space after each module
@@ -101,10 +96,16 @@ def load_module(module_name):
101
 
102
  def generate_new_question(module_name):
103
  module = load_module(module_name)
104
- question_data = module.generate_question()
105
- # Ensure 'answered' is initialized to False
106
- question_data['answered'] = False
107
- return question_data
 
 
 
 
 
 
108
 
109
  def navigate_question(direction):
110
  if direction == "prev" and st.session_state.current_index > 0:
@@ -127,53 +128,20 @@ if module_name != st.session_state.current_module:
127
  st.session_state.current_index = 0
128
  st.session_state.questions = [generate_new_question(module_name)]
129
 
130
- # Load the current module's question
 
131
  current_question = st.session_state.questions[st.session_state.current_index]
132
 
133
  # Display module title and description
134
- st.title(module_name)
135
- st.write(current_question["question"])
136
 
137
- # Option highlighting logic
138
- def get_option_style(option):
139
- if current_question.get('answered', False):
140
- if option == current_question['correct_answer']:
141
- return "background-color:#d4edda;padding:10px;border-radius:5px;" # Green background for correct
142
- elif option == current_question['selected']:
143
- return "background-color:#f8d7da;padding:10px;border-radius:5px;" # Red background for incorrect
144
- return "background-color:#f0f0f0;padding:10px;border-radius:5px;" # Gray background by default
145
-
146
- # Display each option with custom styles
147
- selected_answer = None
148
- for i, option in enumerate(current_question['options']):
149
- option_html = f"<div style='{get_option_style(option)}'>{option}</div>"
150
- if st.button(option_html, key=f"option_{i}"):
151
- selected_answer = option
152
-
153
- if selected_answer:
154
- current_question['selected'] = selected_answer
155
- current_question['answered'] = True
156
- st.session_state.module_question_count[module_name] += 1
157
-
158
- if selected_answer == current_question['correct_answer']:
159
- st.session_state.correct_count += 1
160
- st.session_state.module_correct_count[module_name] += 1
161
-
162
- # Refresh the display after submission to show the correct/incorrect styles
163
- st.experimental_rerun()
164
-
165
- # Navigation buttons
166
  col1, col2, col3 = st.columns([1, 1, 2])
167
  with col1:
168
- if st.button("⬅️ Prev", disabled=st.session_state.current_index == 0):
169
- navigate_question("prev")
170
- st.experimental_rerun()
171
-
172
  with col2:
173
- if st.button("➡️ Next"):
174
- navigate_question("next")
175
- st.experimental_rerun()
176
-
177
  with col3:
178
  if len(st.session_state.questions) > 0:
179
  pdf = generate_pdf_report()
@@ -184,3 +152,37 @@ with col3:
184
  file_name="quiz_report.pdf",
185
  mime="application/pdf"
186
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  for entry in st.session_state.questions:
65
  if entry['module'] == module:
66
+ question, options, selected, correct, explanation = (
67
  entry['question'],
68
  entry['options'],
69
  entry['selected'],
70
+ entry['correct'],
71
+ entry['explanation']
 
72
  )
73
  pdf.multi_cell(0, 10, f"Q: {question}")
74
  for option in options:
 
83
  pdf.multi_cell(0, 10, f" {option}")
84
  pdf.set_text_color(0, 0, 0) # Reset color
85
  pdf.multi_cell(0, 10, f"Explanation: {explanation}")
 
 
 
 
86
  pdf.ln(10)
87
 
88
  pdf.ln(10) # Add space after each module
 
96
 
97
  def generate_new_question(module_name):
98
  module = load_module(module_name)
99
+ question, options, correct_answer, explanation = module.generate_question()
100
+ return {
101
+ 'module': module_name,
102
+ 'question': question,
103
+ 'options': options,
104
+ 'correct': correct_answer,
105
+ 'explanation': explanation,
106
+ 'selected': None,
107
+ 'answered': False
108
+ }
109
 
110
  def navigate_question(direction):
111
  if direction == "prev" and st.session_state.current_index > 0:
 
128
  st.session_state.current_index = 0
129
  st.session_state.questions = [generate_new_question(module_name)]
130
 
131
+ # Load the current module for title and description
132
+ current_module = load_module(st.session_state.current_module)
133
  current_question = st.session_state.questions[st.session_state.current_index]
134
 
135
  # Display module title and description
136
+ st.title(current_module.title)
137
+ st.write(current_module.description)
138
 
139
+ # Button Row: Prev, Next, and PDF Download
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  col1, col2, col3 = st.columns([1, 1, 2])
141
  with col1:
142
+ st.button("⬅️ Prev", disabled=st.session_state.current_index == 0, on_click=lambda: navigate_question("prev"))
 
 
 
143
  with col2:
144
+ st.button("➡️ Next", on_click=lambda: navigate_question("next"))
 
 
 
145
  with col3:
146
  if len(st.session_state.questions) > 0:
147
  pdf = generate_pdf_report()
 
152
  file_name="quiz_report.pdf",
153
  mime="application/pdf"
154
  )
155
+
156
+ # Display the current question
157
+ st.write(f"**Question {st.session_state.current_index + 1}:** {current_question['question']}")
158
+
159
+ # Option highlighting logic
160
+ def get_option_style(option):
161
+ if current_question['answered']:
162
+ if option == current_question['correct']:
163
+ return "background-color:#d4edda;padding:10px;border-radius:5px;" # Green background for correct
164
+ elif option == current_question['selected']:
165
+ return "background-color:#f8d7da;padding:10px;border-radius:5px;" # Red background for incorrect
166
+ return "background-color:#f0f0f0;padding:10px;border-radius:5px;" # Gray background by default
167
+
168
+ selected_answer = st.radio(
169
+ "Choose an answer:",
170
+ current_question['options'],
171
+ index=current_question['options'].index(current_question['selected']) if current_question['selected'] else None,
172
+ key=st.session_state.current_index,
173
+ format_func=lambda x: f"{x}"
174
+ )
175
+
176
+ if st.button("Submit"):
177
+ if not current_question['answered']:
178
+ current_question['selected'] = selected_answer
179
+ current_question['answered'] = True
180
+ st.session_state.module_question_count[module_name] += 1
181
+
182
+ if selected_answer == current_question['correct']:
183
+ st.session_state.correct_count += 1
184
+ st.session_state.module_correct_count[module_name] += 1
185
+
186
+ # Retain and highlight the options after submission
187
+ for option in current_question['options']:
188
+ st.markdown(f"<div style='{get_option_style(option)}'>{option}</div>", unsafe_allow_html=True)