Skip to main content

Adds Injector, a Dependency Injection framework, support to Flask.

Project description

https://codecov.io/gh/alecthomas/flask_injector/branch/master/graph/badge.svg https://github.com/alecthomas/flask_injector/workflows/CI/badge.svg

Adds Injector support to Flask, this way there’s no need to use global Flask objects, which makes testing simpler.

Injector is a dependency-injection framework for Python, inspired by Guice. You can find Injector on PyPI and Injector documentation on Read the Docs.

Flask-Injector is compatible with CPython 3.7+. As of version 0.14.0 it requires Injector version 0.20.0 or greater and Flask 2.1.2 or greater.

GitHub project page: https://github.com/alecthomas/flask_injector

PyPI package page: https://pypi.org/project/Flask-Injector/

Changelog: https://github.com/alecthomas/flask_injector/blob/master/CHANGELOG.rst

Features

Flask-Injector lets you inject dependencies into:

  • views (functions and class-based)

  • before_request handlers

  • after_request handlers

  • teardown_request handlers

  • template context processors

  • error handlers

  • Jinja environment globals (functions in app.jinja_env.globals)

  • Flask-RESTFul Resource constructors

  • Flask-RestPlus Resource constructors

  • Flask-RESTX Resource constructors

Flask-Injector supports defining types using function annotations (Python 3), see below.

Documentation

As Flask-Injector uses Injector under the hood you should find the Injector documentation, including the Injector API reference, helpful. The Injector README provides a tutorial-level introduction to using Injector.

The Flask-Injector public API consists of the following:

  • FlaskInjector class with the constructor taking the following parameters:

    • app, an instance of`flask.Flask` [mandatory] – the Flask application to be used

    • modules, an iterable of Injector modules [optional] – the Injector modules to be used.

    • injector, an instance of injector.Injector [optional] – an instance of Injector to be used if, for some reason, it’s not desirable for FlaskInjector to create a new one. You’re likely to not need to use this.

    • request_scope_class, an injector.Scope subclass [optional] – the scope to be used instead of RequestScope. You’re likely to need to use this except for testing.

  • RequestScope class – an injector.Scope subclass to be used for storing and reusing request-scoped dependencies

  • request object – to be used as a class decorator or in explicit bind() calls in Injector modules.

Creating an instance of FlaskInjector performs side-effectful configuration of the Flask application passed to it. The following bindings are applied (if you want to modify them you need to do it in one of the modules passed to the FlaskInjector constructor):

  • flask.Flask is bound to the Flask application in the (scope: singleton)

  • flask.Config is bound to the configuration of the Flask application

  • flask.Request is bound to the current Flask request object, equivalent to the thread-local flask.request object (scope: request)

Example application using Flask-Injector

import sqlite3
from flask import Flask, Config
from flask.views import View
from flask_injector import FlaskInjector
from injector import inject

app = Flask(__name__)

# Configure your application by attaching views, handlers, context processors etc.:

@app.route("/bar")
def bar():
    return render("bar.html")


# Route with injection
@app.route("/foo")
def foo(db: sqlite3.Connection):
    users = db.execute('SELECT * FROM users').all()
    return render("foo.html")


# Class-based view with injected constructor
class Waz(View):
    @inject
    def __init__(self, db: sqlite3.Connection):
        self.db = db

    def dispatch_request(self, key):
        users = self.db.execute('SELECT * FROM users WHERE name=?', (key,)).all()
        return 'waz'

app.add_url_rule('/waz/<key>', view_func=Waz.as_view('waz'))


# In the Injector world, all dependency configuration and initialization is
# performed in modules (https://injector.readthedocs.io/en/latest/terminology.html#module).
# The same is true with Flask-Injector. You can see some examples of configuring
# Flask extensions through modules below.

# Accordingly, the next step is to create modules for any objects we want made
# available to the application. Note that in this example we also use the
# Injector to gain access to the `flask.Config`:

def configure(binder):
    binder.bind(
        sqlite3.Connection,
        to=sqlite3.Connection(':memory:'),
        scope=request,
    )

# Initialize Flask-Injector. This needs to be run *after* you attached all
# views, handlers, context processors and template globals.

FlaskInjector(app=app, modules=[configure])

# All that remains is to run the application

app.run()

See example.py for a more complete example, including Flask-SQLAlchemy and Flask-Cache integration.

Supporting Flask Extensions

Typically, Flask extensions are initialized at the global scope using a pattern similar to the following.

app = Flask(__name__)
ext = ExtClass(app)

@app.route(...)
def view():
    # Use ext object here...

As we don’t have these globals with Flask-Injector we have to configure the extension the Injector way - through modules. Modules can either be subclasses of injector.Module or a callable taking an injector.Binder instance.

from injector import Module

class MyModule(Module):
    @provider
    @singleton
    def provide_ext(self, app: Flask) -> ExtClass:
        return ExtClass(app)

def main():
    app = Flask(__name__)
    app.config.update(
        EXT_CONFIG_VAR='some_value',
    )

    # attach your views etc. here

    FlaskInjector(app=app, modules=[MyModule])

    app.run()

Make sure to bind extension objects as singletons.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-Injector-0.14.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Flask_Injector-0.14.0-py2.py3-none-any.whl (14.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file Flask-Injector-0.14.0.tar.gz.

File metadata

  • Download URL: Flask-Injector-0.14.0.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for Flask-Injector-0.14.0.tar.gz
Algorithm Hash digest
SHA256 f116c24bb3e3e34ca3e41a940eab6178ede41954d27bb02f8a044f128d750dbc
MD5 dddf521837733b02338413a787a3d058
BLAKE2b-256 8a99a777d83cc3b964ecafe04ef7e8cf3383da4447abd1e64633c9d9da8dd563

See more details on using hashes here.

File details

Details for the file Flask_Injector-0.14.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for Flask_Injector-0.14.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b5330106b02352d55cd2fd8e44b649a2fede5c17d703eea8ea72591314ff0b63
MD5 7b1e67578f5ce95091f74c54c713c000
BLAKE2b-256 643fc7a84e255033e6235dfaaf0a246679def2eb840b3a104e96d5e4e7eac312

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page