pysxLearn by building

Runtime and resilience · Lesson 19 of 28

HTTP rendering and browser attachment

Separate server setup, initial HTML and browser mount without duplicating the graph.

Server rendering is not an empty shell waiting for Python to initialize in the browser. The first HTTP response contains the page's useful content and styles. Async resources may initially be pending, but the page is already real.

When the browser attaches, it adopts the staged graph. Running setup again would duplicate effects, requests and state. Browser mount is a later lifecycle moment and deserves a separate counter from server setup.

Understand the moving parts

The live component counts setup and mount separately. Its asynchronous message arrives afterward. Editing the draft before or during that update should not cause the input to be replaced or the setup count to rise.

Inspect the page response using the browser's network tools or a command-line HTTP client, then compare it with the attached document. Initial-response tests and post-attachment interaction tests establish different parts of the contract.

Walk through the code

import asyncio
from pysx import Fragment, derived, on_mount, on_setup, push, pysx, resource, signal


def app(*, server_rendering: bool = True) -> Fragment:
    draft = signal("")
    setups = signal(0)
    mounts = signal(0)
    generation = 0
    failing = signal(False)
    on_setup(lambda: setups.update(lambda value: value + 1))
    on_mount(lambda: mounts.update(lambda value: value + 1))

    async def load() -> str:
        nonlocal generation
        generation += 1
        current = generation
        should_fail = await push(failing)
        await asyncio.sleep(0.4)

        if should_fail:
            raise ValueError("intentional lesson resource failure")

        return f"Result {current}"

    result = resource("lesson-resource", load)
    status = derived(lambda: result().status)
    message = derived(lambda: result().value or "No completed result")

    def reload(_: object) -> None:
        failing.set(False)
        result.reload()

    def cancel(_: object) -> None:
        result.cancel()

    def fail(_: object) -> None:
        failing.set(True)
        result.reload()

    return pysx(t"""
        label(for="resource-draft"): "Draft stays editable during work"
        input(id="resource-draft" bindValue={draft})
        div:
            button(id="resource-reload" onClick={reload}): "Reload / retry"
            button(id="resource-cancel" onClick={cancel}): "Cancel"
            button(id="resource-fail" onClick={fail}): "Fail next request"
        output(id="resource-status" role="status"): {status}
        output(id="resource-message"): {message}
        p: "Server setup: "
        output(id="resource-setups"): {setups}
        p: "Browser mount: "
        output(id="resource-mounts"): {mounts}
        if {server_rendering}:
            p: "This content was included in the first HTTP response."
    """)

on_setup counts the server lifetime, while on_mount waits for browser attachment. The draft and resource share that lifetime. Reload publishes a later result without rerunning setup or discarding the draft; the first response already contains the form and setup output.

Try it live

Wait for attachment and the async result, edit the draft, then trigger another resource load.

What to observe

Setup remains 1 and mount becomes 1. Later resource updates preserve the draft and setup count.

pendingNo completed result

Server setup:

1

Browser mount:

0

This content was included in the first HTTP response.

Take this with you

Initial HTML, staged graph adoption and browser mount are separate observable stages.