Quick start
On your mark, get set, GO!
Greeter
An example web application to greet people.
Custom types
When dependencies are common types like strings, creating more specific types is
helpful. The typing.NewType
method is useful here. We define a Greeting
type
but it could have been something like DatabaseURL
.
import typing
Greeting = typing.NewType("Greeting", str)
Defining dependencies
You define dependencies using an injector.Module
. Or a simple function with
an injector.Binder
instance as a parameter.
import injector
def configure(binder: injector.Binder) -> None:
binder.bind(Greeting, to="Hello")
The web bits
Create a quart web application as usual. Adding the injector.Inject
annotations
to mark external dependencies in views. The framework will handle passing these values.
import injector
import quart
app = quart.Quart(__name__)
@app.route("/<name>")
@app.route("/", defaults={"name": "World"})
async def greeting_view(greeting: injector.Inject[Greeting], name: str) -> str:
return f"{greeting} {name}!"
Wireing it up
Finally we wire everything together with the wire()
method.
import quart_injector
quart_injector.wire(app, modules=[configure])