-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
113 lines (85 loc) · 3 KB
/
app.py
File metadata and controls
113 lines (85 loc) · 3 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
from __future__ import annotations
import json
import os
import time
from pathlib import Path
from flask import Flask, abort, render_template
from chapter_order import reorder_manifest_subjects
BASE_DIR = Path(__file__).resolve().parent
DATA_DIR = BASE_DIR / "data"
MANIFEST_PATH = DATA_DIR / "subjects.json"
EXTRACTED_DIR = DATA_DIR / "extracted"
app = Flask(__name__)
app.config["ASSET_VERSION"] = int(time.time())
@app.context_processor
def inject_asset_version() -> dict:
return {"asset_version": app.config["ASSET_VERSION"]}
def load_manifest() -> dict:
if not MANIFEST_PATH.exists():
raise FileNotFoundError(
f"Missing manifest: {MANIFEST_PATH}. Run scripts/extract_questions.py first."
)
with MANIFEST_PATH.open("r", encoding="utf-8") as handle:
return reorder_manifest_subjects(json.load(handle))
def get_subject_or_404(subject_id: str) -> dict:
manifest = load_manifest()
for subject in manifest["subjects"]:
if subject["id"] == subject_id:
return subject
abort(404)
def get_chapter_or_404(subject: dict, chapter_id: str) -> dict:
for chapter in subject["chapters"]:
if chapter["id"] == chapter_id:
return chapter
abort(404)
def load_chapter_data(subject_id: str, chapter_id: str) -> dict:
chapter_path = EXTRACTED_DIR / subject_id / f"{chapter_id}.json"
if not chapter_path.exists():
abort(404)
with chapter_path.open("r", encoding="utf-8") as handle:
return json.load(handle)
@app.route("/")
def index():
manifest = load_manifest()
return render_template("index.html", manifest=manifest, subjects=manifest["subjects"])
@app.route("/subjects/<subject_id>")
def subject_detail(subject_id: str):
manifest = load_manifest()
subject = get_subject_or_404(subject_id)
return render_template(
"subject.html",
manifest=manifest,
subject=subject,
subjects=manifest["subjects"],
)
@app.route("/subjects/<subject_id>/chapters/<chapter_id>")
def chapter_detail(subject_id: str, chapter_id: str):
manifest = load_manifest()
subject = get_subject_or_404(subject_id)
chapter = get_chapter_or_404(subject, chapter_id)
chapter_data = load_chapter_data(subject_id, chapter_id)
initial_question = chapter_data["questions"][0] if chapter_data["questions"] else None
return render_template(
"chapter.html",
manifest=manifest,
subject=subject,
chapter=chapter,
chapter_data=chapter_data,
initial_question=initial_question,
subjects=manifest["subjects"],
)
@app.route("/favorites")
def favorites():
manifest = load_manifest()
return render_template(
"favorites.html",
manifest=manifest,
subjects=manifest["subjects"],
)
@app.route("/healthz")
def healthz():
return {"ok": True}
if __name__ == "__main__":
host = os.environ.get("HOST", "::")
port = int(os.environ.get("PORT", "5080"))
app.run(host=host, port=port, debug=False)