diff options
| author | Karan Jayachandra <mail@karanjayachandra.com> | 2024-11-08 17:30:19 +0100 |
|---|---|---|
| committer | Karan Jayachandra <mail@karanjayachandra.com> | 2024-11-08 17:30:19 +0100 |
| commit | 5b01707a04c362ced66428bef8e0234176d212d9 (patch) | |
| tree | 99e43efa171403cc08c7be28f9ec791462f822a9 /src | |
| parent | 2a390e99e03fe538169aa6d7c23669eaa5ece467 (diff) | |
Added the changes to the templates to pass the url_for function
Diffstat (limited to 'src')
| -rw-r--r-- | src/match_up/__init__.py | 20 | ||||
| -rw-r--r-- | src/match_up/templates/index.j2 | 4 | ||||
| -rw-r--r-- | src/match_up/templates/macros.j2 | 4 | ||||
| -rw-r--r-- | src/match_up/templates/players.j2 | 4 |
4 files changed, 19 insertions, 13 deletions
diff --git a/src/match_up/__init__.py b/src/match_up/__init__.py index badf3f0..3a78b8d 100644 --- a/src/match_up/__init__.py +++ b/src/match_up/__init__.py @@ -1,19 +1,24 @@ from dotenv import load_dotenv +from importlib.resources import path from match_up.controller import RoundGenerator from match_up.model import CourtList, PlayerList -from jinja2 import FileSystemLoader, Environment -from flask import Flask, render_template, request - +from jinja2 import FileSystemLoader, Environment, PackageLoader +from flask import Flask, render_template, request, url_for load_dotenv() -app = Flask("Match Up! Backend") +with path("match_up", "static") as p: + static_folder = p +with path("match_up", "templates") as p: + template_folder = p +app = Flask("Match Up! Backend", static_folder=static_folder, template_folder=template_folder) rg = RoundGenerator(courts=CourtList(), players=PlayerList()) -re = Environment(loader=FileSystemLoader("templates")) +re = Environment(loader=PackageLoader("match_up")) @app.route("/", methods=["GET"]) def home(): - return render_template("index.j2") + template = re.get_template("index.j2").render(url_for=url_for) + return template @app.route("/time", methods=["GET"]) @@ -74,7 +79,8 @@ def toggle_player(player_request): @app.route("/players", methods=["GET"]) def get_list_of_players(): - return render_template("players.j2", players=rg.players.get_players()) + template = re.get_template("players.j2").render(players=rg.players.get_players(), url_for=url_for) + return template @app.route("/court-toggle/<court_number>", methods=["POST"]) diff --git a/src/match_up/templates/index.j2 b/src/match_up/templates/index.j2 index 832dbfb..ce0d531 100644 --- a/src/match_up/templates/index.j2 +++ b/src/match_up/templates/index.j2 @@ -1,10 +1,10 @@ {% from "macros.j2" import header, footer, navbar %} <!doctype html> <html lang="en"> - {{ header() }} + {{ header(url_for) }} <body> <section class="section"> - {{ navbar() }} + {{ navbar(url_for) }} <div hx-post="/clear" hx-swap="outerHTML" hx-trigger="load"></div> <div hx-get="/control" hx-swap="outerHTML" hx-trigger="load"></div> {{ footer() }} diff --git a/src/match_up/templates/macros.j2 b/src/match_up/templates/macros.j2 index 8d05bb0..dedbf48 100644 --- a/src/match_up/templates/macros.j2 +++ b/src/match_up/templates/macros.j2 @@ -22,7 +22,7 @@ </div> {%- endmacro %} -{% macro header() -%} +{% macro header(url_for) -%} <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> @@ -38,7 +38,7 @@ <div class="content has-text-centered">Made by <a href="https://karanjayachandra.com/">Karan</a> using <a href="https://flask.palletsprojects.com">Flask</a>, <a href="https://htmx.org/">HTMX</a> and <a href="https://bulma.io">Bulma</a></div> {%- endmacro %} -{% macro navbar() -%} +{% macro navbar(url_for) -%} <nav class="level"> <div class="level-left"> <div class="level-item"> diff --git a/src/match_up/templates/players.j2 b/src/match_up/templates/players.j2 index ae6afe0..0d1f96c 100644 --- a/src/match_up/templates/players.j2 +++ b/src/match_up/templates/players.j2 @@ -1,10 +1,10 @@ {% from "macros.j2" import header, navbar, footer, player %} <!doctype html> <html lang="en"> - {{ header() }} + {{ header(url_for) }} <body> <section class="section"> - {{ navbar() }} + {{ navbar(url_for) }} <div class="fixed-grid has-4-cols"> <div class="grid"> {% for id, value in players.items() %} |
