scrapple/app.py
2022-12-07 01:19:40 -06:00

52 lines
1.1 KiB
Python

import json
import math
from flask import Flask, render_template, abort, request
app = Flask(__name__)
_data = {}
def parks():
global _data
if not _data:
with open("data/parks.json") as f:
_data = {int(p["id"]): p for p in json.load(f)}
return _data
@app.route("/")
def index():
return render_template("index.html")
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/parks")
def park_list():
page = int(request.args.get("page", 1))
per_page = 10
total_items = len(parks())
max_page = math.ceil(total_items / per_page)
if page < 1 or page > max_page:
abort(404)
page_parks = list(parks().values())[(page - 1) * per_page : page * per_page]
return render_template(
"parks.html",
parks=page_parks,
page=page,
prev_page=page - 1,
next_page=page + 1 if page < max_page else None,
)
@app.route("/parks/<id_>")
def park_detail(id_):
if park := parks().get(int(id_)):
return render_template("park_detail.html", park=park)
else:
abort(404)