Skip to content

Flask helpers

Module for the Flask-specific rules.

A sample Flask app generator would look like this.

from flask import Flask
from deescovery discover
from deescovery.flask import get_flask_rules

def app() -> Flask:
    flask_app = Flask(__name__)
    flask_app.config.from_object("myapp.config")
    discovery_rules = get_flask_rules("myapp", flask_app)
    discover("dashboards", discovery_rules)
    return flask_app

blueprints_loader(import_path, flask_app)

Find and import all blueprints in the application.

commands_loader(import_path, flask_app)

Find all commands and register them as Flask CLI commands.

generate_patterns(import_path: str, module_prefix: str) -> List[str]

Generate a list of patterns to discover.

For example, gen_patterns("myapp", "models") generates patterns that make matchers discover the content in the following files.

myapp/users/models.py
myapp/invoices/models.py
(etc. for all domain packages beyond "users" and "invoices")
...

myapp/users/models_roles.py
myapp/users/models_groups.py
(etc. for all modules started with "models_" in all domain packages)
...

myapp/users/models/roles.py
myapp/users/models/groups.py
(if you prefer nested structures)

get_flask_rules(import_path: str, flask_app) -> List[deescovery.discovery.IRule]

Return a list of rules useful for the Flask application.

The following rules will be returned:

  • Load SQLAlchemy models (files models.py)
  • Load Flask blueprints (files controllers.py)
  • Load Flask CLI commands (files cli.py)
  • Initialize services (top-level file services.py)

Parameters:

Name Type Description Default
import_path str

name of the top-level module of the project (like, "myproject")

required
flask_app

a Flask app instance.

required

Returns:

Type Description
List[deescovery.discovery.IRule]

A list of rules, suitable to be passed to "deescovery.discover()"

models_loader(import_path)

Load all models.

service_initializer(import_path, flask_app)

Find and initialize all instances of Flask applications.

Notice that the initialize scans for top-level services files, and doesn't walk over all your app's domain package.