Simple template for a Flask application including MySQL database and Docker
.gitea | ||
db | ||
flaskapp | ||
static | ||
templates | ||
.env | ||
.env_debug | ||
.gitignore | ||
app.py | ||
docker-compose.yml | ||
Dockerfile | ||
Makefile | ||
pylintrc | ||
README.md | ||
requirements.txt |
Flask App Template
It is a simple Gitea template for a Flask web application.
Features
- Basic Flask app
@route
s 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
- Create a repository from this template
- For debugging, edit
.env_debug
file corresponding to configuration of MySQL/MariaDB installed on your PC - Edit
pages.py
: add your own Flask routes - Edit or delete
admin.py
(its content is similar topages.py
) - Create your own Jinja templates in
templates/
directory, I recommend to editbase.html
and inherit other templates from it - Edit or delete
forms.py
(it is used for storing wtforms classes) - Edit routes, error pages and (if needed) add other Flask extensions, as described in the next section
- Edit
db/schema.sql
corresponding to your database structure - Check
Makefile
,Dockerfile
,docker-compose.yml
- Run formatter and linters (
make format
, thenmake check
)
Add routes, exts, error pages
To use your own routes file:
- Create a file with the same content as
pages.py
- Give the
RoutePages
class another name - Edit
add_routes
method - Then, open
app.py
- Find the comment
# Add your routes...
- Import the created module (routes file)
- Add the routes class from the module
to the
routes
list
Custom error pages
- Open
errors.py
- Find the comment
# Add other...
- Edit the list below corresponding to which HTTP error pages you want to customize
- Create templates for these errors,
e.g.
404.html
for custom 404 page
Other Flask extensions
- Open
exts.py
- Find the comment
# Add your extensions...
- Create extensions objects above, like for Flask-MySQL
- 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.