Skip to content

Commit

Permalink
feat: jinja2 templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Scobiform committed Apr 17, 2024
1 parent 626ecf5 commit 976d221
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
34 changes: 6 additions & 28 deletions start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,42 +40,20 @@ async def setup_app():
level=getattr(logging, logging_config['level'])
)

# Html header jinja2 template
html_header = '''
<!DOCTYPE html>
<html>
<head>
<title>'''+config['app_name']+'''</title>
<script src="//unpkg.com/force-graph"></script>
<link rel="stylesheet" href="static/style.css">
</head>
<body>
'''

# Html footer jinja2 template
html_footer = '''
</body>
</html>
'''

@app.route('/')
async def home():
header = html_header
footer = html_footer
if 'access_token' in session:
# Initialize Mastodon with the access token
mastodon = Mastodon(
access_token=session['access_token'],
api_base_url=config['instance_url']
)
user = mastodon.account_verify_credentials()
return header+f'''
Logged in successfully! <br>
<a href="/logout">Logout</a><br>
Username: {user['username']}
<br>
'''+footer
return header+'<a href="/login">Login with Mastodon</a>'+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():
Expand Down
22 changes: 22 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<script src="//unpkg.com/force-graph"></script>
<link rel="stylesheet" href="static/style.css">
</head>
<body>

{% if logged_in %}
<p>Welcome, {{ username }}!</p>
<a href="/logout">Logout</a>
{% else %}
<a href="/login">Login</a>
{% endif %}

<div id="graph"></div>
<script src="static/script.js"></script>

</body>
</html>


0 comments on commit 976d221

Please sign in to comment.