Skip to main content

Adds Injector support to Flask.

Project description

Adds Injector support to Flask.

Injector is a dependency-injection framework for Python, inspired by Guice.

Here’s a pseudo-example:

import sqlite3
from flask.ext.injector import Builder, route
from injector import inject

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


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


# Class-based view with injection
@route('/waz')
class Waz(object):
    @inject(db=sqlite3.Connection)
    def __init__(self, db):
        self.db = db

    @route("/waz")
    def waz(self):
        users = db.execute('SELECT * FROM users').all()
        return 'waz'


def configure(binder):
    config = binder.injector.get(Config)
    binder.bind(
        sqlite3.Connection,
        to=sqlite3.Connection(config['DB_CONNECTION_STRING']),
        scope=request,
        )


def main():
    views = [foo, bar, Waz]
    modules = [configure]
    app = Builder(views, modules, config={
        'DB_CONNECTION_STRING': ':memory:',
        }).build()
    app.run()

Project details


Download files

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

Files for Flask-Injector, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size Flask-Injector-0.1.0.tar.gz (4.2 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page