Skip to content

Commit

Permalink
Refactor flask-assets.
Browse files Browse the repository at this point in the history
  • Loading branch information
toddbirchard committed Apr 15, 2024
1 parent ef2431e commit 99229e1
Show file tree
Hide file tree
Showing 15 changed files with 304 additions and 227 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ENVIRONMENT=production
FLASK_DEBUG=True
SECRET_KEY=YourSecretKey

REDIS_URI=redis://:[email protected]:1234
REDIS_URI="redis://:[email protected]:1234"

SQLALCHEMY_DATABASE_URI=mysql+pymysql://myuser:[email protected]:1234/mydatabase
SQLALCHEMY_CA_CERTS="./creds/certificate.crt"
Expand Down
3 changes: 2 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""App configuration."""
from os import environ, path

import subprocess
from os import environ, path

import redis
from dotenv import load_dotenv
Expand Down
10 changes: 8 additions & 2 deletions flask_session_tutorial/__init__.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
"""Initialize app."""

from flask import Flask
from flask_assets import Environment
from flask_login import LoginManager
from flask_session import Session
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
login_manager = LoginManager()
session = Session()
assets = Environment()


def create_app():
"""Construct the core flask_session_tutorial."""
app = Flask(__name__, instance_relative_config=False)
app.config.from_object("config.Config")
assets = Environment()

# Initialize Plugins
db.init_app(app)
login_manager.init_app(app)
session.init_app(app)
assets.init_app(app)

with app.app_context():
from . import auth, routes
from .assets import compile_static_assets
from .assets import compile_javascript, compile_stylesheets

app.register_blueprint(routes.main_blueprint)
app.register_blueprint(auth.auth_blueprint)

# Create static asset bundles
compile_static_assets(app)
compile_stylesheets(assets)
compile_javascript(assets)

# Create Database Models
db.create_all()
Expand Down
32 changes: 12 additions & 20 deletions flask_session_tutorial/assets.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
"""Compile static asset bundles."""
from flask import Flask
from flask_assets import Bundle, Environment


def compile_static_assets(app: Flask):
"""
Compile all asset bundles.

:param Flask app: Top-level Flask application.
"""
compile_stylesheets(app)
compile_javascript(app)
from flask import current_app as app
from flask_assets import Bundle, Environment


def compile_stylesheets(app: Flask):
def compile_stylesheets(assets: Environment) -> Environment:
"""
Generate CSS from .LESS source.
:param Flask app: Top-level Flask application.
"""
assets = Environment(app)
Environment.auto_build = True
Environment.debug = False
# Stylesheets Bundle
stylesheet_bundle_account = Bundle(
account_stylesheet_bundle = Bundle(
"src/less/account.less",
filters="less,cssmin",
output="dist/css/account.css",
extra={"rel": "stylesheet/less"},
)
stylesheet_bundle_dashboard = Bundle(
dashboard_stylesheet_bundle = Bundle(
"src/less/dashboard.less",
filters="less,cssmin",
output="dist/css/dashboard.css",
extra={"rel": "stylesheet/less"},
)
# Register assets
assets.register("styles_account", stylesheet_bundle_account)
assets.register("styles_dashboard", stylesheet_bundle_dashboard)
assets.register("styles_account", account_stylesheet_bundle)
assets.register("styles_dashboard", dashboard_stylesheet_bundle)
# Build assets in development mode
if app.config.get("ENVIRONMENT") != "production":
stylesheet_bundle_account.build(force=True)
stylesheet_bundle_dashboard.build(force=True)
account_stylesheet_bundle.build(force=True)
dashboard_stylesheet_bundle.build(force=True)
return assets


def compile_javascript(app: Flask):
def compile_javascript(assets: Environment) -> Environment:
"""
Bundle and minimize Javascript source files.
Expand All @@ -60,3 +51,4 @@ def compile_javascript(app: Flask):
# Build assets in development mode
if app.config.get("ENVIRONMENT") != "production":
js_bundle.build(force=True)
return assets
1 change: 1 addition & 0 deletions flask_session_tutorial/auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Routes for user authentication."""

from typing import Optional

from flask import Blueprint, Response, flash, redirect, render_template, request, url_for
Expand Down
1 change: 1 addition & 0 deletions flask_session_tutorial/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Sign-up & log-in forms."""

from flask_wtf import FlaskForm
from wtforms import PasswordField, StringField, SubmitField
from wtforms.validators import DataRequired, Email, EqualTo, Length, Optional
Expand Down
1 change: 1 addition & 0 deletions flask_session_tutorial/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Database models."""

from flask_login import UserMixin
from werkzeug.security import check_password_hash, generate_password_hash

Expand Down
1 change: 1 addition & 0 deletions flask_session_tutorial/routes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Routes for logged-in flask_session_tutorial."""

from flask import Blueprint, Response, redirect, render_template, session, url_for
from flask_login import current_user, login_required, logout_user

Expand Down
5 changes: 5 additions & 0 deletions flask_session_tutorial/static/src/less/dashboard.less
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ html {
margin: auto;
height: fit-content;

@media(max-width: @mobile-breakpoint) {
width: 100%;
margin: 0;
}

h1 {
font-family: 'agenda', sans-serif;
font-weight: 600;
Expand Down
4 changes: 4 additions & 0 deletions flask_session_tutorial/static/src/less/vars.less
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@

// Animation
@transition: all .3s ease-out;

// Breakpoints
@tablet-breakpoint: 800px;
@mobile-breakpoint: 600px;
1 change: 1 addition & 0 deletions gunicorn.conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Gunicorn configuration file."""

from os import environ, path

from dotenv import load_dotenv
Expand Down
Loading

0 comments on commit 99229e1

Please sign in to comment.