Not sure that is what you need, but you asked for a small example with inject + Flask
in comments. As I understood the main problem is related with Flask + configuration + initialization
. This is just an example how it works.
app.py:
from flask import Flask
from api import bp
from configurator import configure
def create_app():
app = Flask(__name__)
# configure Flask app config as you wish... (app.config.from_object(config))
# just some settings for demonstration
app.config.update(dict(
MODULE1_TIMER=1,
MODULE2_LIMIT=2,
))
# configure inject using app context and Flask config
with app.app_context():
configure()
# demo blueprint
app.register_blueprint(bp)
return app
if __name__ == '__main__':
create_app().run(debug=True)
Let's imagine that we have some modules:
# mod1.py
class Module1:
def __init__(self, timer: int) -> None:
self._timer = timer
# mod2.py
class Module2:
def __init__(self, limit: int) -> None:
self._limit = limit
def get_limit(self):
return self._limit
# mod3.py - works with mod1 and mod2
class Module3:
def __init__(self, module1, module2) -> None:
self._module1 = module1
self._module2 = module2
def get_limit(self):
return self._module2.get_limit()
configurator.py:
import inject
from flask import current_app
from mod1 import Module1
from mod2 import Module2
from mod3 import Module3
@inject.params(
module1=Module1,
module2=Module2,
)
def _init_module3(module1, module2):
# module1 and module2 are injected instances
return Module3(module1, module2)
def _injector_config(binder):
# initialization of Module1 and Module2 using Flask config
binder.bind(Module1, Module1(current_app.config['MODULE1_TIMER']))
binder.bind(Module2, Module2(current_app.config['MODULE2_LIMIT']))
# initialization of Module3 using injected Module1 + Module2
# you can use bind_to_constructor + any function
binder.bind_to_constructor(Module3, _init_module3)
def configure():
def config(binder):
binder.install(_injector_config)
# one more binder.install... etc...
inject.clear_and_configure(config)
api.py:
import inject
from flask import Blueprint, jsonify
from mod1 import Module1
from mod2 import Module2
from mod3 import Module3
bp = Blueprint('api', __name__)
@bp.route('/test')
def test():
# get instances which was created using inject
return jsonify(dict(
module1=str(type(inject.instance(Module1))),
module2=str(type(inject.instance(Module2))),
module3=str(type(inject.instance(Module3))),
))
# you can inject something as arg
@bp.route('/test2')
@inject.params(module3=Module3)
def test2(module3: Module3):
return jsonify(dict(module3=str(type(module3))))
@bp.route('/test3')
def test3():
# you can inject something into anything
class Example:
module3 = inject.attr(Module3)
@inject.params(module2=Module2)
def __init__(self, module2: Module2) -> None:
self.module2 = module2
return jsonify({
'MODULE2_LIMIT': Example.module3.get_limit(),
'example': dir(Example()),
})
Run server, open /test
, test2
, /test3
.
A few words about benefits:
- One point for initialization and configuration
- Lower dependency on current_app, flask config / context etc.
- Less problems with recursive imports
- Easy to writing tests
Hope this helps.
init_app
, haven't I? – SwungModule()
, you can only import it and invoke objects inside it. – Kalvnrq-schedular
. If you refer github.com/rq/rq-scheduler, it initialize the object as below:scheduler = Scheduler(connection=Redis(host='0.0.0.0', port=6379))
– Swungsome_module_obj
from thecreate_app
instead of attaching it to app as an attribute? App factory should return app only. – Kalvnsome_module_obj
in create_app, but I do not want to handle configuration outside of thecreate_app
function. Sincesome_module_obj
needs configuration value when it's initialize, I tried to initialize inside ofcreate_app
function. – Swunginject
increate_app()
. – Offutt