Modules¶
Modules are Python packages that provide additional services and handlers. They can be used to integrate external libraries into the framework.
During application startup, modules are scanned in order to find services and handlers declared in them.
Using modules¶
Modules need to be activated in settings.py, in the modules property:
settings = {
"modules": [
"zayt.ext.sqlalchemy",
"zayt.ext.jinja",
],
}
Note
The application.py (or application package) is a module itself and is
loaded by default as the last module in the list, if it is not declared.
Dynamic services and handlers¶
Services and handlers can be registered dynamically in modules.
To do this, we need to create a startup hook and inject it with the zayt.di.DIContainer
or the zayt.web.routing.Router.
from zayt import di, web
from zayt.web import routing
class MyService:
...
async def my_handler(request):
...
@startup
async def init(container: di.DiContainer, router: routing.Router):
container.register(di.service(MyService))
router.route(web.get("/")(my_handler))