pysxLearn by building

Runtime and resilience · Lesson 22 of 28

Browser-held UI recovery

Recover compatible delivered UI after server loss while keeping refresh semantics clear.

Server loss destroys the old graph. Ordinary browser-held recovery therefore reconstructs a new graph from a bounded encrypted capsule retained by the open document. It is a continuity feature, not restoration of Python objects.

Only supported latest-delivered UI state can be restored. The host's key ring, current authentication session, route and compatibility checks still apply. An undelivered edit or uncertain action must not become an invented confirmed result.

Understand the moving parts

Run the recovery playground on a server you control and keep its tab open while restarting it with the same command and application-state directory. Compatible delivered state should return.

Now compare a refresh and a new tab: both start fresh ordinary UI. Capsules are held in document memory, not a cross-device account store. Use application persistence for saved business records and explicit durable UI state when you need the contract in the following lesson.

Walk through the code

from pysx import Fragment, each, pysx, signal


def app() -> Fragment:
    name = signal("")
    notes = signal("")
    revisions = signal(0)
    history = signal(list[int]())

    def revise(_event: object) -> None:
        revisions.set(revisions() + 1)
        history.set([*history(), revisions()])

    def reverse(_event: object) -> None:
        history.set(list(reversed(history())))

    def revision(value: int) -> Fragment:
        return pysx(t"""
            li(data-revision={value}): {value}
        """)

    return pysx(t"""
        main:
            h1: "Browser-held recovery"
            p: "Ordinary local state participates in the host's default recovery policy."
            label(htmlFor="name"): "Name"
            input(id="name" bindValue={name} autocomplete="off")
            label(htmlFor="notes"): "Notes"
            textarea(id="notes" bindValue={notes} rows="4")
            p:
                "Revision: "
                span(id="revision"): {revisions}
            button(id="revise" type="button" onClick={revise}): "Add a revision"
            button(id="reverse" type="button" onClick={reverse}): "Reverse revisions"
            ol(id="history"): {each(history, revision, key=lambda value: value)}
            h2: "Try it"
            ol:
                li: "Fill in the draft and add a few revisions."
                li: "Stop the server with Ctrl+C, leaving this tab open."
                li: "Start the same command again. The open document restores your draft."
                li: "Refresh the page instead, and it starts fresh."
    """)

name and notes are ordinary local signals bound to form fields. Add a revision updates revisions and appends to history; each renders the revision rows with stable keys. Reverse revisions changes their order. The host supplies browser-held recovery for this delivered state without an explicit durable root or application store. The code shown is the recovery playground you run below.

Try it locally

Use Python 3.14 or newer. In your project's activated virtual environment, run:

pip install pysx-framework
pysx-playground recovery
pysx --app playgrounds.recovery

Open the local URL printed in the terminal. Keep that terminal open while you try the playground.

Scaffold once. To restart, repeat only the pysx --app command; scaffolding again resets the playground's saved state.

What you'll do

  1. Fill in Name and Notes, then click Add a revision a few times.
  2. Stop the server with Ctrl+C, leaving the tab open.
  3. Start the same run command again and compare the fields and revision history.
  4. Refresh the page, then open the local URL in a new tab to compare fresh state.

What you'll see

The open document restores compatible fields and revision history after server loss. Refresh and a new tab start with empty fields and no revisions.

Take this with you

UI continuity reconstructs a new authorized graph; it does not persist business transactions.