Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import json
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# -------------------------
|
| 6 |
-
# Quiz
|
| 7 |
# -------------------------
|
| 8 |
quiz_questions = [
|
| 9 |
{
|
|
@@ -68,31 +68,46 @@ save_counters(counters)
|
|
| 68 |
# Quiz Functions
|
| 69 |
# -------------------------
|
| 70 |
def submit_answer(selected, q_index, score):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
question = quiz_questions[q_index]
|
| 72 |
explanation = ""
|
|
|
|
| 73 |
if selected == question["answer"]:
|
| 74 |
score += 1
|
| 75 |
explanation = f"✅ Correct!\n\n{question['explanation']}"
|
| 76 |
else:
|
| 77 |
explanation = f"❌ Incorrect.\n\n{question['explanation']}"
|
| 78 |
-
|
| 79 |
next_index = q_index + 1
|
|
|
|
|
|
|
| 80 |
if next_index >= len(quiz_questions):
|
| 81 |
# Quiz completed → update completion counter
|
| 82 |
-
counters = load_counters()
|
| 83 |
counters["completions"] += 1
|
| 84 |
save_counters(counters)
|
| 85 |
-
return (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
else:
|
| 87 |
next_question = quiz_questions[next_index]
|
| 88 |
-
return (
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 96 |
|
| 97 |
def show_hint(q_index):
|
| 98 |
return f"💡 Hint: {quiz_questions[q_index]['hint']}"
|
|
@@ -104,7 +119,8 @@ with gr.Blocks() as quiz_app:
|
|
| 104 |
gr.Markdown("# 🧪 Magnetic Flow Quiz")
|
| 105 |
|
| 106 |
# Counters
|
| 107 |
-
visitor_md = gr.Markdown(f"👥 Visitors: {counters['visitors']}
|
|
|
|
| 108 |
|
| 109 |
# Quiz area
|
| 110 |
question_md = gr.Markdown(f"### Question 1: {quiz_questions[0]['question']}")
|
|
@@ -124,10 +140,14 @@ with gr.Blocks() as quiz_app:
|
|
| 124 |
submit_btn.click(
|
| 125 |
fn=submit_answer,
|
| 126 |
inputs=[options_radio, q_index_state, score_state],
|
| 127 |
-
outputs=[question_md, options_radio, output_md,
|
| 128 |
)
|
| 129 |
|
| 130 |
-
hint_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
# Launch
|
| 133 |
quiz_app.launch()
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# -------------------------
|
| 6 |
+
# Quiz Data
|
| 7 |
# -------------------------
|
| 8 |
quiz_questions = [
|
| 9 |
{
|
|
|
|
| 68 |
# Quiz Functions
|
| 69 |
# -------------------------
|
| 70 |
def submit_answer(selected, q_index, score):
|
| 71 |
+
"""Process answer and move to next question or show final score."""
|
| 72 |
+
if selected is None:
|
| 73 |
+
return "Please select an answer before submitting.", gr.update(choices=quiz_questions[q_index]["options"], value=None), "", score, q_index, counters["visitors"], counters["completions"]
|
| 74 |
+
|
| 75 |
question = quiz_questions[q_index]
|
| 76 |
explanation = ""
|
| 77 |
+
|
| 78 |
if selected == question["answer"]:
|
| 79 |
score += 1
|
| 80 |
explanation = f"✅ Correct!\n\n{question['explanation']}"
|
| 81 |
else:
|
| 82 |
explanation = f"❌ Incorrect.\n\n{question['explanation']}"
|
| 83 |
+
|
| 84 |
next_index = q_index + 1
|
| 85 |
+
counters = load_counters()
|
| 86 |
+
|
| 87 |
if next_index >= len(quiz_questions):
|
| 88 |
# Quiz completed → update completion counter
|
|
|
|
| 89 |
counters["completions"] += 1
|
| 90 |
save_counters(counters)
|
| 91 |
+
return (
|
| 92 |
+
"🎉 Quiz Completed!",
|
| 93 |
+
gr.update(choices=[], value=None),
|
| 94 |
+
explanation + f"\n\n**Your Score: {score}/{len(quiz_questions)}**",
|
| 95 |
+
score,
|
| 96 |
+
next_index,
|
| 97 |
+
counters["visitors"],
|
| 98 |
+
counters["completions"]
|
| 99 |
+
)
|
| 100 |
else:
|
| 101 |
next_question = quiz_questions[next_index]
|
| 102 |
+
return (
|
| 103 |
+
f"### Question {next_index+1}: {next_question['question']}",
|
| 104 |
+
gr.update(choices=next_question["options"], value=None),
|
| 105 |
+
explanation,
|
| 106 |
+
score,
|
| 107 |
+
next_index,
|
| 108 |
+
counters["visitors"],
|
| 109 |
+
counters["completions"]
|
| 110 |
+
)
|
| 111 |
|
| 112 |
def show_hint(q_index):
|
| 113 |
return f"💡 Hint: {quiz_questions[q_index]['hint']}"
|
|
|
|
| 119 |
gr.Markdown("# 🧪 Magnetic Flow Quiz")
|
| 120 |
|
| 121 |
# Counters
|
| 122 |
+
visitor_md = gr.Markdown(f"👥 Visitors: {counters['visitors']}")
|
| 123 |
+
completion_md = gr.Markdown(f"✅ Completions: {counters['completions']}")
|
| 124 |
|
| 125 |
# Quiz area
|
| 126 |
question_md = gr.Markdown(f"### Question 1: {quiz_questions[0]['question']}")
|
|
|
|
| 140 |
submit_btn.click(
|
| 141 |
fn=submit_answer,
|
| 142 |
inputs=[options_radio, q_index_state, score_state],
|
| 143 |
+
outputs=[question_md, options_radio, output_md, score_state, q_index_state, visitor_md, completion_md]
|
| 144 |
)
|
| 145 |
|
| 146 |
+
hint_btn.click(
|
| 147 |
+
fn=show_hint,
|
| 148 |
+
inputs=q_index_state,
|
| 149 |
+
outputs=output_md
|
| 150 |
+
)
|
| 151 |
|
| 152 |
+
# Launch app
|
| 153 |
quiz_app.launch()
|