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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
import time
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
# Set page configuration
|
| 8 |
+
st.set_page_config(
|
| 9 |
+
page_title="Interactive Quiz App",
|
| 10 |
+
page_icon="π"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Custom CSS for styling
|
| 14 |
+
def load_css():
|
| 15 |
+
st.markdown("""
|
| 16 |
+
<style>
|
| 17 |
+
.stButton button {
|
| 18 |
+
width: 100%;
|
| 19 |
+
border-radius: 5px;
|
| 20 |
+
height: 3em;
|
| 21 |
+
background-color: #4CAF50;
|
| 22 |
+
color: white;
|
| 23 |
+
}
|
| 24 |
+
.correct-answer {
|
| 25 |
+
color: green;
|
| 26 |
+
font-weight: bold;
|
| 27 |
+
}
|
| 28 |
+
.wrong-answer {
|
| 29 |
+
color: red;
|
| 30 |
+
font-weight: bold;
|
| 31 |
+
}
|
| 32 |
+
.question-card {
|
| 33 |
+
background-color: #f5f5f5;
|
| 34 |
+
padding: 20px;
|
| 35 |
+
border-radius: 10px;
|
| 36 |
+
margin: 10px 0;
|
| 37 |
+
}
|
| 38 |
+
</style>
|
| 39 |
+
""", unsafe_allow_html=True)
|
| 40 |
+
|
| 41 |
+
# Load questions from JSON files
|
| 42 |
+
def load_questions():
|
| 43 |
+
questions_data = {}
|
| 44 |
+
data_folder = Path("data")
|
| 45 |
+
|
| 46 |
+
if not data_folder.exists():
|
| 47 |
+
st.error("Data folder not found! Please create a 'data' folder with subject JSON files.")
|
| 48 |
+
return {}
|
| 49 |
+
|
| 50 |
+
for file in data_folder.glob("*.json"):
|
| 51 |
+
with open(file, 'r') as f:
|
| 52 |
+
data = json.load(f)
|
| 53 |
+
questions_data[data["subject"]] = data["questions"]
|
| 54 |
+
|
| 55 |
+
return questions_data
|
| 56 |
+
|
| 57 |
+
# Initialize session state
|
| 58 |
+
def init_session_state():
|
| 59 |
+
if 'current_mode' not in st.session_state:
|
| 60 |
+
st.session_state.current_mode = 'Practice'
|
| 61 |
+
if 'test_start_time' not in st.session_state:
|
| 62 |
+
st.session_state.test_start_time = None
|
| 63 |
+
if 'test_answers' not in st.session_state:
|
| 64 |
+
st.session_state.test_answers = {}
|
| 65 |
+
if 'test_score' not in st.session_state:
|
| 66 |
+
st.session_state.test_score = 0
|
| 67 |
+
if 'test_completed' not in st.session_state:
|
| 68 |
+
st.session_state.test_completed = False
|
| 69 |
+
|
| 70 |
+
# Landing page
|
| 71 |
+
def show_landing_page(subjects):
|
| 72 |
+
st.title("π Interactive Quiz Application")
|
| 73 |
+
st.write("Welcome to the Interactive Quiz App! Choose a subject to begin your learning journey.")
|
| 74 |
+
|
| 75 |
+
cols = st.columns(len(subjects))
|
| 76 |
+
for idx, subject in enumerate(subjects):
|
| 77 |
+
with cols[idx]:
|
| 78 |
+
if st.button(subject, key=f"subject_{idx}"):
|
| 79 |
+
st.session_state.selected_subject = subject
|
| 80 |
+
st.session_state.page = 'subject'
|
| 81 |
+
st.experimental_rerun()
|
| 82 |
+
|
| 83 |
+
# Practice mode
|
| 84 |
+
def practice_mode(questions, selected_week=None):
|
| 85 |
+
if selected_week:
|
| 86 |
+
questions = [q for q in questions if q['week'] == selected_week]
|
| 87 |
+
|
| 88 |
+
for idx, question in enumerate(questions):
|
| 89 |
+
with st.container():
|
| 90 |
+
st.markdown(f"### Question {idx + 1}")
|
| 91 |
+
st.write(question['question'])
|
| 92 |
+
|
| 93 |
+
options = [
|
| 94 |
+
question['option1'],
|
| 95 |
+
question['option2'],
|
| 96 |
+
question['option3'],
|
| 97 |
+
question['option4']
|
| 98 |
+
]
|
| 99 |
+
|
| 100 |
+
user_answer = st.radio(
|
| 101 |
+
"Choose your answer:",
|
| 102 |
+
options,
|
| 103 |
+
key=f"practice_{idx}",
|
| 104 |
+
index=None # Set index to None to start with no selection
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
if user_answer:
|
| 108 |
+
correct_answer = options[question['correct'] - 1]
|
| 109 |
+
if user_answer == correct_answer:
|
| 110 |
+
st.success("Correct! β
")
|
| 111 |
+
else:
|
| 112 |
+
st.error(f"Incorrect! The correct answer is: {correct_answer}")
|
| 113 |
+
|
| 114 |
+
st.markdown("---")
|
| 115 |
+
|
| 116 |
+
# Test mode
|
| 117 |
+
def test_mode(questions, selected_week=None):
|
| 118 |
+
if not st.session_state.test_start_time:
|
| 119 |
+
st.session_state.test_start_time = time.time()
|
| 120 |
+
|
| 121 |
+
if selected_week:
|
| 122 |
+
questions = [q for q in questions if q['week'] == selected_week]
|
| 123 |
+
|
| 124 |
+
test_questions = questions
|
| 125 |
+
|
| 126 |
+
with st.form("test_form"):
|
| 127 |
+
for idx, question in enumerate(test_questions):
|
| 128 |
+
st.markdown(f"### Question {idx + 1}")
|
| 129 |
+
st.write(question['question'])
|
| 130 |
+
|
| 131 |
+
options = [
|
| 132 |
+
question['option1'],
|
| 133 |
+
question['option2'],
|
| 134 |
+
question['option3'],
|
| 135 |
+
question['option4']
|
| 136 |
+
]
|
| 137 |
+
|
| 138 |
+
answer = st.radio(
|
| 139 |
+
"Choose your answer:",
|
| 140 |
+
options,
|
| 141 |
+
key=f"test_{idx}",
|
| 142 |
+
index=None
|
| 143 |
+
)
|
| 144 |
+
st.session_state.test_answers[idx] = {
|
| 145 |
+
'question': question['question'],
|
| 146 |
+
'user_answer': answer,
|
| 147 |
+
'correct_answer': options[question['correct'] - 1]
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
st.markdown("---")
|
| 151 |
+
|
| 152 |
+
submitted = st.form_submit_button("Submit Test")
|
| 153 |
+
|
| 154 |
+
if submitted:
|
| 155 |
+
end_time = time.time()
|
| 156 |
+
time_taken = end_time - st.session_state.test_start_time
|
| 157 |
+
st.session_state.test_start_time = end_time
|
| 158 |
+
|
| 159 |
+
# Calculate score
|
| 160 |
+
correct_count = 0
|
| 161 |
+
for idx, answer_data in st.session_state.test_answers.items():
|
| 162 |
+
if answer_data['user_answer'] == answer_data['correct_answer']:
|
| 163 |
+
correct_count += 1
|
| 164 |
+
|
| 165 |
+
st.session_state.test_score = (correct_count / len(test_questions)) * 100
|
| 166 |
+
st.session_state.test_completed = True
|
| 167 |
+
st.session_state.time_taken = time_taken
|
| 168 |
+
|
| 169 |
+
# Show results
|
| 170 |
+
st.success(f"Test completed! Your score: {st.session_state.test_score:.2f}%")
|
| 171 |
+
st.info(f"Time taken: {time_taken:.2f} seconds")
|
| 172 |
+
|
| 173 |
+
# Show review
|
| 174 |
+
st.markdown("### Review Your Answers")
|
| 175 |
+
for idx, answer_data in st.session_state.test_answers.items():
|
| 176 |
+
if answer_data['user_answer'] == answer_data['correct_answer']:
|
| 177 |
+
color_class = "correct-answer"
|
| 178 |
+
else:
|
| 179 |
+
color_class = "wrong-answer"
|
| 180 |
+
|
| 181 |
+
with st.expander(f"Question {idx + 1}", expanded=True):
|
| 182 |
+
if answer_data['user_answer'] == answer_data['correct_answer']:
|
| 183 |
+
st.success(f"{answer_data['question']}")
|
| 184 |
+
st.markdown(f"Your answer: {answer_data['user_answer']}")
|
| 185 |
+
st.markdown(f"Correct answer: {answer_data['correct_answer']}")
|
| 186 |
+
st.markdown(f"<div class='{color_class}'>Correct! β
</div>", unsafe_allow_html=True)
|
| 187 |
+
else:
|
| 188 |
+
st.error(f"{answer_data['question']}")
|
| 189 |
+
st.markdown(f"Your answer: {answer_data['user_answer']}")
|
| 190 |
+
st.markdown(f"Correct answer: {answer_data['correct_answer']}")
|
| 191 |
+
st.markdown(f"<div class='{color_class}'>Incorrect β</div>", unsafe_allow_html=True)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
# Subject page
|
| 196 |
+
def show_subject_page(subject, questions):
|
| 197 |
+
st.title(f"{subject} Quiz")
|
| 198 |
+
|
| 199 |
+
# Sidebar components
|
| 200 |
+
with st.sidebar:
|
| 201 |
+
if st.button("Home"):
|
| 202 |
+
st.session_state.page = 'landing'
|
| 203 |
+
st.experimental_rerun()
|
| 204 |
+
|
| 205 |
+
# Mode selection
|
| 206 |
+
mode = st.radio(
|
| 207 |
+
"Select Mode:",
|
| 208 |
+
['Practice', 'Test'],
|
| 209 |
+
key="mode_selection"
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
# Week selection
|
| 213 |
+
weeks = sorted(list(set(q['week'] for q in questions)))
|
| 214 |
+
selected_week = st.selectbox(
|
| 215 |
+
"Select Week:",
|
| 216 |
+
[None] + weeks,
|
| 217 |
+
format_func=lambda x: "All Weeks" if x is None else f"Week {x}"
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
if mode == 'Practice':
|
| 221 |
+
practice_mode(questions, selected_week)
|
| 222 |
+
else:
|
| 223 |
+
test_mode(questions, selected_week)
|
| 224 |
+
|
| 225 |
+
def main():
|
| 226 |
+
# Initialize session state
|
| 227 |
+
init_session_state()
|
| 228 |
+
|
| 229 |
+
# Load CSS
|
| 230 |
+
load_css()
|
| 231 |
+
|
| 232 |
+
# Load questions
|
| 233 |
+
questions_data = load_questions()
|
| 234 |
+
|
| 235 |
+
if not questions_data:
|
| 236 |
+
st.error("No question data available. Please check the data folder.")
|
| 237 |
+
return
|
| 238 |
+
|
| 239 |
+
# Initialize page state if not exists
|
| 240 |
+
if 'page' not in st.session_state:
|
| 241 |
+
st.session_state.page = 'landing'
|
| 242 |
+
|
| 243 |
+
# Navigation
|
| 244 |
+
if st.session_state.page == 'landing':
|
| 245 |
+
show_landing_page(questions_data.keys())
|
| 246 |
+
elif st.session_state.page == 'subject':
|
| 247 |
+
show_subject_page(
|
| 248 |
+
st.session_state.selected_subject,
|
| 249 |
+
questions_data[st.session_state.selected_subject]
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
if __name__ == "__main__":
|
| 253 |
+
main()
|