Skip to content

ASGI

wybthon.asgi

asgi

Serve a Wybthon app from Python: pages rendered per request, server functions, and assets.

create_app returns an ASGI application for a project built with server = true in its wybthon.toml. It:

  • renders pages on every request, streaming each Loading boundary's content as its data arrives, into the build's page shell, which then boots Pyodide and hydrates;
  • runs server functions posted (or, for queries, fetched) to {base}_wyb/fn/{id}, including JavaScript-free form posts, which it answers with a redirect back and a flash cookie carrying the result;
  • serves HTTP handlers (GET, POST, ...) that file route modules define, for API endpoints;
  • serves the build's assets with long-lived caching for the content-hashed ones.

Run it with wyb serve (or wyb dev while developing), or under any ASGI server:

WYBTHON_PROJECT=. uvicorn --factory wybthon.asgi:create_app

Middleware. List module:function names under middleware in wybthon.toml; each is async def handle(event, call_next) and returns await call_next() or a Response of its own. Set event.locals (the signed-in user, a database session) for components and server functions to read through get_request_event.

Classes:

Name Description
Response

An HTTP response from middleware or an HTTP handler.

Request

The HTTP request, as event.request for pages, server functions, and handlers.

App

The ASGI application create_app returns.

Functions:

Name Description
create_app

The ASGI application for a Wybthon project built with server = true.

Response dataclass

Response(body: bytes | str = b'', status: int = 200, headers: dict[str, str] = dict(), cookies: list[str] = list(), stream: Any = None)

An HTTP response from middleware or an HTTP handler.

Attributes:

Name Type Description
body bytes | str

The body; a str is sent as UTF-8.

status int

The status code.

headers dict[str, str]

Header values by name.

cookies list[str]

set-cookie values, one per cookie.

Methods:

Name Description
encoded

The body as bytes.

stream class-attribute instance-attribute

stream: Any = field(default=None, repr=False)

More of the body, sent after body as it arrives: an async iterator of str or bytes.

encoded

encoded() -> bytes

The body as bytes.

Request

Request(scope: dict[str, Any], body: bytes)

The HTTP request, as event.request for pages, server functions, and handlers.

Attributes:

Name Type Description
method str

The HTTP method.

path str

The URL path.

query str

The query string, without ?.

headers dict[str, str]

Header values by lowercase name.

body

The request body.

scope

The raw ASGI scope.

Methods:

Name Description
json

The body parsed as JSON.

form

The body parsed as a form.

url property

url: str

Path and query.

json

json() -> Any

The body parsed as JSON.

form

form() -> FormData

The body parsed as a form.

App

App(project: Path, dist: Path, *, dev: bool = False, max_body: int = 10 * 1024 * 1024)

The ASGI application create_app returns.

Attributes:

Name Type Description
project

The project directory.

dist

The build directory it serves.

base

The app's base path, with a trailing slash.

dev

Whether it's serving wyb dev.

Methods:

Name Description
load

Read the build and import the application (again, after a rebuild).

handle

Answer one request (the ASGI entry point, after reading its body).

load

load() -> None

Read the build and import the application (again, after a rebuild).

handle async

handle(request: Request, send: Any) -> None

Answer one request (the ASGI entry point, after reading its body).

create_app

create_app(project: str | PathLike[str] | None = None, *, dist: str | PathLike[str] | None = None, dev: bool = False) -> App

The ASGI application for a Wybthon project built with server = true.

Parameters:

Name Type Description Default
project str | PathLike[str] | None

The project directory. Defaults to $WYBTHON_PROJECT, or the current directory.

None
dist str | PathLike[str] | None

The build to serve. Defaults to the project's dist.

None
dev bool

Serve in development mode (dev diagnostics on, no long-lived caching). wyb dev sets it.

False

Returns:

Type Description
App

An App.

run

run(app: App, *, host: str = '127.0.0.1', port: int = 8000) -> None

Serve app with Wybthon's small built-in HTTP server (wyb serve).

What's in this module

Name Description
create_app create_app(project=None, *, dist=None, dev=False): the ASGI app for a server = true build.
App The application: pages, server functions, form posts, HTTP handlers, and assets.
Request event.request on the server: method, path, query, headers, body, json(), form().
Response What middleware and HTTP handlers return: body, status, headers, cookies.
wyb serve --build
WYBTHON_PROJECT=. uvicorn --factory wybthon.asgi:create_app

See also