Skip to content
Snippets Groups Projects

Python3 Flask Template

A started template for Flask web apps using Python 3.8. This started template includes:

  • Dynamic frontend templates (not React)
  • Database support
  • Imperial LDAP user authentication
  • Multiple environments (development and production)

This template is useful if you would like to create a backend REST API, optionally coupled with a simple dynamic frontend.

Getting started

Once you have created your new app, take a few minutes to look through the files to familiarise yourself with the project structure.

  • app.py : entry point to the Flask app
  • templates/ : contains the frontend dynamic HTML files
  • static/ : contains the static frontend assets (images, stylesheets and scripts)
  • blueprints/ : contains all the application routes
  • models/ : contains all the database models
  • database/ : contains the database creation
  • config/ : contains the app settings for the different environments

The first change you should make is to set the APP_NAME variable in config/config.py to whatever you app's name is.

To start the application locally, you can just run python3 app.py and this will launch the app on port 5000 (by default). You will notice a message in the console saying:

WARNING: Could not connect to the given database URL!

To fix this, you should set the environment variable DATABASE_URL accordingly. If you have PostgreSQL running locally, you can use that. Alternatively, you could use SQLite which is much simpler and does not require installation.

If you do not want to use a database yet, you can ignore this warning and delete any routes that interact with the database.

If you navigate to http://localhost:5000, you will see the response created by the route defined in blueprints/home.py.

You will also notice the lines Environment: production and Debug mode: off when the Flask application starts in the console. To enable debug mode, you must set the environment variable ENV to dev, ie: export ENV=dev (see config/config.py for more details on different environments).

Tutorial 1: Adding a new route

Tutorial 2: Adding database interaction

Tutorial 3: Configuring a test environment