This works for now only if configure
is enabled
Created
April 9, 2017 00:06
-
-
Save pdonorio/5b18fcc86d040f4f13cea3f649fea2a8 to your computer and use it in GitHub Desktop.
A minimal setup for Flask with latest Injector working
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from flask import Flask | |
from flask.views import View | |
from flask_injector import FlaskInjector | |
from injector import Module, provides, singleton, inject | |
class ExtClass(object): | |
def __init__(self, app): | |
print("APP", app) | |
class MyModule(Module): | |
@singleton | |
@provides | |
def provide_ext(self, app: Flask) -> ExtClass: | |
print("calling module", app) | |
return ExtClass(app) | |
# @inject | |
# def configure(self, binder, app: Flask): | |
# print("calling module", app) | |
# binder.bind(ExtClass, to=ExtClass(app), scope=singleton) | |
class Waz(View): | |
@inject | |
def __init__(self, db: ExtClass): | |
self.db = db | |
def dispatch_request(self): | |
print("TEST DB", self.db) | |
return 'waz' | |
if __name__ == "__main__": | |
app = Flask(__name__) | |
# app.config.update(EXT_CONFIG_VAR='some_value') | |
app.add_url_rule('/waz', view_func=Waz.as_view('waz')) | |
FlaskInjector(app=app, modules=[MyModule]) | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment