Application API
Flaxon
The main application class.
Constructor
Flaxon(name: str, *, debug: bool | None = None, config: dict[str, Any] | None = None)
| Parameter | Type | Description |
|---|---|---|
name | str | Application name |
debug | bool | None | Enable debug mode |
config | dict[str, Any] | None | Configuration dictionary |
Routing Methods
route
route(path: str, *, methods: set[str] | list[str] | tuple[str, ...] = ("GET",), name: str | None = None) -> Callable
Register a route with custom methods.
get
get(path: str, *, name: str | None = None) -> Callable
Register a GET route.
post
post(path: str, *, name: str | None = None) -> Callable
Register a POST route.
put
put(path: str, *, name: str | None = None) -> Callable
Register a PUT route.
patch
patch(path: str, *, name: str | None = None) -> Callable
Register a PATCH route.
delete
delete(path: str, *, name: str | None = None) -> Callable
Register a DELETE route.
head
head(path: str, *, name: str | None = None) -> Callable
Register a HEAD route.
options
options(path: str, *, name: str | None = None) -> Callable
Register an OPTIONS route.
websocket
websocket(path: str, *, name: str | None = None) -> Callable
Register a WebSocket route.
include_router
include_router(router: Router) -> None
Include routes from another router.
url_for
url_for(name: str, **params: Any) -> str
Generate a URL for a named route.
Middleware Methods
add_middleware
add_middleware(middleware_class: type[Any], **options: Any) -> None
Add middleware to the application.
Template Methods
use_templates
use_templates(engine: Any) -> None
Configure Jinax template engine.
Lifecycle Methods
on_startup
on_startup(callback: Callable[..., Any]) -> Callable[..., Any]
Register a startup callback.
on_shutdown
on_shutdown(callback: Callable[..., Any]) -> Callable[..., Any]
Register a shutdown callback.
Server Methods
run
run(host: str = "127.0.0.1", port: int = 8000, *, reload: bool = False, workers: int = 1) -> None
Run the application using Uvicorn.
Properties
routes— All registered HTTP routeswebsocket_routes— All registered WebSocket routesstate— Application state containerconfig— Application configurationdebug— Debug mode statusname— Application name
Config
Configuration container.
Constructor
Config(values: Mapping[str, Any] | None = None, *, prefix: str = "FLAXON_")
Methods
get_env()— Get the current environment nameis_development()— Check if environment is developmentis_production()— Check if environment is productionis_debug()— Check if debug mode is enabledget_secret_key()— Get the secret keyget_allowed_hosts()— Get the list of allowed hostsget_max_body_size()— Get the maximum body sizeto_dict()— Convert to dictionary
State
Application state container.
Methods
get(name, default)— Get a state attributesetdefault(name, default)— Set if not existsupdate(**kwargs)— Update with multiple attributesto_dict()— Convert to dictionaryclear()— Clear all state attributes
Example
from flaxon import Flaxon
app = Flaxon("my-app")
app.state.db = await create_pool()
app.state.redis = await create_redis()
# Later
db = app.state.db