Py.Cafe

MinetestA/

NEA_chemistry_quiz_system

Streamlit in Py.cafe - Examples & Demos

DocsPricing
  • Media/
  • LICENSE
  • app.py
  • questions.json
  • requirements.txt
  • utils.py
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# A program that implements a chemistry quiz application using Streamlit.
# Made by Josiah Anderson between 1st Jan '26 and #think about the end date a little
# NEA_chemistry_quiz_system/app.py

import streamlit as st
import json
import time
import pandas as pd
from utils import (
    tolerance_mark,
    apply_penalty,
    initialise_state,
    mark_checkbox,
    mark_fill_blank,
    render_media
)
# import os #removed os as it becomes unused after refactoring
from pathlib import Path
BASE_DIR = Path(__file__).parent
# =========================

st.set_page_config(page_title="Chemistry Quiz", layout="centered")
initialise_state(st.session_state)

# πŸ”’ Ensure title-page flag exists before use
# Defensive session-state initialisation
if "started" not in st.session_state:
    st.session_state.started = False

# Handle question advancement cleanly
if st.session_state.advance_question:
    st.session_state.index += 1
    st.session_state.start_time = time.time()
    st.session_state.answered = False
    st.session_state.advance_question = False

# =========================
# TITLE PAGE
# =========================

if not st.session_state.started:
    st.title("GCSE Chemistry Required Practical Quiz")
    st.subheader("Assess your understanding of core experimental chemistry")

    st.markdown(
        """
        ### Instructions
        - Answer each question carefully.
        - All of the questions in this quiz are for RP4 - Temperature Changes.
        - Some questions may include images or videos.
        - Numerical answers allow a small tolerance.
        - Your score may decrease with repeated attempts.
        - Click **Submit** to check your answer, then **Continue** to move on.
        """
    )

    # ---- Title image ----
    title_img = {
        # "image":"Media/temperature_image_title_page.png",
        # "image":"Media/schoolkids_sweaters_image_title_page.png",
        "image":"Media/schoolkids_labcoats_image_title_page.png"
        # OR you could use:
        # "video":"https://www.youtube.com/watch?v=/....."
    }
    render_media(title_img, BASE_DIR)

    # ---- Begin button ----
    if st.button("β–Ά Click to begin"):
        st.session_state.started = True
        st.session_state.start_time = time.time()
        st.rerun()

    st.stop()


# Load questions
with open("questions.json", "r") as f:
    QUESTIONS = json.load(f)

TOTAL_QUESTIONS = len(QUESTIONS)

# Guard against index overflow
if st.session_state.index >= TOTAL_QUESTIONS:
    current_q = None
else:
    current_q = QUESTIONS[st.session_state.index]

st.title("GCSE Chemistry RP Temperature Changes Quiz")

# =========================
# QUIZ FINISHED
# =========================
if current_q is None:
    st.title("πŸŽ‰ Quiz Complete")

    total_score = sum(r["score"] for r in st.session_state.results)
    percentage = (total_score / TOTAL_QUESTIONS) * 100

    st.write(f"Final score: **{percentage:.1f}%**")

    if percentage >= 80:
        st.balloons()

    # ----- Feedback breakdown table -----
    st.markdown("### Feedback Breakdown")

    df = pd.DataFrame(st.session_state.results)

    # Add 1-indexed question numbers
    df.insert(0, "Question", range(1, len(df) + 1))

    # Friendly display correctness values
    df["Correct"] = df["correct"].map({True: "βœ…", False: "❌"})

    # Rename question column properly
    df = df.rename(columns={"question": "Question Text"})

    # Select and order columns for display
    df = df[["Question", "Question Text", "Correct", "attempts", "score", "time"]]

    # Display without Pandas' 0-indexed row labels
    st.dataframe(df, width='stretch', hide_index=True)

    st.markdown(
        """
        ###### Notes
        - To see the full question text, select a question then click on it agian.
        - Use the scroll bar on the table to see the score and time taken. 
        """
    )

    st.stop()

# =========================
# QUESTION DISPLAY
# =========================

if st.session_state.start_time is None:
    st.session_state.start_time = time.time()

st.subheader(f"Question {st.session_state.index + 1} of {TOTAL_QUESTIONS}")
st.write(current_q["prompt"])

# ------- Media -------
# Only show media for question types that support it
# Follows my question-type design.
if current_q["type"] in ["numerical", "radio", "checkbox", "graph"]:
    render_media(current_q.get("media"), BASE_DIR)


# =========================
# ANSWER INPUT
# =========================

user_answer = None
user_answers = None

if current_q["type"] == "numerical":
    user_answer = st.number_input("Your answer", step=0.01)

elif current_q["type"] == "radio":
    user_answer = st.radio("Choose one:", current_q["choices"])

elif current_q["type"] == "checkbox":
    user_answers = []
    for choice in current_q["choices"]:
        if st.checkbox(choice):
            user_answers.append(choice)  # βœ… store text instead of index


elif current_q["type"] == "fill_blank":
    user_answers = []
    st.write(current_q["text"])
    for i in range(len(current_q["blanks"])):
        user_answers.append(
            st.text_input(f"Blank {i + 1}")
        )

elif current_q["type"] == "graph":
    st.markdown("### Choose the correct graph:")
    graphs = current_q["graphs"]

    # Labels for each graph (A, B, C, ...)
    labels = ["A", "B", "C", "D", "E"]

    # --- Display all graphs side-by-side ---
    cols = st.columns(len(graphs))
    for i, graph_path in enumerate(graphs):
        with cols[i]:
            st.image(str(BASE_DIR / graph_path), width='stretch')
            st.markdown(f"**Graph {labels[i]}**")
    st.markdown("---")

    user_answer = st.radio(
        "Which graph is correct",
        options=list(range(len(graphs))),
        format_func=lambda x: f"Graph {labels[x]}"
    )
    #st.image(current_q["graphs"][user_answer]) #Can be added if users want this back.


# =========================
# SUBMIT ANSWER
# =========================

submitted = st.button("Submit")

if submitted:
    qid = current_q["id"]
    st.session_state.current_qid = qid  # Storing the qid safely

    st.session_state.attempts.setdefault(qid, 0)
    st.session_state.attempts[qid] += 1

    # -----------------------------
    # Default values (always defined)
    # -----------------------------
    correct = False
    status = "incorrect"
    correct_count = 0
    total = 0
    total_correct = 0  # βœ… FIX: prevents NameError
    over_selected = False
    max_selections = None

    # -----------------------------
    # Marking logic
    # -----------------------------

    if current_q["type"] == "numerical":
        correct = tolerance_mark(
            user_answer,
            current_q["answer"]["value"],
            current_q["answer"]["tolerance"]
        )
        status = "correct" if correct else "incorrect"

    elif current_q["type"] == "radio":
        if "answer" not in current_q:  # Defensive check
            st.error("Question configuration error: missing correct answer.")
            st.stop()

        correct = (user_answer == current_q["answer"])
        status = "correct" if correct else "incorrect"

    elif current_q["type"] == "checkbox":
        max_sel = current_q.get("max_selections", None)

        status, correct_count, total_correct, over_selected, max_selections = mark_checkbox(
            user_answers,  # βœ… Correct variable
            current_q["answer"],
            tolerance=0.8,
            max_selections=max_sel
        )

        correct = (status == "correct")

        # Store totals so feedback works
        total = total_correct

    elif current_q["type"] == "fill_blank":
        status, correct_count, total = mark_fill_blank(
            user_answers,
            current_q["blanks"]
        )
        correct = (status == "correct")

    elif current_q["type"] == "graph":
        correct = (user_answer == current_q["correct_index"])
        status = "correct" if correct else "incorrect"

    # -----------------------------
    # Store result ONCE (unified)
    # -----------------------------
    st.session_state.answered = True

    st.session_state.last_result = {
        "correct": correct,
        "status": status,
        "correct_count": correct_count,
        "total_correct": total_correct,
        "over_selected": over_selected,
        "max_selections": max_selections,
        "total": total,
        "user_answer": user_answer if user_answer is not None else user_answers
    }

# =========================
# FEEDBACK + CONTINUE
# =========================

if st.session_state.answered:

    #Storing this in a shorter name so that lines are shorter and easier to read.
    last_result = st.session_state.last_result 

    # Feedback
    if current_q["type"] == "fill_blank":
        if last_result["status"] == "correct":
            st.success("Correct βœ…")
        elif last_result["status"] == "partial":
            st.warning(
                f"Nearly correct ⚠️ ({last_result['correct_count']}/"
                f"{last_result['total']} blanks correct)"
            )
        else:
            st.error("Incorrect ❌")
            
    elif current_q["type"] == "checkbox":
        correct_count = last_result.get("correct_count", 0)
        total_correct = last_result.get("total_correct", 0)

        over_selected = last_result.get("over_selected", False)
        max_selections = last_result.get("max_selections", total_correct)

        # Special stakeholder-friendly message
        if over_selected:
            st.error(
                "Oops! Try again.\n\n"
            f"Only {max_selections} of these items are needed for this experiment."
            )


        elif last_result["status"] == "correct":
            st.success(
                f"βœ… Correct! You selected all {max_selections} required items."
                )
        elif last_result["status"] == "partial":
            st.warning(
                f"Nearly correct! ⚠️ You got {correct_count} out of {total_correct} correct."
                )
        else:
            st.error(
                f"❌ Incorrect. You got {correct_count} out of {total_correct} correct."
                )

    else:
        # Existing feedback for other question types
        if last_result["correct"]:
            st.success("βœ… Correct!")
        else:
            st.error("❌ Incorrect.")

    # Continue button
    if st.button("Continue"):
        # Calculate time taken
        time_taken = round(time.time() - st.session_state.start_time, 1)

        qid = st.session_state.get("current_qid")
        if qid is None: # Defensive check
            st.error("Internal error: Question ID missing.")
            st.stop()
        retries = st.session_state.attempts[qid] - 1

        # Scoring
        if current_q["type"] == "fill_blank":
            if last_result["status"] == "correct":
                score = 1
            elif last_result["status"] == "partial":
                score = 0.5
            else:
                score = 0
        elif current_q["type"] == "checkbox":
            if last_result["status"] == "correct":
                base_score = 1
            elif last_result["status"] == "partial":
                base_score = 0.5
            else:
                base_score = 0
            score = apply_penalty(base_score, retries)


        else:
            score = apply_penalty(1, retries)

        # βœ… STORE RESULT BEFORE RERUN
        st.session_state.results.append({
            "question": current_q["prompt"],
            "user_answer": last_result["user_answer"],
            "correct": last_result["correct"],
            "attempts": retries + 1,
            "score": score,
            "time": time_taken,
            "explanation": current_q["explanation"]
        })

        # Advance question safely
        st.session_state.advance_question = True
        st.rerun()