2022-12-07 06:11:21 +00:00
|
|
|
import json
|
2021-06-04 21:49:42 +00:00
|
|
|
import math
|
|
|
|
from flask import Flask, render_template, abort, request
|
2021-05-22 18:29:40 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
2022-12-07 06:11:21 +00:00
|
|
|
_data = {}
|
2021-05-23 02:42:39 +00:00
|
|
|
|
2021-05-22 18:29:40 +00:00
|
|
|
|
2022-12-07 06:11:21 +00:00
|
|
|
def parks():
|
|
|
|
global _data
|
|
|
|
if not _data:
|
|
|
|
with open("data/parks.json") as f:
|
2022-12-07 07:19:40 +00:00
|
|
|
_data = {int(p["id"]): p for p in json.load(f)}
|
2022-12-07 06:11:21 +00:00
|
|
|
return _data
|
2021-05-23 02:18:20 +00:00
|
|
|
|
|
|
|
|
2021-05-22 18:29:40 +00:00
|
|
|
@app.route("/")
|
|
|
|
def index():
|
|
|
|
return render_template("index.html")
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/about")
|
|
|
|
def about():
|
|
|
|
return render_template("about.html")
|
2021-05-22 19:23:17 +00:00
|
|
|
|
|
|
|
|
2022-12-07 06:11:21 +00:00
|
|
|
@app.route("/parks")
|
2022-12-07 07:19:40 +00:00
|
|
|
def park_list():
|
2021-06-04 21:49:42 +00:00
|
|
|
page = int(request.args.get("page", 1))
|
|
|
|
per_page = 10
|
2022-12-07 06:11:21 +00:00
|
|
|
total_items = len(parks())
|
2021-06-04 21:49:42 +00:00
|
|
|
max_page = math.ceil(total_items / per_page)
|
|
|
|
if page < 1 or page > max_page:
|
|
|
|
abort(404)
|
2022-12-07 06:11:21 +00:00
|
|
|
page_parks = list(parks().values())[(page - 1) * per_page : page * per_page]
|
2021-06-04 21:49:42 +00:00
|
|
|
return render_template(
|
2022-12-07 06:11:21 +00:00
|
|
|
"parks.html",
|
|
|
|
parks=page_parks,
|
2021-06-04 21:49:42 +00:00
|
|
|
page=page,
|
|
|
|
prev_page=page - 1,
|
|
|
|
next_page=page + 1 if page < max_page else None,
|
|
|
|
)
|
2021-05-23 02:42:39 +00:00
|
|
|
|
2021-05-22 19:23:17 +00:00
|
|
|
|
2022-12-07 06:11:21 +00:00
|
|
|
@app.route("/parks/<id_>")
|
|
|
|
def park_detail(id_):
|
2022-12-07 07:19:40 +00:00
|
|
|
if park := parks().get(int(id_)):
|
|
|
|
return render_template("park_detail.html", park=park)
|
2021-05-23 02:42:39 +00:00
|
|
|
else:
|
2021-05-23 02:57:58 +00:00
|
|
|
abort(404)
|