Adds Injector, a Dependency Injection framework, support to Flask.
Project description
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.
Flask-Injector is compatible with CPython 3.3+. As of version 0.10.0 it requires Injector version 0.12.0 or greater and Flask 0.12.0 or greater.
GitHub project page: https://github.com/alecthomas/flask_injector
PyPI package page: https://pypi-hypernode.com/pypi/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-Injector supports defining types using function annotations (Python 3), see below.
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 (http://packages.python.org/injector/#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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file Flask-Injector-0.11.0.tar.gz
.
File metadata
- Download URL: Flask-Injector-0.11.0.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3ea0c21f3f19b15d71b47c615c95a059777908d0e33f4cc525c7cfb5c134d5f |
|
MD5 | 6510aff8d32d5b4f4e9e51e6b2d89b5c |
|
BLAKE2b-256 | d2ae65ba91f0bba76da7c8f852d225f78418823f5627f1c1019276803bbcd94e |
File details
Details for the file Flask_Injector-0.11.0-py2.py3-none-any.whl
.
File metadata
- Download URL: Flask_Injector-0.11.0-py2.py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c164a5456b38969c7700d186bde3dcc6d28b9589718a731c58b05d042c1ef3cc |
|
MD5 | f3a41306a93da0e216304f7cc7941fbf |
|
BLAKE2b-256 | 0139bc8e09153d030b53a1ceeeacfeb0f8e9aba2efe7172f7619cb30a3f772f0 |