Flask Configurations
This introductory chapter will help us understand the different ways Flask can be configured to suit the various needs of a project. Flask is “The Python micro framework for building web applications” (pallets/Flask, https://github.com/pallets/flask).
So, why is Flask called a microframework? Does this mean Flask lacks functionality, or that it’s mandatory for the complete code of your web application to be contained in a single file? Not really! The term microframework simply refers to the fact that Flask aims to keep the core of its framework small but highly extensible. This makes writing applications or extensions both easy and flexible and gives developers the power to choose the configurations they want for their application without imposing any restrictions on the choice of database, templating engine, admin interface, and so on. In this chapter, you will learn several ways to set up and configure Flask.
Important information
This whole book uses Python 3 as the default version of Python. Python 2 lost its support on December 31, 2019, and is therefore not supported in this book. It is recommended that you use Python 3 while learning from this book, as many of the recipes might not work on Python 2.
Likewise, while writing this book, Flask 2.2.x was the latest version. Although a lot of code in this book can work on earlier versions of Flask, it is recommended that you use versions 2.2.x and above.
Getting started with Flask takes just a couple of minutes. Setting up a simple Hello World application is as easy as pie. Simply create a file, such as app.py,
in any location on your computer that can access python
or python3
that contains the following script:
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello to the World of Flask!' if __name__ == '__main__': app.run()
Now, Flask needs to be installed; this can be done via pip
or pip3
. You may have to use sudo
on a Unix-based machine if you run into access issues:
$ pip3 install Flask
Important
The code and Flask installation example here is just intended to demonstrate the ease with which Flask can be used. To set up a proper development environment, follow the recipes in this chapter.
The preceding snippet is a complete Flask-based web application. Here, an instance of the imported Flask
class is a Web Server Gateway Interface (WSGI) (http://legacy.python.org/dev/peps/pep-0333/) application. So, app
in this code becomes our WSGI application, and as this is a standalone module, we set the __name__
string to '__main__'
. If we save this in a file called app.py
, then the application can simply be run using the following command:
$ python3 app.py * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
Now, if we head over to our browser and type http://127.0.0.1:5000/
, we can see our application running.
Alternatively, the application can be run by using flask run
or Python’s -m
switch with Flask. While following this approach, the last two lines of app.py
can be skipped. Note that the following commands work only if there is a file named app.py
or wsgi.py
in the current directory. If not, then the file containing the app
object should be exported as an environment variable, namely FLASK_APP
. As a best practice, this should be done in either case:
$ export FLASK_APP=app.py $ flask run * Running on http://127.0.0.1:5000/
Alternatively, if you decide to use the -m
switch, it will look as follows:
$ export FLASK_APP=app.py $ python3 -m flask run * Running on http://127.0.0.1:5000/
Tip
Never save your application file as flask.py
; if you do, it will conflict with Flask itself while importing.
In this chapter, we will cover the following recipes:
- Setting up our environment with
virtualenv
- Handling basic configurations
- Configuring class-based settings
- Organizing static files
- Being deployment-specific with the
instance
folder - Compositions of views and models
- Creating a modular web app with blueprints
- Making a Flask app installable using
setuptools