Sina Media Lab commited on
Commit
e0f928e
·
1 Parent(s): 9d5f38a
Files changed (1) hide show
  1. app.py +26 -10
app.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
  import importlib
3
  import logging
4
  from fpdf import FPDF
 
5
 
6
  # Configure logging
7
  logging.basicConfig(level=logging.INFO)
@@ -18,6 +19,10 @@ module_names = {
18
  "Subtraction": "subtraction_bases",
19
  }
20
 
 
 
 
 
21
  # Initialize session state variables
22
  if 'questions' not in st.session_state:
23
  st.session_state.questions = []
@@ -121,9 +126,7 @@ module_name = st.sidebar.radio("Choose a module:", list(module_names.keys()), in
121
  if module_name != st.session_state.current_module:
122
  st.session_state.current_module = module_name
123
  st.session_state.current_index = 0
124
-
125
- # Generate the first question for the new module if it's a new module
126
- st.session_state.questions.append(generate_new_question(module_name))
127
 
128
  # Load the current module for title and description
129
  current_module = load_module(st.session_state.current_module)
@@ -153,7 +156,20 @@ with col3:
153
  # Display the current question
154
  st.write(f"**Question {st.session_state.current_index + 1}:** {current_question['question']}")
155
 
156
- selected_answer = st.radio("Choose an answer:", current_question['options'], index=current_question['options'].index(current_question['selected']) if current_question['selected'] else None, key=st.session_state.current_index)
 
 
 
 
 
 
 
 
 
 
 
 
 
157
 
158
  if st.button("Submit"):
159
  if not current_question['answered']:
@@ -165,12 +181,12 @@ if st.button("Submit"):
165
  st.session_state.correct_count += 1
166
  st.session_state.module_correct_count[module_name] += 1
167
 
168
- # Highlight the correct and incorrect answers
169
- if current_question['answered']:
170
- for option in current_question['options']:
171
  if option == current_question['correct']:
172
- st.markdown(f"<div style='background-color:#d4edda;padding:10px;border-radius:5px;'>{option}</div>", unsafe_allow_html=True)
173
  elif option == current_question['selected']:
174
- st.markdown(f"<div style='background-color:#f8d7da;padding:10px;border-radius:5px;'>{option}</div>", unsafe_allow_html=True)
175
  else:
176
- st.markdown(f"<div style='padding:10px;border-radius:5px;'>{option}</div>", unsafe_allow_html=True)
 
2
  import importlib
3
  import logging
4
  from fpdf import FPDF
5
+ import uuid
6
 
7
  # Configure logging
8
  logging.basicConfig(level=logging.INFO)
 
19
  "Subtraction": "subtraction_bases",
20
  }
21
 
22
+ # Initialize unique session state
23
+ if 'session_id' not in st.session_state:
24
+ st.session_state.session_id = str(uuid.uuid4())
25
+
26
  # Initialize session state variables
27
  if 'questions' not in st.session_state:
28
  st.session_state.questions = []
 
126
  if module_name != st.session_state.current_module:
127
  st.session_state.current_module = module_name
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)
 
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
+ option_styles = {
161
+ 'correct': "background-color:#d4edda;padding:10px;border-radius:5px;",
162
+ 'incorrect': "background-color:#f8d7da;padding:10px;border-radius:5px;",
163
+ 'default': "padding:10px;border-radius:5px;"
164
+ }
165
+
166
+ selected_answer = st.radio(
167
+ "Choose an answer:",
168
+ current_question['options'],
169
+ index=current_question['options'].index(current_question['selected']) if current_question['selected'] else None,
170
+ key=st.session_state.current_index,
171
+ format_func=lambda x: f"{x}"
172
+ )
173
 
174
  if st.button("Submit"):
175
  if not current_question['answered']:
 
181
  st.session_state.correct_count += 1
182
  st.session_state.module_correct_count[module_name] += 1
183
 
184
+ # Retain and highlight the options after submission
185
+ for option in current_question['options']:
186
+ if current_question['answered']:
187
  if option == current_question['correct']:
188
+ st.markdown(f"<div style='{option_styles['correct']}'>{option}</div>", unsafe_allow_html=True)
189
  elif option == current_question['selected']:
190
+ st.markdown(f"<div style='{option_styles['incorrect']}'>{option}</div>", unsafe_allow_html=True)
191
  else:
192
+ st.markdown(f"<div style='{option_styles['default']}'>{option}</div>", unsafe_allow_html=True)