Search notes:

Python library: Flask

Installation

On Ubuntu/Debian or similar distributions with APT:
$ python3 -m venv virtual-env
$ . virtual-env/bin/activate
$ pip install Flask

Hello world application

helloWorld.py

We write the application code for our Hello world application in a file named helloWorld.py:
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

Running the application

The application can then be started by setting the enviornment variable FLASK_APP to the name of the Python script we created and executing flask run:
$ FLASK_APP=helloWorld.py flask run
 * Serving Flask app 'helloWorld.py'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
The application is now visible on a browser by connnecting to http://localhost:5000.
Note the warning!

Running with Gunicorn

If flask is installed into a virtual environment, Gunicorn needs to be installed into the same virtual environment:
$ pip install gunicorn
The hello world application can now be started with:
$ gunicorn helloWorld:app
The application is now visible on a browser by connnecting to http://localhost:8000.

Flask depends/builds on Werkzeug

$ python3 -m flask --version
Python 3.9.2
Flask 3.0.0
Werkzeug 3.0.1

TODO

Flask comes with the Jinja template engine by default, but it's possible to choose another one.

See also

WSGI

Index