Application API

Flaxon

The main application class.

Constructor

Flaxon(name: str, *, debug: bool | None = None, config: dict[str, Any] | None = None)
Parameter Type Description
namestrApplication name
debugbool | NoneEnable debug mode
configdict[str, Any] | NoneConfiguration 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 routes
  • websocket_routes — All registered WebSocket routes
  • state — Application state container
  • config — Application configuration
  • debug — Debug mode status
  • name — Application name

Config

Configuration container.

Constructor

Config(values: Mapping[str, Any] | None = None, *, prefix: str = "FLAXON_")

Methods

  • get_env() — Get the current environment name
  • is_development() — Check if environment is development
  • is_production() — Check if environment is production
  • is_debug() — Check if debug mode is enabled
  • get_secret_key() — Get the secret key
  • get_allowed_hosts() — Get the list of allowed hosts
  • get_max_body_size() — Get the maximum body size
  • to_dict() — Convert to dictionary

State

Application state container.

Methods

  • get(name, default) — Get a state attribute
  • setdefault(name, default) — Set if not exists
  • update(**kwargs) — Update with multiple attributes
  • to_dict() — Convert to dictionary
  • clear() — 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