Skip to content

Commit

Permalink
Upgrade to Python 3.10; resolve dependency changes; minor refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
toddbirchard committed Dec 16, 2023
1 parent b02521d commit 147f164
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 170 deletions.
9 changes: 2 additions & 7 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
ENVIRONMENT=production
FLASK_DEBUG=false
SECRET_KEY=randomstringofcharacters

LESS_BIN=/usr/local/bin/lessc
ASSETS_DEBUG=False
LESS_RUN_IN_DEBUG=False
COMPRESSOR_DEBUG=False
FLASK_DEBUG=False
SECRET_KEY=randomstringofcharacters
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
select = E9,F63,F7,F82
exclude = .git,.github,__pycache__,.pytest_cache,.venv,logs,creds
max-line-length = 120
42 changes: 20 additions & 22 deletions .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,30 @@ name: Python application

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.10"
cache: "pip" # caching pip dependencies

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ $(VIRTUAL_ENV):

.PHONY: run
run: env
$(LOCAL_PYTHON) -m gunicorn -w 4 wsgi:app
$(LOCAL_PYTHON) -m gunicorn -w 4 wsgi:app

.PHONY: install
install: env
Expand Down
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Flask Blueprint Tutorial

![Python](https://img.shields.io/badge/Python-v^3.10-blue.svg?logo=python&longCache=true&logoColor=white&colorB=5e81ac&style=flat-square&colorA=4c566a)
![Flask](https://img.shields.io/badge/Flask-v2.2.2-blue.svg?longCache=true&logo=flask&style=flat-square&logoColor=white&colorB=5e81ac&colorA=4c566a)
![Flask-Assets](https://img.shields.io/badge/Flask--Assets-v2.0-blue.svg?longCache=true&logo=flask&style=flat-square&logoColor=white&colorB=5e81ac&colorA=4c566a)
![Python](https://img.shields.io/badge/Python-v3.10-blue.svg?logo=python&longCache=true&logoColor=white&colorB=5e81ac&style=flat-square&colorA=4c566a)
![Flask](https://img.shields.io/badge/Flask-v3.0.0-blue.svg?longCache=true&logo=flask&style=flat-square&logoColor=white&colorB=5e81ac&colorA=4c566a)
![Flask-Assets](https://img.shields.io/badge/Flask--Assets-v2.1.0-blue.svg?longCache=true&logo=flask&style=flat-square&logoColor=white&colorB=5e81ac&colorA=4c566a)
![Gunicorn](https://img.shields.io/badge/Gunicorn-v21.2.0-blue.svg?longCache=true&logo=gunicorn&style=flat-square&logoColor=white&colorB=a3be8c&colorA=4c566a)
![GitHub Last Commit](https://img.shields.io/github/last-commit/google/skia.svg?style=flat-square&colorA=4c566a&colorB=a3be8c&logo=GitHub)
[![GitHub Issues](https://img.shields.io/github/issues/hackersandslackers/flask-blueprint-tutorial.svg?style=flat-square&colorA=4c566a&logo=GitHub&colorB=ebcb8b)](https://github.com/hackersandslackers/flask-blueprint-tutorial/issues)
[![GitHub Stars](https://img.shields.io/github/stars/hackersandslackers/flask-blueprint-tutorial.svg?style=flat-square&colorA=4c566a&logo=GitHub&colorB=ebcb8b)](https://github.com/hackersandslackers/flask-blueprint-tutorial/stargazers)
Expand All @@ -24,12 +25,8 @@ Get set up locally in two steps:
Replace the values in **.env.example** with your values and rename this file to **.env**:

* `ENVIRONMENT`: The environment in which to run your application (either `development` or `production`).
* `FLASK_APP`: Entry point of your application; should be `wsgi.py`.
* `FLASK_DEBUG`: Set to `True` to enable Flask's debug mode (default to `False` in prod).
* `SECRET_KEY`: Randomly generated string of characters used to encrypt your app's data.
* `LESS_BIN` *(optional for static assets)*: Path to your local LESS installation via `which lessc`.
* `ASSETS_DEBUG` *(optional)*: Debug asset creation and bundling in `development`.
* `LESS_RUN_IN_DEBUG` *(optional)*: Debug LESS while in `development`.
* `COMPRESSOR_DEBUG` *(optional)*: Debug asset compression while in `development`.

*Remember never to commit secrets saved in .env files to Github.*

Expand All @@ -40,7 +37,7 @@ Get up and running with `make run`:
```shell
git clone https://github.com/hackersandslackers/flask-blueprint-tutorial.git
cd flask-blueprint-tutorial
make run
make deploy
```

-----
Expand Down
16 changes: 9 additions & 7 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Class-based Flask app configuration."""
from os import environ, path
from os import environ, path, system

from dotenv import load_dotenv

Expand All @@ -11,22 +11,24 @@ class Config:
"""Configuration from environment variables."""

# General Config\
ENVIRONMENTS = environ.get("ENVIRONMENT")
ENVIRONMENT = environ.get("ENVIRONMENT")

# Flask Config
SECRET_KEY = environ.get("SECRET_KEY")
FLASK_DEBUG = environ.get("FLASK_DEBUG")
FLASK_APP = "wsgi.py"

# Flask-Assets
LESS_BIN = environ.get("LESS_BIN")
ASSETS_DEBUG = False
LESS_RUN_IN_DEBUG = False

# Static Assets
STATIC_FOLDER = "static"
TEMPLATES_FOLDER = "templates"
COMPRESSOR_DEBUG = False

# Flask-Assets
LESS_BIN = system("which lessc")
ASSETS_DEBUG = False
LESS_RUN_IN_DEBUG = False
if ENVIRONMENT == "development" and LESS_BIN is None:
raise ValueError("Application running in `development` mode cannot create assets without `lessc` installed.")

# Hardcoded data
PRODUCT_DATA_FILEPATH = f"{BASE_DIR}/data/products.json"
12 changes: 9 additions & 3 deletions flask_blueprint_tutorial/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
from flask_assets import Bundle


def compile_static_assets(assets):
"""Create stylesheet bundles."""
def compile_static_assets(assets: Bundle) -> Bundle:
"""
Create CSS stylesheet bundles from .less files.
:param Bundle assets: Static asset bundle.
:returns: Bundle
"""
assets.auto_build = True
assets.debug = False
common_style_bundle = Bundle(
Expand Down Expand Up @@ -35,7 +41,7 @@ def compile_static_assets(assets):
assets.register("home_style_bundle", home_style_bundle)
assets.register("profile_style_bundle", profile_style_bundle)
assets.register("product_style_bundle", product_style_bundle)
if app.config["ENVIRONMENTS"] == "development":
if app.config["ENVIRONMENT"] == "development":
common_style_bundle.build()
home_style_bundle.build()
profile_style_bundle.build()
Expand Down
24 changes: 18 additions & 6 deletions flask_blueprint_tutorial/home/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@


@home_blueprint.route("/", methods=["GET"])
def home():
"""Render application Homepage."""
def home() -> str:
"""
Serve `Home` page template.
:returns: str
"""
products = fetch_products(app)
return render_template(
"index.jinja2",
Expand All @@ -23,8 +27,12 @@ def home():


@home_blueprint.route("/about", methods=["GET"])
def about():
"""Render static `about` page."""
def about() -> str:
"""
Serve `About` page template.
:returns: str
"""
return render_template(
"index.jinja2",
title="About",
Expand All @@ -34,8 +42,12 @@ def about():


@home_blueprint.route("/contact", methods=["GET"])
def contact():
"""Render page."""
def contact() -> str:
"""
Serve `Contact` page template.
:returns: str
"""
return render_template(
"index.jinja2",
title="Contact",
Expand Down
11 changes: 8 additions & 3 deletions flask_blueprint_tutorial/products/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@


@product_bp.route("/products/<int:product_id>/", methods=["GET"])
def product_page(product_id):
"""Product description page."""
products_json = app.config["FLASK_ENV"]
def product_page(product_id: int) -> str:
"""
Product detail page for a given product ID.
:params int product_id: Unique product ID.
:returns: str
"""
product = fetch_products(app)[product_id]
return render_template(
"products.jinja2",
Expand Down
8 changes: 6 additions & 2 deletions flask_blueprint_tutorial/profile/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@


@profile_blueprint.route("/profile", methods=["GET"])
def user_profile():
"""Logged-in user profile page."""
def user_profile() -> str:
"""
Logged-in user profile page.
:returns: str
"""
user = fake.simple_profile()
job = fake.job()
return render_template(
Expand Down
Loading

0 comments on commit 147f164

Please sign in to comment.