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
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
Flask-Injector-0.1.0.tar.gz
(4.2 kB
view details)
File details
Details for the file Flask-Injector-0.1.0.tar.gz
.
File metadata
- Download URL: Flask-Injector-0.1.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 450dc10585c467cefb4dff8f1622a12e4952cc5127eb493c15663c61f7f36ddc |
|
MD5 | 0880b6405cccf47508316fd2a3b50c01 |
|
BLAKE2b-256 | 88a1638c4c0c46c8c307c9046151c7b534c8e4be6554f3b04b92cebf1f003bde |