Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications.
Classified as a microframework, Flask is written in Python and it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.
To use the stater, Python3 should be installed properly in the workstation. If you are not sure if Python is properly installed, please open a terminal and type python --version. The full-list with dependencies and tools required to build the app:
Python3 - the programming language used to code the app
GIT - used to clone the source code from the Github repository
Basic development tools (g++ compiler, python development libraries ..etc) used by Python to compile the app dependencies in your environment.
For more information on how to set up your environment please access the resources listed below. In case we’ve missed something, contact us on Discord.
To built and start the app locally, follow the steps:
Get the source code
Download the ZIP from Github Repository
Using GIT tool in the terminal to clone the source code
Change the current directory to source code directory
At this point, we can visit the app in the browser http://127.0.0.1:5000/.
By default, the app will redirect guest users to the login page. To access the private pages:
Create a new user using the registration page
Authenticate using the login page
App Codebase (simplified)
Starter uses a simple codebase (no Blueprints) with a structure presented bellow:
The bootstrap flow
run.py loads the .env file
Initialize the app using the specified profile: Debug or Production
If env.DEBUG is set to True the SQLite storage is used
If env.DEBUG is set to False the specified DB driver is used (MySql, PostgreSQL)
Call the app factory method create_app defined in app/init.py
Redirect the guest users to Login page
Unlock the pages served by home blueprint for authenticated users
.env (saved in the root of the project)
run.py (simplified version)
app/__init__.py (simplified version)
The app/__init__.py constructs the app by putting together a short-list of things:
Invoke SQLAlchemy
Invoke and inject the Login Manager into the app
Load the configuration from config.py file
Register the app blueprints
Check if the database tables are created
return the WSGI app
App Codebase
The starter defines two blueprints:
Base blueprint - handles the authentication (routes and forms) and assets management
Home blueprint - serve HTML pages for authenticated users
App / Base Blueprint structure
App / Home Blueprint structure
App Configuration
The configuration file config.py (in the root of the project) define a dual configuration controlled via the .env file ( DEBUG variable)
DebugConfig - default configuration used for development
This configuration becomes active if .env file has the DEBUG file set to True
During the first request, the SQLite database and tables are automatically created in the root in the project.
Hint: to visualize the SQLite database content an external tool should be installed: DB Browser for SQLite it might be a good choice.
ProductionConfig - production configuration
This configuration becomes active if .env file has the DEBUG file set to False
In this configuration profile, the database defaults to a PostgreSQL DBMS. Make sure the .env has the right credentials to access the database.
App Tables
The file app/base/models.py (Base Blueprint) defines the table(s) used by the application. Being a simple starter, by default the following tabes are defined:
Table #1 - User with fields:
Id - Primary key, unique
user - Store the username
email - The email address
password - Hashed password
App Forms
The file app/base/forms.py (Base Blueprint) defines the table(s) used by the application. Being a simple starter, by default the following forms are defined:
Form #1 - LoginForm with fields:
username
password
Form #2 - RegisterForm with fields:
username - used to authenticate
email - email address
password - used to authenticate
App Routing
The routing rules are defined by Base and Home blueprints as specified below. This is the public zone of the app.
Base Blueprint - routing file app/base/routes.py
/login route is resolved by login() method
/register route is resolved by register() method
/logout route calls the logout_user() defined in flask_login
Registered ERROR handlers
404 Error - Page not found
403 Error - Access Forbidden
500 Error - Internal Error
Home Blueprint - routing file app/home/routes.py
This blueprint will serve requested pages from app/home/templates directory to authenticated users.
The authentication status is checked by @login_required decorator.
/<template> route resolved by route_template().
If a requested page is not found a default 404 page is returned to the user
Pages & Assets
Pages and all assets defined in the UI Kits are served by the app using both blueprints:
Home Blueprint manage the static assets - app/base/static/assets
Home Blueprint store the layout master pages, HTML chunks (footer. header, scripts) and login, registration pages
Base Blueprint serve the HTML pages (index, page-404, etc) and the rest of the pages defined in the UI kit.
Data Structures
The Flask starter exposes a short-list with data structures used globally across the app:
current_user object
Constructed by Flask-Login can be used to detect if the current request is executed by an authenticated user or not. The object has global visibility and can be used in all app controllers and handlers but also in views.
How it works
app/base/models.py define the callback functions required by Flask-Login library: