Added simple web page, routes are in a separate file

This commit is contained in:
DarkCat09 2023-02-15 17:34:49 +04:00
parent 9ead8a457c
commit c7da7e32b7
9 changed files with 168 additions and 7 deletions

View file

@ -2,32 +2,38 @@
import os
import secrets
from pathlib import Path
from flask import Flask
from . import routes
from . import errors
def create_app() -> Flask:
"""Flask app factory function"""
root = Path('..')
static = str(root / 'static')
tmpl = str(root / 'templates')
# Create an app object
app = Flask(
__name__,
static_folder=static,
template_folder=tmpl,
static_folder='../static',
template_folder='../templates',
instance_relative_config=True,
)
# Get the token from environment
# or generate it using secrets
app.config['SECRET_KEY'] = os.getenv(
'SECRET_KEY',
secrets.token_hex(32),
)
# Create instance/ directory
try:
os.makedirs(app.instance_path)
except OSError:
pass
# Add routes
routes.add_routes(app)
errors.add_routes(app)
return app

48
flaskapp/errors.py Normal file
View file

@ -0,0 +1,48 @@
"""Flask app error handlers"""
from pathlib import Path
from flask import Flask
from flask import render_template
# Add other HTTP error codes here
CODES = [404, 500]
def add_routes(app: Flask) -> None:
"""Add all error handlers
Args:
app (Flask): Flask application
"""
tmpl_dir = app.template_folder
if tmpl_dir is None:
return
tmpl = Path(__file__).parent / tmpl_dir
for code in CODES:
add_handler(app, tmpl, code)
def add_handler(
app: Flask,
tmpl: Path,
code: int) -> None:
"""Add Flask app error handler.
Only for internal use
Args:
app (Flask): Flask application
file (str): Template filename
code (int): Error code
"""
file = f'{code}.html'
if (tmpl / file).exists():
@app.errorhandler(code)
def handler(_e):
return render_template(file), code

16
flaskapp/routes.py Normal file
View file

@ -0,0 +1,16 @@
"""Main Flask app routes"""
from flask import Flask
from flask import render_template
def add_routes(app: Flask) -> None:
"""Add main routes
Args:
app (Flask): Flask application
"""
@app.route('/')
def index():
return render_template('index.html')

31
static/css/style.css Normal file
View file

@ -0,0 +1,31 @@
body {
height: 100vh;
padding: 0;
margin: 0;
font-family: sans-serif;
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
background: #fff;
color: #000;
}
@media (prefers-color-scheme: dark) {
body {
background: #202023;
color: #eee;
}
}
header { margin-top: 5px; }
footer { margin-bottom: 5px; }
a {
color: #5b8a55;
}
a:hover {
filter: brightness(120%);
}

4
static/js/script.js Normal file
View file

@ -0,0 +1,4 @@
addEventListener('load', () => {
document.getElementById('js')
.innerText = new Date().toLocaleString()
})

11
templates/404.html Normal file
View file

@ -0,0 +1,11 @@
{% extends "base.html" %}
{% block title %}404{% endblock %}
{% block content %}
<h1>404: Not Found</h1>
<p>
Go to the
<a href="/">main page</a>
</p>
{% endblock %}

15
templates/500.html Normal file
View file

@ -0,0 +1,15 @@
{% extends "base.html" %}
{% block title %}500{% endblock %}
{% block content %}
<h1>500: ISE</h1>
<p>
An error occured while
processing your request.
</p>
<p>
Please, try again later
or contact web site admin.
</p>
{% endblock %}

18
templates/base.html Normal file
View file

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="${REPO_DESCRIPTION}">
<title>{% block title %}{% endblock %} | ${REPO_NAME}</title>
<link rel="stylesheet" href="/static/css/style.css">
<script src="/static/js/script.js"></script>
</head>
<body>
<header>${REPO_NAME}</header>
<article>
{% block content %}{% endblock %}
</article>
<footer id="js"></footer>
</body>
</html>

12
templates/index.html Normal file
View file

@ -0,0 +1,12 @@
{% extends "base.html" %}
{% block title %}Main page{% endblock %}
{% block content %}
<h1>
This is the default main page of
<a href="https://git.dc09.ru/DarkCat09/tmpl-flask">
Flask app template
</a>
</h1>
{% endblock %}