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 environment 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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758206304, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/libraries/Flask(98): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78