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.

Source Distribution

Flask-Injector-0.1.0.tar.gz (4.2 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for Flask-Injector-0.1.0.tar.gz
Algorithm Hash digest
SHA256 450dc10585c467cefb4dff8f1622a12e4952cc5127eb493c15663c61f7f36ddc
MD5 0880b6405cccf47508316fd2a3b50c01
BLAKE2b-256 88a1638c4c0c46c8c307c9046151c7b534c8e4be6554f3b04b92cebf1f003bde

See more details on using hashes here.

Supported by

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