Flask-Blueprints-Loader’s documentation

GitHub Workflow Status (with event) Codecov

Flask-Blueprints-Loader is an extension for Flask that automatically discovers and registers Blueprint for your application.

Features

  • Automatic loading and registration of Flask blueprints.

  • Easy configuration via Flask app configuration variables.

Why Use Flask-Blueprints-Loader?

Flask-Blueprints-Loader is particularly useful for Large Applications as Packages that heavily rely on blueprints. Instead of manually registering each blueprint, this extension allows you to automate the process by automatically discovering and registering all blueprints for your application.

Installation

Flask-Blueprints-Loader is available on PyPI and can be installed with various Python Application dependency management tools. Install using pip:

$ pip install flask-blueprints-loader

Initialize the Extension

Application Instance Pattern

from flask import Flask
from flask_blueprints_loader import BlueprintsLoader

app = Flask(__name__)
loader = BlueprintsLoader(app)

with app.app_context():
   loader.load_blueprints()

Application Factories Pattern

from flask import Flask
from flask_blueprints_loader import BlueprintsLoader

loader = BlueprintsLoader()

def create_app():
   app = Flask(__name__)
   loader.init_app(app)

   with app.app_context():
      loader.load_blueprints()

   return app

Directory Structure

Flask-Blueprints-Loader expects your blueprints to be organized in a specific directory structure. By default, it looks for blueprints under the current_app.root_path directory, with each blueprint in its own package containing a views.py module that defines the Blueprint instance and route definitions.

Here is a structured Flask application with two blueprints, auth and blog

myapp/
├── __init__.py
├── auth/
│ ├── __init__.py
│ └── views.py
├── blog/
│ ├── __init__.py
│ └── views.py
├── templates/
│ ├── auth/
│ │ ├── login.html
│ │ └── register.html
│ └── blog/
│ ├── index.html
│ └── post.html
├── config.py
└── wsgi.py

Configuration

BLUEPRINTS_LOADER_PATH_NAME: str = ""

The name of the subdirectory containing the blueprints. By default blueprints are under the current_app.root_path.

BLUEPRINTS_LOADER_MODULE_NAME: str = views

The name of the module containing the blueprints. By default, it is set to “views”.

BLUEPRINTS_LOADER_UNLOADS: list = []

A list of blueprint names to exclude from loading. By default, it is an empty list.

Constraints and Drawbacks

Flask-Blueprints-Loader has the following constraints and drawbacks:

  • Blueprints are expected to be organized in a specific way, with a views.py module containing the Blueprint instance in each blueprint package.

  • If your blueprints are not organized in this way, you may need to make changes to your code to use Flask-Blueprints-Loader.

API Reference

class flask_blueprints_loader.blueprints_loader.BlueprintsLoader(app=None)

Automatically discover and register Flask blueprints.

The BlueprintsLoader class provides functionality for dynamically loading and registering Flask blueprints.

Parameters:

app (Flask | None) – The Flask application instance. (optional)

path_name

The name of the directory containing the blueprints.

module_name

The name of the module containing the blueprints.

unloads

A list of blueprint names to exclude from loading.

blueprints_path

The full path to the directory containing the blueprints.

init_app(app)

Initializes the Flask application with the BlueprintsLoader instance.

Parameters:

app (Flask) – The Flask application instance.

Return type:

None

load_blueprint(path)

Loads a Flask blueprint from the specified path.

The load_blueprint method is used to load a Flask blueprint from the given path. It iterates over the attributes of the views module and returns the first attribute that is an instance of Blueprint and is not in the unloads list.

Parameters:

path (Path) – The path to the blueprint module file.

Returns:

The loaded blueprint if found, or None if no suitable blueprint is found.

Return type:

Blueprint | None

load_blueprints()

Loads and registers Flask blueprints.

The load_blueprints method is used to load and register Flask blueprints. It iterates over the blueprint paths in the blueprints_path directory that match the specified module name. If a blueprint is successfully loaded, it is registered with the current Flask application using current_app.register_blueprint.

Return type:

None

static load_module(path)

Loads a Python module from the specified path.

The load_module static method is used to load a Python module from the given path. It takes a path parameter of type Path representing the path to the module file. The method attempts to import the module using importlib.util.spec_from_file_location and importlib.util.module_from_spec. If the module is successfully imported, it is executed using spec.loader.exec_module and returned.

Parameters:

path (Path) – The path to the module file.

Returns:

The loaded module if successful, or None if an error occurs during import.

Return type:

ModuleType | None

Change Log

Released 2023-10-21

  • First public preview release.

License

This project in under the BSD-3-Clause License.