Skip to content

Commit 99229e1

Browse files
committedApr 15, 2024
Refactor flask-assets.
1 parent ef2431e commit 99229e1

15 files changed

+304
-227
lines changed
 

‎.env.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ ENVIRONMENT=production
22
FLASK_DEBUG=True
33
SECRET_KEY=YourSecretKey
44

5-
REDIS_URI=redis://:password@host.com:1234
5+
REDIS_URI="redis://:password@host.com:1234"
66

77
SQLALCHEMY_DATABASE_URI=mysql+pymysql://myuser:mypassword@host.example.com:1234/mydatabase
88
SQLALCHEMY_CA_CERTS="./creds/certificate.crt"

‎config.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""App configuration."""
2-
from os import environ, path
2+
33
import subprocess
4+
from os import environ, path
45

56
import redis
67
from dotenv import load_dotenv

‎flask_session_tutorial/__init__.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
"""Initialize app."""
2+
23
from flask import Flask
4+
from flask_assets import Environment
35
from flask_login import LoginManager
46
from flask_session import Session
57
from flask_sqlalchemy import SQLAlchemy
68

79
db = SQLAlchemy()
810
login_manager = LoginManager()
911
session = Session()
12+
assets = Environment()
1013

1114

1215
def create_app():
1316
"""Construct the core flask_session_tutorial."""
1417
app = Flask(__name__, instance_relative_config=False)
1518
app.config.from_object("config.Config")
19+
assets = Environment()
1620

1721
# Initialize Plugins
1822
db.init_app(app)
1923
login_manager.init_app(app)
2024
session.init_app(app)
25+
assets.init_app(app)
2126

2227
with app.app_context():
2328
from . import auth, routes
24-
from .assets import compile_static_assets
29+
from .assets import compile_javascript, compile_stylesheets
2530

2631
app.register_blueprint(routes.main_blueprint)
2732
app.register_blueprint(auth.auth_blueprint)
2833

2934
# Create static asset bundles
30-
compile_static_assets(app)
35+
compile_stylesheets(assets)
36+
compile_javascript(assets)
3137

3238
# Create Database Models
3339
db.create_all()

‎flask_session_tutorial/assets.py

+12-20
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,41 @@
11
"""Compile static asset bundles."""
2-
from flask import Flask
3-
from flask_assets import Bundle, Environment
4-
5-
6-
def compile_static_assets(app: Flask):
7-
"""
8-
Compile all asset bundles.
92

10-
:param Flask app: Top-level Flask application.
11-
"""
12-
compile_stylesheets(app)
13-
compile_javascript(app)
3+
from flask import current_app as app
4+
from flask_assets import Bundle, Environment
145

156

16-
def compile_stylesheets(app: Flask):
7+
def compile_stylesheets(assets: Environment) -> Environment:
178
"""
189
Generate CSS from .LESS source.
1910
2011
:param Flask app: Top-level Flask application.
2112
"""
22-
assets = Environment(app)
2313
Environment.auto_build = True
2414
Environment.debug = False
2515
# Stylesheets Bundle
26-
stylesheet_bundle_account = Bundle(
16+
account_stylesheet_bundle = Bundle(
2717
"src/less/account.less",
2818
filters="less,cssmin",
2919
output="dist/css/account.css",
3020
extra={"rel": "stylesheet/less"},
3121
)
32-
stylesheet_bundle_dashboard = Bundle(
22+
dashboard_stylesheet_bundle = Bundle(
3323
"src/less/dashboard.less",
3424
filters="less,cssmin",
3525
output="dist/css/dashboard.css",
3626
extra={"rel": "stylesheet/less"},
3727
)
3828
# Register assets
39-
assets.register("styles_account", stylesheet_bundle_account)
40-
assets.register("styles_dashboard", stylesheet_bundle_dashboard)
29+
assets.register("styles_account", account_stylesheet_bundle)
30+
assets.register("styles_dashboard", dashboard_stylesheet_bundle)
4131
# Build assets in development mode
4232
if app.config.get("ENVIRONMENT") != "production":
43-
stylesheet_bundle_account.build(force=True)
44-
stylesheet_bundle_dashboard.build(force=True)
33+
account_stylesheet_bundle.build(force=True)
34+
dashboard_stylesheet_bundle.build(force=True)
35+
return assets
4536

4637

47-
def compile_javascript(app: Flask):
38+
def compile_javascript(assets: Environment) -> Environment:
4839
"""
4940
Bundle and minimize Javascript source files.
5041
@@ -60,3 +51,4 @@ def compile_javascript(app: Flask):
6051
# Build assets in development mode
6152
if app.config.get("ENVIRONMENT") != "production":
6253
js_bundle.build(force=True)
54+
return assets

‎flask_session_tutorial/auth.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Routes for user authentication."""
2+
23
from typing import Optional
34

45
from flask import Blueprint, Response, flash, redirect, render_template, request, url_for

‎flask_session_tutorial/forms.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Sign-up & log-in forms."""
2+
23
from flask_wtf import FlaskForm
34
from wtforms import PasswordField, StringField, SubmitField
45
from wtforms.validators import DataRequired, Email, EqualTo, Length, Optional

‎flask_session_tutorial/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Database models."""
2+
23
from flask_login import UserMixin
34
from werkzeug.security import check_password_hash, generate_password_hash
45

‎flask_session_tutorial/routes.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Routes for logged-in flask_session_tutorial."""
2+
23
from flask import Blueprint, Response, redirect, render_template, session, url_for
34
from flask_login import current_user, login_required, logout_user
45

‎flask_session_tutorial/static/src/less/dashboard.less

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ html {
2929
margin: auto;
3030
height: fit-content;
3131

32+
@media(max-width: @mobile-breakpoint) {
33+
width: 100%;
34+
margin: 0;
35+
}
36+
3237
h1 {
3338
font-family: 'agenda', sans-serif;
3439
font-weight: 600;

‎flask_session_tutorial/static/src/less/vars.less

+4
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@
2323

2424
// Animation
2525
@transition: all .3s ease-out;
26+
27+
// Breakpoints
28+
@tablet-breakpoint: 800px;
29+
@mobile-breakpoint: 600px;

‎gunicorn.conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Gunicorn configuration file."""
2+
23
from os import environ, path
34

45
from dotenv import load_dotenv

‎poetry.lock

+247-185
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ run = "wsgi:app"
3939
issues = "https://github.com/hackersandslackers/flask-session-tutorial/issues"
4040

4141
[build-system]
42-
requires = ["poetry-core>=1.0.0"]
42+
requires = ["poetry-core>=1.5.0"]
4343
build-backend = "poetry.core.masonry.api"
4444

4545
[tool.pylint.'MESSAGES CONTROL']

‎requirements.txt

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
1-
async-timeout==4.0.3 ; python_version >= "3.10" and python_full_version <= "3.11.2"
2-
black==23.12.1 ; python_version >= "3.10" and python_version < "4.0"
1+
async-timeout==4.0.3 ; python_version >= "3.10" and python_full_version < "3.11.3"
2+
black==24.4.0 ; python_version >= "3.10" and python_version < "4.0"
33
blinker==1.7.0 ; python_version >= "3.10" and python_version < "4.0"
4-
cachelib==0.10.2 ; python_version >= "3.10" and python_version < "4.0"
4+
cachelib==0.13.0 ; python_version >= "3.10" and python_version < "4.0"
55
click==8.1.7 ; python_version >= "3.10" and python_version < "4.0"
66
colorama==0.4.6 ; python_version >= "3.10" and python_version < "4.0" and platform_system == "Windows"
77
cssmin==0.2.0 ; python_version >= "3.10" and python_version < "4.0"
8-
dnspython==2.4.2 ; python_version >= "3.10" and python_version < "4.0"
9-
email-validator==2.1.0.post1 ; python_version >= "3.10" and python_version < "4.0"
8+
dnspython==2.6.1 ; python_version >= "3.10" and python_version < "4.0"
9+
email-validator==2.1.1 ; python_version >= "3.10" and python_version < "4.0"
1010
flake8==7.0.0 ; python_version >= "3.10" and python_version < "4.0"
1111
flask-assets==2.1.0 ; python_version >= "3.10" and python_version < "4.0"
1212
flask-login==0.6.3 ; python_version >= "3.10" and python_version < "4.0"
13-
flask-session==0.5.0 ; python_version >= "3.10" and python_version < "4.0"
13+
flask-session==0.8.0 ; python_version >= "3.10" and python_version < "4.0"
1414
flask-sqlalchemy==3.1.1 ; python_version >= "3.10" and python_version < "4.0"
1515
flask-wtf==1.2.1 ; python_version >= "3.10" and python_version < "4.0"
16-
flask==3.0.0 ; python_version >= "3.10" and python_version < "4.0"
16+
flask==3.0.3 ; python_version >= "3.10" and python_version < "4.0"
1717
greenlet==3.0.3 ; python_version >= "3.10" and python_version < "4.0" and (platform_machine == "aarch64" or platform_machine == "ppc64le" or platform_machine == "x86_64" or platform_machine == "amd64" or platform_machine == "AMD64" or platform_machine == "win32" or platform_machine == "WIN32")
1818
gunicorn==21.2.0 ; python_version >= "3.10" and python_version < "4.0"
19-
idna==3.6 ; python_version >= "3.10" and python_version < "4.0"
19+
idna==3.7 ; python_version >= "3.10" and python_version < "4.0"
2020
isort==5.13.2 ; python_version >= "3.10" and python_version < "4.0"
2121
itsdangerous==2.1.2 ; python_version >= "3.10" and python_version < "4.0"
22-
jinja2==3.1.2 ; python_version >= "3.10" and python_version < "4.0"
22+
jinja2==3.1.3 ; python_version >= "3.10" and python_version < "4.0"
2323
jsmin==3.0.1 ; python_version >= "3.10" and python_version < "4.0"
2424
lesscpy==0.15.1 ; python_version >= "3.10" and python_version < "4.0"
25-
markupsafe==2.1.3 ; python_version >= "3.10" and python_version < "4.0"
25+
markupsafe==2.1.5 ; python_version >= "3.10" and python_version < "4.0"
2626
mccabe==0.7.0 ; python_version >= "3.10" and python_version < "4.0"
27+
msgspec==0.18.6 ; python_version >= "3.10" and python_version < "4.0"
2728
mypy-extensions==1.0.0 ; python_version >= "3.10" and python_version < "4.0"
28-
packaging==23.2 ; python_version >= "3.10" and python_version < "4.0"
29+
packaging==24.0 ; python_version >= "3.10" and python_version < "4.0"
2930
pathspec==0.12.1 ; python_version >= "3.10" and python_version < "4.0"
30-
platformdirs==4.1.0 ; python_version >= "3.10" and python_version < "4.0"
31+
platformdirs==4.2.0 ; python_version >= "3.10" and python_version < "4.0"
3132
ply==3.11 ; python_version >= "3.10" and python_version < "4.0"
3233
pycodestyle==2.11.1 ; python_version >= "3.10" and python_version < "4.0"
3334
pyflakes==3.2.0 ; python_version >= "3.10" and python_version < "4.0"
3435
pymysql==1.1.0 ; python_version >= "3.10" and python_version < "4.0"
35-
python-dotenv==1.0.0 ; python_version >= "3.10" and python_version < "4.0"
36-
redis==5.0.1 ; python_version >= "3.10" and python_version < "4.0"
37-
sqlalchemy==2.0.25 ; python_version >= "3.10" and python_version < "4.0"
36+
python-dotenv==1.0.1 ; python_version >= "3.10" and python_version < "4.0"
37+
redis==5.0.3 ; python_version >= "3.10" and python_version < "4.0"
38+
sqlalchemy==2.0.29 ; python_version >= "3.10" and python_version < "4.0"
3839
tomli==2.0.1 ; python_version >= "3.10" and python_version < "3.11"
39-
typing-extensions==4.9.0 ; python_version >= "3.10" and python_version < "4.0"
40+
typing-extensions==4.11.0 ; python_version >= "3.10" and python_version < "4.0"
4041
webassets==2.0 ; python_version >= "3.10" and python_version < "4.0"
41-
werkzeug==3.0.1 ; python_version >= "3.10" and python_version < "4.0"
42+
werkzeug==3.0.2 ; python_version >= "3.10" and python_version < "4.0"
4243
wtforms==3.1.2 ; python_version >= "3.10" and python_version < "4.0"

‎wsgi.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""App entry point."""
2+
23
from flask_session_tutorial import create_app
34

45
app = create_app()

0 commit comments

Comments
 (0)
Please sign in to comment.