Example: Counter
Signals, a computed value and a bound number input.
- 37 lines of Python
- 1 page
- 0 server functions
- 1223 B page JS (gzip)
#Source
examples/counter/app.pyweb
from pyweb import App
from pyweb.ui import Button, Card, CardContent, CardDescription, CardHeader
app = App(title="Counter")
@app.page("/")
def Home():
count = 0
step = 1
doubled = count * 2
def increment():
count += step
def reset():
count = 0
<main style="max-width: 420px; margin: 64px auto; padding: 0 16px">
<Card>
<CardHeader>
<h1 data-part="title">Counter</h1>
<CardDescription>A signal, a computed value and a bound input.</CardDescription>
</CardHeader>
<CardContent>
<p style="font-size: 56px; font-weight: 700; line-height: 1; margin: 8px 0 24px" animate="count">{count}</p>
<div style="display: flex; gap: 8px; margin-bottom: 16px">
<Button id="inc" onclick={increment}>Count: {count}</Button>
<Button id="reset" variant="outline" onclick={reset} disabled={count == 0}>Reset</Button>
</div>
<label class="pw-field">
Step <input id="step" class="pw-control" type="number" bind={step} />
</label>
<p id="doubled">Doubled: {doubled}</p>
</CardContent>
</Card>
</main>
#What runs where
Output of pyweb inspect: every page variable, handler and server function, where it runs and why.
Output
page Home route=/
browser count reactive state: assigned in increment(); literal initial value
browser step reactive state: bound to an input (line 32); literal initial value
browser doubled derived from count; recomputed when they change
browser increment event handler (compiled to JavaScript)
browser reset event handler (compiled to JavaScript)#Generated JavaScript
The browser modules the compiler wrote (before minification). They import the shared runtime.
JavaScript
// Home.js
import { h as $h, t as $t, dyn as $dyn, list as $list, when as $when, signal as $signal, computed as $computed, mount as $mount, onMount as $onMount, py as $py, rpc as $rpc, slot as $slot, onCleanup as $onCleanup, spreadProps as $spreadProps } from "./runtime.js";
// card.pyweb
const { Card, CardHeader, CardDescription, CardContent } = (() => {
function Card($p) {
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
return [$h("div", {"class": "pw-card", "data-pw-ui": "card", "data-part": "card", "$spread3": () => attrs()}, () => [$dyn(() => children())])];
}
function CardHeader($p) {
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
return [$h("div", {"data-part": "header", "$spread1": () => attrs()}, () => [$dyn(() => children())])];
}
function CardDescription($p) {
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
return [$h("p", {"data-part": "description", "$spread1": () => attrs()}, () => [$dyn(() => children())])];
}
function CardContent($p) {
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
return [$h("div", {"data-part": "content", "$spread1": () => attrs()}, () => [$dyn(() => children())])];
}
return { Card, CardHeader, CardDescription, CardContent };
})();
// button.pyweb
const { Button } = (() => {
function Button($p) {
const variant = $p["variant"] || (() => "default");
const size = $p["size"] || (() => "md");
const type = $p["type"] || (() => "button");
const href = $p["href"] || (() => null);
const loading = $p["loading"] || (() => false);
const disabled = $p["disabled"] || (() => false);
const onclick = $p["onclick"] || (() => null);
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
const busy = $computed(() => ($py.truth(loading()) ? "true" : null));
const part = $computed(() => $py.or($py.m(attrs(), "get", "data-part"), () => "button"));
return [$when(() => $py.truth(href()), () => [$h("a", {"class": "pw-button", "data-part": () => part(), "data-variant": () => variant(), "data-size": () => size(), "href": () => href(), "aria-disabled": () => ($py.truth(disabled()) ? "true" : null), "onclick": (e) => { const f = onclick(); return f ? f(e) : undefined; }, "$spread7": () => attrs()}, () => [$dyn(() => children())])], () => [$h("button", {"class": "pw-button", "data-part": () => part(), "data-variant": () => variant(), "data-size": () => size(), "type": () => type(), "disabled": () => $py.or(disabled(), () => loading()), "aria-busy": () => busy(), "onclick": (e) => { const f = onclick(); return f ? f(e) : undefined; }, "$spread8": () => attrs()}, () => [
$when(() => $py.truth(loading()), () => [$h("span", {"class": "pw-spinner", "aria-hidden": "true"})]),
$dyn(() => children())
])])];
}
return { Button };
})();
function Home($s) {
const count = $signal("count" in $s ? $s["count"] : 0, "count");
const step = $signal("step" in $s ? $s["step"] : 1, "step");
const doubled = $computed(() => $py.mul(count(), 2));
function increment() {
count($py.add(count(), step()));
}
function reset() {
count(0);
}
return [$h("main", {"style": "max-width: 420px; margin: 64px auto; padding: 0 16px"}, () => [Card({"children": () => [
CardHeader({"children": () => [
$h("h1", {"data-part": "title"}, () => [$t("Counter")]),
CardDescription({"children": () => [$t("A signal, a computed value and a bound input.")]})
]}),
CardContent({"children": () => [
$h("p", {"data-pw-motion": JSON.stringify({"animate": "count"}), "style": ((i, s) => s)(null, "font-size: 56px; font-weight: 700; line-height: 1; margin: 8px 0 24px", null, "count")}, () => [$dyn(() => count())]),
$h("div", {"style": "display: flex; gap: 8px; margin-bottom: 16px"}, () => [
Button({"onclick": () => increment, "children": () => [
$t("Count: "),
$dyn(() => count())
], "$rest": () => ({"id": "inc"})}),
Button({"variant": () => "outline", "onclick": () => reset, "disabled": () => (count() === 0), "children": () => [$t("Reset")], "$rest": () => ({"id": "reset"})})
]),
$h("label", {"class": "pw-field"}, () => [
$t("Step "),
$h("input", {"id": "step", "class": "pw-control", "type": "number", "$bind": step})
]),
$h("p", {"id": "doubled"}, () => [
$t("Doubled: "),
$dyn(() => doubled())
])
]})
]})])];
}
$mount("Home", Home);