From 5b01707a04c362ced66428bef8e0234176d212d9 Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Fri, 8 Nov 2024 17:30:19 +0100 Subject: Added the changes to the templates to pass the url_for function --- src/match_up/__init__.py | 20 +++++++++++++------- src/match_up/templates/index.j2 | 4 ++-- src/match_up/templates/macros.j2 | 4 ++-- src/match_up/templates/players.j2 | 4 ++-- 4 files changed, 19 insertions(+), 13 deletions(-) (limited to 'src/match_up') 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/", 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 %} - {{ header() }} + {{ header(url_for) }}
- {{ navbar() }} + {{ navbar(url_for) }}
{{ 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 @@ {%- endmacro %} -{% macro header() -%} +{% macro header(url_for) -%} @@ -38,7 +38,7 @@
Made by Karan using Flask, HTMX and Bulma
{%- endmacro %} -{% macro navbar() -%} +{% macro navbar(url_for) -%}