Simple template for a Flask application including MySQL database and Docker
Find a file Use this template
2023-02-17 17:51:26 +04:00
.gitea Added template files 2023-02-17 15:57:01 +04:00
db Working with DB, admin form, WTForms, .env 2023-02-16 18:19:58 +04:00
flaskapp Full Readme, .env_debug 2023-02-17 17:49:41 +04:00
static Working with DB, admin form, WTForms, .env 2023-02-16 18:19:58 +04:00
templates Working with DB, admin form, WTForms, .env 2023-02-16 18:19:58 +04:00
.env Docker 2023-02-16 18:54:45 +04:00
.env_debug Full Readme, .env_debug 2023-02-17 17:49:41 +04:00
.gitignore Removed .env from gitignore 2023-02-17 16:21:02 +04:00
app.py Improved structure 2023-02-15 16:00:30 +04:00
docker-compose.yml Docker-Compose DB bugfix 2023-02-17 17:51:26 +04:00
Dockerfile Working Dockerfile and Compose.yml, makefile docker cmd 2023-02-17 15:55:52 +04:00
Makefile Docker build command bugfix 2023-02-17 16:14:45 +04:00
pylintrc Implemented get_db for MySQL, wrapped all routes in classes 2023-02-15 19:55:06 +04:00
README.md Full Readme, .env_debug 2023-02-17 17:49:41 +04:00
requirements.txt Working with DB, admin form, WTForms, .env 2023-02-16 18:19:58 +04:00

Flask App Template

It is a simple Gitea template for a Flask web application.

Features

  • Basic Flask app
  • @routes are defined in classes inherited from flaskapp.routes.Routes, so they can be separated into multiple modules
  • Flask WTForms extension
  • MySQL/MariaDB database
  • Gunicorn for production deployment
  • AutoPEP8 formatter, MyPy and Pylint
  • Dockerfile, compose YAML config

Usage

  1. Create a repository from this template
  2. For debugging, edit .env_debug file corresponding to configuration of MySQL/MariaDB installed on your PC
  3. Edit pages.py: add your own Flask routes
  4. Edit or delete admin.py (its content is similar to pages.py)
  5. Create your own Jinja templates in templates/ directory, I recommend to edit base.html and inherit other templates from it
  6. Edit or delete forms.py (it is used for storing wtforms classes)
  7. Edit routes, error pages and (if needed) add other Flask extensions, as described in the next section
  8. Edit db/schema.sql corresponding to your database structure
  9. Check Makefile, Dockerfile, docker-compose.yml
  10. Run formatter and linters (make format, then make check)

Add routes, exts, error pages

To use your own routes file:

  1. Create a file with the same content as pages.py
  2. Give the RoutePages class another name
  3. Edit add_routes method
  4. Then, open app.py
  5. Find the comment # Add your routes...
  6. Import the created module (routes file)
  7. Add the routes class from the module to the routes list

Custom error pages

  1. Open errors.py
  2. Find the comment # Add other...
  3. Edit the list below corresponding to which HTTP error pages you want to customize
  4. Create templates for these errors, e.g. 404.html for custom 404 page

Other Flask extensions

  1. Open exts.py
  2. Find the comment # Add your extensions...
  3. Create extensions objects above, like for Flask-MySQL
  4. Then edit the list below the comment corresponding to the added extensions

Makefile

Make commands:

Command Description
make format Format the code using AutoPEP8
make check Check the code with linters (MyPy, Pylint)
make dev Run the app in development mode
make prod Run production server
make docker Build a docker image from Dockerfile
make clean Clean all cache

Publishing app

First of all, build an image: make docker
Follow this documentation page to upload your image to Docker Hub.