Flask framework

The Flask extension streamlines the process of building Flask application rocks.

It facilitates the installation of Flask application dependencies, including Gunicorn, inside the rock. Additionally, it transfers your project files to /flask/app within the rock. By default, the system foundation, or base, is set as bare to generate a lightweight image.

Note

The Flask extension is compatible with the bare, ubuntu@22.04 and ubuntu@24.04 bases.

The Flask extension supports both synchronous and asynchronous Gunicorn workers.

Project requirements

There are 2 requirements to be able to use the flask-framework extension:

  1. There must be a requirements.txt or pyproject.toml file in the root of the project with Flask declared as a dependency

  2. The project must include a Web Server Gateway Interface (WSGI) app with the path app:app. This means there must be an app.py file at the root of the project where the Flask object is named app.

For the project to make use of asynchronous Gunicorn workers:

  • The requirements.txt or pyproject.toml file must include gevent as a dependency.

App dependencies

The stage-packages key specifies all additional dependencies. If the Flask app has its own special dependencies, this key must declare them.

The following example specifies the libpq-dev package:

rockcraft.yaml
parts:
  flask-framework/dependencies:
    stage-packages:
      # list required packages or slices for your flask app below.
      - libpq-dev

StatsD exporter

A StatsD exporter is installed alongside the Gunicorn server to record server metrics. Some of the Gunicorn-provided metrics are mapped to new names:

Gunicorn metric

StatsD metric

gunicorn.request.status.*

flask_response_code

gunicorn.requests

flask_requests

gunicorn.request.duration

flask_request_duration

The exporter listens on localhost at port 9125. You can push your own metrics to the exporter using any StatsD client. This snippet from an example Flask app uses pystatsd as a client:

import statsd
c = statsd.StatsClient('localhost', 9125)
c.incr('my_counter')

See the StatsD exporter documentation for more information.

Gunicorn worker selection

If the project has gevent as a dependency, Rockcraft automatically updates the pebble plan to spawn asynchronous Gunicorn workers.

When the project instead needs synchronous workers, you can override the worker type by adding --args flask sync to the Docker command that launches the rock:

docker run --name flask-container -d -p 8000:8000 flask-image:1.0 \
--args flask sync

Included or excluded files

Some files, if they exist, are included by default in the rock. These include: app, app.py, migrate, migrate.sh, migrate.py, static, templates.

The prime key specifies the files to be included or excluded from the rock upon rockcraft pack, following the app/<filename> notation. For example:

rockcraft.yaml
parts:
  flask-framework/install-app:
    prime:
      - flask/app/.env
      - flask/app/app.py
      - flask/app/webapp
      - flask/app/templates
      - flask/app/static

The prime key supports glob patterns to define the list of files. See Filesets for the various ways you can specify files in your rock.

Adding the prime key to the project file overrides the default files to be included. Files are excluded from the rock by defining prime and omitting the file to be excluded.