Extensions

Broadway would be nothing without extensions. Extensions allow broadway to load and configure flask extensions using environment variables.

Registering Extensions

Install extensions from PyPI then load them into your application using Factory.add_extension(). You can load many extensions at once using Factory.add_extensions().

$ pip install broadway-sqlalchemy
factory.add_extensions(
    'broadway_sqlalchemy',
    'some_extension.intergrations.broadway',
)

Extensions will generally add to the factory’s config schema and initialise third-party code. The factory will initialise applications it creates using the extensions in registration order.

Creating Extensions

Extensions are python objects or modules that provide the following methods:

myextension.register(factory)

A hook to register the extension with a factory. Do any setup here, such as adding to the factories config schema.

def register(factory):
    factory.add_config_schema(SCHEMA)
Parameters:factory (Factory) – the calling factory
myextension.init_app(application)

A hook called to initialise an application. Do any initialisation here, such as calling init_app() on a flask extensions.

def init_app(application):
    extension.init_app(application)
Parameters:application (flask.Flask) – the flask application
myextension.extension

When the extension wraps a flask extension, extension should provide an instance of the original flask extension.

Do not create libraries dependent on broadway. Instead create reusable libraries, and then flask/broadway bindings on top of them.

Example Extension

Most broadway extensions should be simple wrappers to existing libraries and flask extensions. The following is an example broadway extension. It wraps the SQLite3 extension in the Flask documentation.

from flask_sqlite3 import SQLite3

SCHEMA = {
  'SQLITE3_DATABASE': {
    'type': str,
    'default': ':memory:'
  }
}

extension = SQLite3()

def register(factory):
    factory.add_config_schema(SCHEMA)

def init_app(application):
    extension.init_app(application)