-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_app.py
More file actions
192 lines (166 loc) · 7.23 KB
/
web_app.py
File metadata and controls
192 lines (166 loc) · 7.23 KB
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
import os
from flask import Flask, redirect, render_template, request, session, url_for
from game import default_python_challenges
def create_app() -> Flask:
app = Flask(__name__)
app.secret_key = os.environ.get("CODE_QUEST_SECRET", "dev-secret-change-me")
@app.get("/")
def home():
progress = _get_progress()
current = min(progress["python_done"] + 1, len(_python_lessons()))
return render_template("home.html", progress=progress, current=current, total=3)
@app.get("/python")
def python_lesson():
progress = _get_progress()
lessons = _python_lessons()
idx = progress["python_done"]
if idx >= len(lessons):
return render_template("python.html", progress=progress, complete=True, lesson=None)
lesson = lessons[idx]
attempts = session.get("attempts", {}).get(str(idx), 0)
return render_template(
"python.html",
progress=progress,
complete=False,
lesson=lesson,
lesson_number=idx + 1,
total_lessons=len(lessons),
attempts=attempts,
feedback=None,
status="idle",
)
@app.post("/python")
def submit_python_answer():
progress = _get_progress()
lessons = _python_lessons()
idx = progress["python_done"]
if idx >= len(lessons):
return redirect(url_for("python_lesson"))
lesson = lessons[idx]
answer = request.form.get("answer", "").strip()
attempts_map = session.get("attempts", {})
attempts = int(attempts_map.get(str(idx), 0)) + 1
attempts_map[str(idx)] = attempts
session["attempts"] = attempts_map
if lesson["checker"](answer):
progress["python_done"] += 1
progress["score"] += 10
_save_progress(progress)
attempts_map[str(idx)] = 0
session["attempts"] = attempts_map
return render_template(
"python.html",
progress=progress,
complete=False,
lesson=lesson,
lesson_number=idx + 1,
total_lessons=len(lessons),
attempts=attempts,
feedback={
"title": "Correct!",
"message": lesson["success_message"],
"explanation": lesson["explanation"],
"example": lesson["example"],
"award": "+10 points",
},
status="success",
)
status = "retry"
feedback = {
"title": "Not yet",
"message": "Good attempt. Learning is step-by-step.",
"explanation": lesson["gentle_explanation"],
"example": lesson["example"],
"award": None,
"hint": lesson["hint"],
}
if attempts >= 3:
status = "reveal"
feedback = {
"title": "Review Mode",
"message": "You used all attempts for this round. Here is the solution path.",
"explanation": lesson["solution_breakdown"],
"example": f"Reference answer: {lesson['model_answer']}",
"award": "No points this round, but you can continue learning.",
"hint": None,
}
return render_template(
"python.html",
progress=progress,
complete=False,
lesson=lesson,
lesson_number=idx + 1,
total_lessons=len(lessons),
attempts=attempts,
feedback=feedback,
status=status,
)
@app.post("/python/continue")
def continue_after_review():
progress = _get_progress()
lessons = _python_lessons()
idx = progress["python_done"]
if idx < len(lessons):
progress["python_done"] += 1
_save_progress(progress)
attempts_map = session.get("attempts", {})
attempts_map[str(idx)] = 0
session["attempts"] = attempts_map
return redirect(url_for("python_lesson"))
@app.post("/reset")
def reset():
session["progress"] = {"python_done": 0, "score": 0}
session["attempts"] = {}
return redirect(url_for("home"))
return app
def _get_progress() -> dict:
progress = session.get("progress", {"python_done": 0, "score": 0})
return {"python_done": int(progress.get("python_done", 0)), "score": int(progress.get("score", 0))}
def _save_progress(progress: dict) -> None:
session["progress"] = {"python_done": int(progress["python_done"]), "score": int(progress["score"])}
def _python_lessons() -> list[dict]:
challenges = default_python_challenges()
return [
{
"title": "Lists + sum()",
"concept": "A list can store many values. `sum()` adds numeric items together.",
"prompt": challenges[0].prompt,
"hint": challenges[0].hint,
"checker": challenges[0].checker,
"success_message": challenges[0].success_message,
"explanation": "The list contains 1, 2, and 3. `sum(numbers)` performs 1 + 2 + 3, which equals 6.",
"gentle_explanation": "Focus on what `sum()` does: it adds every number in the list.",
"solution_breakdown": "Step 1: Identify list values (1, 2, 3). Step 2: Add them in order. Step 3: Final output is 6.",
"example": "Try: `print(sum([4, 5, 6]))` -> 15",
"model_answer": "6",
},
{
"title": "for-loops + range()",
"concept": "`range(n)` creates numbers from 0 up to (but not including) n.",
"prompt": challenges[1].prompt,
"hint": challenges[1].hint,
"checker": challenges[1].checker,
"success_message": challenges[1].success_message,
"explanation": "`range(3)` gives 0, 1, 2. The loop prints each value one by one.",
"gentle_explanation": "A loop needs something iterable. `range()` is the standard way to generate simple index values.",
"solution_breakdown": "Step 1: Need three values (0,1,2). Step 2: `range(3)` provides exactly that. Step 3: loop prints each.",
"example": "Try: `for i in range(5): print(i)` -> 0 1 2 3 4",
"model_answer": "range",
},
{
"title": "Dictionaries",
"concept": "A dictionary stores key/value pairs, like labels and their values.",
"prompt": challenges[2].prompt,
"hint": challenges[2].hint,
"checker": challenges[2].checker,
"success_message": challenges[2].success_message,
"explanation": "Both `{...}` syntax and `dict(...)` syntax are valid ways to create dictionaries.",
"gentle_explanation": "Think of a dictionary as named slots: `name` and `skill` each hold one value.",
"solution_breakdown": "Step 1: Create keys `name` and `skill`. Step 2: assign values `Ada` and `SQL`. Step 3: return dictionary.",
"example": "Try: `{'city': 'Austin', 'state': 'TX'}`",
"model_answer": "{'name': 'Ada', 'skill': 'SQL'}",
},
]
if __name__ == "__main__":
app = create_app()
app.run(debug=True, host="127.0.0.1", port=5000)