Sina Media Lab commited on
Commit
ee53acf
·
1 Parent(s): 169cec0
Files changed (1) hide show
  1. app.py +31 -47
app.py CHANGED
@@ -84,17 +84,6 @@ def generate_pdf_report():
84
 
85
  return pdf.output(dest='S').encode('latin1', 'replace')
86
 
87
- # Logging for debugging
88
- logging.info(f"Current Module: {st.session_state.current_module}")
89
- logging.info(f"Question Count: {st.session_state.question_count}")
90
- logging.info(f"Correct Count: {st.session_state.correct_count}")
91
- logging.info(f"Submitted: {st.session_state.submitted}")
92
- logging.info(f"Current Question: {st.session_state.current_question}")
93
- logging.info(f"Options: {st.session_state.options}")
94
- logging.info(f"Selected Answer: {st.session_state.selected_answer}")
95
- logging.info(f"Correct Answer: {st.session_state.correct_answer}")
96
- logging.info(f"Explanation: {st.session_state.explanation}")
97
-
98
  # Streamlit interface
99
  st.sidebar.title("Quiz Modules")
100
  module_name = st.sidebar.radio("Choose a module:", list(module_names.keys()), index=0)
@@ -107,7 +96,6 @@ if module_name:
107
  st.session_state.question_queue = []
108
  st.session_state.current_question = None
109
  st.session_state.submitted = False
110
- logging.info("Module changed. Resetting session state.")
111
 
112
  module_file = module_names[module_name]
113
  try:
@@ -120,46 +108,35 @@ if module_name:
120
  st.title(title)
121
  st.write(description)
122
 
123
- # Handle question generation and submission
124
- if st.session_state.current_question is None or st.session_state.submitted:
125
- st.session_state.current_question, st.session_state.options, st.session_state.correct_answer, st.session_state.explanation = generate_question()
126
- st.session_state.submitted = False
127
- st.session_state.selected_answer = None
128
- logging.info("Generated new question.")
129
-
130
- if st.session_state.question_count > 0:
131
- correct_percentage = (st.session_state.correct_count / st.session_state.question_count) * 100
132
- st.write(f"**Correct Answers:** {st.session_state.correct_count}/{st.session_state.question_count} ({correct_percentage:.2f}%)")
133
-
134
  # Button Row: Prev, Next, and PDF Download
135
  col1, col2, col3 = st.columns([1, 1, 2])
136
  with col1:
137
- if st.session_state.question_count > 1:
138
- if st.button("⬅️ Prev"):
139
- st.session_state.question_count -= 1
140
- st.session_state.submitted = False
141
- question_data = st.session_state.question_queue[st.session_state.question_count - 1]
142
- st.session_state.current_question = question_data['question']
143
- st.session_state.options = question_data['options']
144
- st.session_state.selected_answer = question_data['selected']
145
- st.session_state.correct_answer = question_data['correct']
146
- st.session_state.explanation = question_data['explanation']
147
- logging.info("Navigated to previous question.")
148
  with col2:
149
- if st.session_state.submitted:
150
- if st.button("➡️ Next"):
151
- st.session_state.current_question = None
152
- logging.info("Navigated to next question.")
153
  with col3:
154
- if st.session_state.question_count > 0:
 
155
  pdf = generate_pdf_report()
156
  st.download_button(
157
  label="Download PDF Report 📄",
158
  data=pdf,
159
  file_name="quiz_report.pdf",
160
- mime="application/pdf"
 
161
  )
162
- logging.info("PDF report generated.")
 
 
 
 
 
 
 
 
 
163
 
164
  st.write(f"**Question {st.session_state.question_count + 1}:** {st.session_state.current_question}")
165
  st.session_state.selected_answer = st.radio("Choose an answer:", st.session_state.options, key=st.session_state.question_count)
@@ -184,10 +161,6 @@ if module_name:
184
  'explanation': st.session_state.explanation
185
  })
186
 
187
- logging.info("Answer submitted.")
188
- logging.info(f"Selected Answer: {st.session_state.selected_answer}")
189
- logging.info(f"Correct Answer: {st.session_state.correct_answer}")
190
-
191
  # Explanation with proper width
192
  st.markdown(
193
  f"""
@@ -200,7 +173,18 @@ if module_name:
200
 
201
  except ModuleNotFoundError as e:
202
  st.error(f"The module '{module_name}' was not found. Please select another module.")
203
- logging.error(f"Error: {e}")
204
  except Exception as e:
205
  st.error(f"An unexpected error occurred: {str(e)}")
206
- logging.error(f"Error: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  return pdf.output(dest='S').encode('latin1', 'replace')
86
 
 
 
 
 
 
 
 
 
 
 
 
87
  # Streamlit interface
88
  st.sidebar.title("Quiz Modules")
89
  module_name = st.sidebar.radio("Choose a module:", list(module_names.keys()), index=0)
 
96
  st.session_state.question_queue = []
97
  st.session_state.current_question = None
98
  st.session_state.submitted = False
 
99
 
100
  module_file = module_names[module_name]
101
  try:
 
108
  st.title(title)
109
  st.write(description)
110
 
 
 
 
 
 
 
 
 
 
 
 
111
  # Button Row: Prev, Next, and PDF Download
112
  col1, col2, col3 = st.columns([1, 1, 2])
113
  with col1:
114
+ prev_disabled = st.session_state.question_count <= 1
115
+ st.button("⬅️ Prev", disabled=prev_disabled, key='prev_button', on_click=lambda: navigate_question("prev"))
 
 
 
 
 
 
 
 
 
116
  with col2:
117
+ next_disabled = not st.session_state.submitted
118
+ st.button("➡️ Next", disabled=next_disabled, key='next_button', on_click=lambda: navigate_question("next"))
 
 
119
  with col3:
120
+ pdf_disabled = st.session_state.question_count == 0
121
+ if not pdf_disabled:
122
  pdf = generate_pdf_report()
123
  st.download_button(
124
  label="Download PDF Report 📄",
125
  data=pdf,
126
  file_name="quiz_report.pdf",
127
+ mime="application/pdf",
128
+ disabled=pdf_disabled
129
  )
130
+
131
+ # Handle question generation and submission
132
+ if st.session_state.current_question is None or st.session_state.submitted:
133
+ st.session_state.current_question, st.session_state.options, st.session_state.correct_answer, st.session_state.explanation = generate_question()
134
+ st.session_state.submitted = False
135
+ st.session_state.selected_answer = None
136
+
137
+ if st.session_state.question_count > 0:
138
+ correct_percentage = (st.session_state.correct_count / st.session_state.question_count) * 100
139
+ st.write(f"**Correct Answers:** {st.session_state.correct_count}/{st.session_state.question_count} ({correct_percentage:.2f}%)")
140
 
141
  st.write(f"**Question {st.session_state.question_count + 1}:** {st.session_state.current_question}")
142
  st.session_state.selected_answer = st.radio("Choose an answer:", st.session_state.options, key=st.session_state.question_count)
 
161
  'explanation': st.session_state.explanation
162
  })
163
 
 
 
 
 
164
  # Explanation with proper width
165
  st.markdown(
166
  f"""
 
173
 
174
  except ModuleNotFoundError as e:
175
  st.error(f"The module '{module_name}' was not found. Please select another module.")
 
176
  except Exception as e:
177
  st.error(f"An unexpected error occurred: {str(e)}")
178
+
179
+ def navigate_question(direction):
180
+ if direction == "prev" and st.session_state.question_count > 1:
181
+ st.session_state.question_count -= 1
182
+ st.session_state.submitted = False
183
+ question_data = st.session_state.question_queue[st.session_state.question_count - 1]
184
+ st.session_state.current_question = question_data['question']
185
+ st.session_state.options = question_data['options']
186
+ st.session_state.selected_answer = question_data['selected']
187
+ st.session_state.correct_answer = question_data['correct']
188
+ st.session_state.explanation = question_data['explanation']
189
+ elif direction == "next" and st.session_state.submitted:
190
+ st.session_state.current_question = None