From 976d2211d985e32e6f22cc9851519ab1cabcbbed Mon Sep 17 00:00:00 2001 From: Scobiform Date: Wed, 17 Apr 2024 16:03:20 +0200 Subject: [PATCH] feat: jinja2 templates --- start.py | 34 ++++++---------------------------- templates/index.html | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/start.py b/start.py index 2ac2c99..db475b7 100644 --- a/start.py +++ b/start.py @@ -3,7 +3,7 @@ import subprocess from dotenv import load_dotenv from mastodon import Mastodon -from quart import Quart, abort, jsonify, request, redirect, session, url_for +from quart import Quart, abort, jsonify, render_template, request, redirect, session, url_for from app.configuration import ConfigurationManager from app.secrets import SecretManager from app.websocket import ConnectionManager @@ -40,28 +40,8 @@ async def setup_app(): level=getattr(logging, logging_config['level']) ) -# Html header jinja2 template -html_header = ''' - - - - '''+config['app_name']+''' - - - - -''' - -# Html footer jinja2 template -html_footer = ''' - - -''' - @app.route('/') async def home(): - header = html_header - footer = html_footer if 'access_token' in session: # Initialize Mastodon with the access token mastodon = Mastodon( @@ -69,13 +49,11 @@ async def home(): api_base_url=config['instance_url'] ) user = mastodon.account_verify_credentials() - return header+f''' - Logged in successfully!
- Logout
- Username: {user['username']} -
- '''+footer - return header+'Login with Mastodon'+footer + # Pass user data to the template + return await render_template('index.html', logged_in=True, user=user) + else: + # Render the template without user data + return await render_template('index.html', logged_in=False) @app.route('/login') async def login(): diff --git a/templates/index.html b/templates/index.html index e69de29..4451cc6 100644 --- a/templates/index.html +++ b/templates/index.html @@ -0,0 +1,22 @@ + + + + + + + + + {% if logged_in %} +

Welcome, {{ username }}!

+ Logout + {% else %} + Login + {% endif %} + +
+ + + + + +