Skip to content

Instantly share code, notes, and snippets.

@pdonorio
Created April 9, 2017 00:06
Show Gist options
  • Save pdonorio/5b18fcc86d040f4f13cea3f649fea2a8 to your computer and use it in GitHub Desktop.
Save pdonorio/5b18fcc86d040f4f13cea3f649fea2a8 to your computer and use it in GitHub Desktop.
A minimal setup for Flask with latest Injector working

This works for now only if configure is enabled

# -*- 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