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
Loadingboundary'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:
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 |
App |
The ASGI application |
Functions:
| Name | Description |
|---|---|
create_app |
The ASGI application for a Wybthon project built with |
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 |
status |
int
|
The status code. |
headers |
dict[str, str]
|
Header values by name. |
cookies |
list[str]
|
|
Methods:
| Name | Description |
|---|---|
encoded |
The body as bytes. |
Request
¶
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. |
App
¶
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 |
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). |
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 |
None
|
dist
|
str | PathLike[str] | None
|
The build to serve. Defaults to the project's |
None
|
dev
|
bool
|
Serve in development mode (dev diagnostics on, no
long-lived caching). |
False
|
Returns:
| Type | Description |
|---|---|
App
|
An |
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. |
See also¶
- Concepts: Full-stack apps
- Guides: Deployment
- Testing:
Clientsends requests to an app in-process