Skip to content

Server rendering

wybthon.server

server

Render components to HTML on the server, for hydration in the browser.

Wybthon components are plain Python, so the same code that runs in the browser under Pyodide renders to HTML in CPython. Serve that HTML with the page, and the browser shows content immediately instead of waiting for Pyodide to boot. Once it has, hydrate adopts the server's DOM and makes it interactive.

Two entry points cover the common deployment shapes, as in Solid 2.0:

  • render_to_string renders synchronously. Async data isn't loaded; its Loading boundaries render their fallbacks and the browser loads it after hydration.
  • render_to_stream sends the page with fallbacks immediately, then streams each boundary's content as its data arrives. Awaiting it instead waits for every async memo the page reads (including memos that only appear once other data arrives) and returns the complete HTML with the results embedded, so the browser neither fetches them again nor shows a loading state.

Both return (or yield) the contents of the mount container; the caller writes the surrounding document. Output always ends with a <script type="application/json" data-wyb-state> element, which hydrate reads and removes.

Example

With any ASGI framework:

from wybthon import RequestEvent
from wybthon.server import render_to_stream

async def page(request):
    event = RequestEvent(url=str(request.url.path), request=request)
    body = await render_to_stream(App(), event=event)
    return HTMLResponse(
        TEMPLATE.replace("<!-- app -->", body),
        status_code=event.response.status,
        headers=dict(event.response.header_items()),
    )

Components declare the response status and headers with http_status and http_header, and read the request with get_request_event.

Rendering walks the same tree the browser mounts and writes its HTML directly (RFC 0006): components run once, each expression is evaluated once, and nothing subscribes, so the output is exactly what the browser's hydration expects at a fraction of a mounted render's cost. Renders may run concurrently on one event loop; don't render from several threads at once.

See Also

Classes:

Name Description
RenderStream

The result of render_to_stream.

Functions:

Name Description
render_to_string

Render view to HTML synchronously.

render_to_stream

Render view as a stream of HTML, or await it for the complete page.

RenderStream

RenderStream(view: Any, event: RequestEvent, timeout: float)

The result of render_to_stream.

Iterate it (async for chunk in stream) to send the page as it renders, or await it (html = await stream) for the complete HTML once every async memo has resolved. Use one or the other, once.

render_to_string

render_to_string(view: Any, *, url: str | None = None, event: RequestEvent | None = None) -> str

Render view to HTML synchronously.

Async memos don't start: the Loading boundaries that read them render their fallbacks, and the browser loads the data after hydrate. Await render_to_stream to include async data.

Parameters:

Name Type Description Default
view Any

The root view: a VNode (such as App()) or a zero-arg callable returning one.

required
url str | None

The request path and query, read by the router. Defaults to the event's URL, or "/".

None
event RequestEvent | None

The RequestEvent being rendered; read its response afterward for the status and headers the page declared.

None

Returns:

Type Description
str

The HTML for the mount container's contents, ending with the

str

serialized state script.

render_to_stream

render_to_stream(view: Any, *, url: str | None = None, event: RequestEvent | None = None, timeout: float = 30.0) -> RenderStream

Render view as a stream of HTML, or await it for the complete page.

Iterating the result yields the page first, with a fallback in every boundary whose data isn't ready. Each later chunk carries the content of the boundaries that became ready, as <template> elements plus a small inline script that swaps them into place (out-of-order streaming). The last chunk is the serialized state for hydrate. Write every chunk inside the mount container, in order. Reveal ordering is respected: a boundary is sent once it would show its content in the browser. The response head is committed with the first chunk.

Awaiting the result renders in passes instead: each pass renders with the values resolved so far, then waits for the async memos it started, and a later pass can start memos that only render once earlier data has arrived. The final pass renders synchronously, as the browser will while hydrating, and returns the complete HTML with its values serialized.

Parameters:

Name Type Description Default
view Any

The root view: a VNode (such as App()) or a zero-arg callable returning one.

required
url str | None

The request path and query, read by the router. Defaults to the event's URL, or "/".

None
event RequestEvent | None

The RequestEvent being rendered; read its response for the declared status and headers.

None
timeout float

Seconds to wait for data in total. Boundaries still pending then keep their fallbacks and load in the browser.

30.0

Returns:

Type Description
RenderStream

What's in this module

Server rendering: the same components that run in the browser render to HTML in CPython. A server render expands each html template into the same element nodes h() would build, so the output and hydration keys don't depend on which style you used. See Server rendering for the full guide.

Name Description
render_to_string render_to_string(view, *, url=None, event=None): render synchronously. Async data isn't loaded; Loading boundaries render their fallbacks.
render_to_stream render_to_stream(view, *, url=None, event=None, timeout=30): returns a RenderStream.
RenderStream Iterate it (async for chunk in stream) for the page with fallbacks, then each Loading boundary as its data arrives, then the state. Await it (html = await stream) for the complete HTML once every async memo has resolved. Use one or the other, once.

Awaiting render_to_stream replaces the removed render_to_string_async. The browser side, hydrate, is_server, is_hydrating, client_only, NoHydration, and ServerError, is exported from wybthon, as are the request helpers components use to set the status and headers.

import asyncio

from wybthon import RequestEvent, component, html, http_header
from wybthon.server import render_to_stream


@component
def App():
    http_header("Cache-Control", "max-age=60")
    return html(t"<p>Hello from the server</p>")


async def main():
    event = RequestEvent(url="/")
    page = await render_to_stream(App(), event=event)
    print(event.response.status, event.response.headers, page)


asyncio.run(main())

See also