Browser-held UI recovery
Understand the moving parts
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."
""")
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
Fill in Name and Notes, then click Add a revision a few times. Stop the server with Ctrl+C, leaving the tab open. Start the same run command again and compare the fields and revision history. Refresh the page, then open the local URL in a new tab to compare fresh state.