Blueprint Collectors

Flask Smore Better defines blueprint collectors that collect blueprints in one list to be used to register them with the Flask application. All blueprint collectors use a module name to determine where to look for blueprints.

Structuring Application Blueprints

There are a few ways to structure blueprints to take advantage of blueprint collectors, module or package level blueprints are the most common.

Module Level Blueprints

Module level blueprints are blueprints that are defined in a single module. This is the simplest way to define blueprints and is suitable for small applications:

routes.py
from flask_smore_better.blueprint import Blueprint

blueprint = Blueprint("my_blueprint", __name__)
...

Note

If you are using plugins, they must be set up in the same module as the blueprints to ensure they are collected without errors. See Using Plugins for more information.

Package Level Blueprints

Package level blueprints are blueprints that are defined in a package. This is useful for larger applications where blueprints are grouped by functionality.

This example assumes the following directory structure:

routes/
    __init__.py
    group_a.py
    group_b.py

The routes/__init__.py file imports the blueprints from the submodules, and the submodules define the blueprints:

routes/__init__.py
from flask_smore_better.blueprint import Blueprint

# Use the noqa comment to prevent these from being removed by
# pyflakes or similar tools
from .group_a import blueprint_a  # noqa: F401
from .group_b import blueprint_b  # noqa: F401
routes/group_a.py
from flask_smore_better.blueprint import Blueprint

blueprint_a = Blueprint("group_a", __name__)
...
routes/group_b.py
from flask_smore_better.blueprint import Blueprint

blueprint_b = Blueprint("group_b", __name__)
...

Note

If you are using plugins, they must be set up in the __init__.py file to ensure they are collected without errors. See Using Plugins for more information.

DevBlueprintCollector

The DevBlueprintCollector is a blueprint collector that finds blueprints in a module/package by looking for all attributes that are instances of flask.Blueprint in the given module/package and returns them in a list. This collector is useful for development environments where blueprints are changed frequently and speed is not a concern.

Simply create a new instance of the collector and call it with the module name:

from flask_smore_better.blueprint import DevBlueprintCollector

collector = DevBlueprintCollector()
blueprints = collector.load("my_app.routes")

PyCachedBlueprintCollector

The PyCachedBlueprintCollector is a blueprint collector that collects blueprints using a cache of the module/package, which is a python module. This cache file can be generated using the generate-blueprint-cache command. This collector is useful for production environments where speed is a concern.

Create a new instance of the collector with the folder containing the cache file and call it with the module name:

from flask_smore_better.blueprint import PyCachedBlueprintCollector

collector = PyCachedBlueprintCollector("/path/to/cache/folder")
blueprints = collector.load("my_app.routes")