Run a Python application and identify which work happens on the server.
A live interface starts with an ordinary Python callable. The callable returns a fragment describing the page; pysx renders it to HTML, then connects the browser to the server for later events. You do not need a JavaScript application toolchain to make your first button work.
Use Python 3.14 or newer: t-strings preserve expressions instead of immediately flattening them into text. The local playground runs Python on your machine and sends updates to the browser.
Understand the moving parts
The commands below copy a runnable counter into your own project and serve it. That counter is an ordinary file you own; nothing about it is special to this course.
The run command is shorthand: given playgrounds.counter, pysx looks for a callable named app inside that module. Written in full it is pysx --app playgrounds.counter:app. The part before the colon is an importable module; the part after it is the exported callable or host, and its name is yours to choose. A project called foo whose entry point is bar runs as pysx --app foo:bar.
The initial page is already meaningful HTML. A later input event travels to Python and the resulting small patch returns to this document. If you started the counter locally, keep its terminal open; Ctrl+C shuts that server down along with the work it owns.
Walk through the code
from pysx import Fragment, pysx, signal
def app()-> Fragment:
count = signal(0)def increment(_event: object)->None:
count.set(count()+1)return pysx(t"""
main:
h1: "Your first live application"p: "The button sends an event to Python, which sends back a small patch."p:
"Count: "span(id="count"): {count}button(id="increment"type="button"onClick={increment}): "Increment"h2: "Try it"ol:
li: "Increment the count a few times."li: "Refresh the page. The count returns to 0."li: "Open a second tab. Its count starts at 0 and is independent of this one."
""")
count starts at zero inside app, so each browser tab gets its own signal. Clicking Increment sends an event to increment, which updates the signal with count.set. The live {count} hole updates the displayed number without reloading the document. The code below is the counter copied by the scaffold command.
Try it locally
Use Python 3.14 or newer. In your project's activated virtual environment, run: