Example: Teamboard (SaaS)
Teams with invitations and roles, tenant Models with migrations, forms, lists that update live in every tab, a background job, a webhook, API tokens and an admin; installable.
- 269 lines of Python
- 3 pages
- 4 server functions
- 4782 B page JS (gzip)
This example needs its server (database, sessions or server functions). Run it locally:
Terminal
pyweb dev examples/saas/app.pyweb#Source
examples/saas/app.pyweb
"""Teamboard: a small SaaS. Teams with invitations and roles, projects and tasks that
update live, forms with validation, a background job, a webhook, a daily email, API
tokens, an admin, and an installable app.
Run it: pyweb dev app.pyweb (sign up; the confirmation link is printed in the terminal)
Test it: pytest
Ship it: pyweb deploy compose (or fly, render, railway, k8s)
"""
import logging
from pyweb import App, Field, Form, Input, Model, Submit, Textarea, server, webhooks
from pyweb.authkit import User
from pyweb.telemetry import span
from pyweb.ui import (Badge, Button, Card, CardContent, Checkbox, Dialog, DialogContent, DialogDescription,
DialogHeader, DialogTitle, DialogTrigger, DropdownMenu, DropdownMenuContent, DropdownMenuTrigger,
MenuItem, MenuLabel, MenuSeparator, Sidebar, SidebarContent, SidebarFooter, SidebarGroup,
SidebarHeader, SidebarInset, SidebarMenu, SidebarMenuButton, SidebarMenuItem, SidebarProvider,
SidebarTrigger, Theme, toast)
app = App(title="Teamboard", database="sqlite:///app.db", stylesheets=["/static/app.css"],
theme=Theme.preset("zinc"), pwa=True)
auth = app.use_auth(orgs=True) # teams: /orgs to invite people and set roles
auth.plans({"free": {"seats": 5}, "pro": {"seats": 50}})
log = logging.getLogger("teamboard")
# ------------------------------------------------------------------ data
class Project(Model, tenant=True): # belongs to a team: other teams can't see it
name: str = Field(min=2, max=60)
about: str = Field(default="", max=500)
owner: User = Field(readonly=True)
class Task(Model, tenant=True):
title: str = Field(min=1, max=120)
done: bool = False
project: Project = Field(readonly=True)
owner: User = Field(readonly=True)
def my_team():
"""The signed-in person's current team. Their first visit makes them one of their own."""
team = auth.org()
user = auth.user()
if team is None and user is not None:
team = auth.create_org(f"{user.name or user.email.split('@')[0]}'s team")
auth.switch_org(team)
return team
# ------------------------------------------------------------------ actions
@server(login=True)
def create_project(project: Project):
my_team()
project.owner = auth.user()
project.save() # in the current team
welcome_tasks.enqueue(project.id) # runs in the background, after this commits
webhooks.publish("project.created", {"id": project.id, "name": project.name}) # to the team's endpoints
toast("Project created", "Its starter checklist is on the way.", tone="success")
return project
@server(login=True)
def add_task(project_id: int, title: str = Field(min=1, max=120)):
project = Project.get_or_404(project_id) # another team's project: 404
return Task.create(title=title, project=project, owner=auth.user())
@server(login=True)
def toggle(task_id: int) -> bool:
task = Task.get_or_404(task_id)
task.done = not task.done
task.save()
return task.done
@server(login=True)
def remove(task_id: int) -> None:
Task.get_or_404(task_id).delete()
toast("Task deleted")
# ------------------------------------------------------------------ background work
@app.job(retries=3)
def welcome_tasks(project_id: int):
"""A new project starts with a checklist (a job: it could just as well call an API). Jobs run for
the team that queued them."""
project = Project.get(project_id)
with span("seed checklist", project=project_id):
for title in ("Invite your team", "Write the first task", "Ship something small"):
Task.get_or_create(title=title, project=project, defaults={"owner_id": project.owner_id})
@app.cron("0 8 * * 1-5", tz="UTC")
def morning_digest():
"""Weekday mornings: email everyone their open tasks."""
from pyweb import mail
for user in User.query():
open_tasks = Task.where(owner=user, done=False).order("id").limit(20).pluck("title")
if open_tasks:
mail.send(user.email, f"{len(open_tasks)} open tasks",
"Good morning! Still open:\n\n" + "\n".join(f"- {t}" for t in open_tasks))
log.info("sent digest", extra={"tasks": len(open_tasks)})
# ------------------------------------------------------------------ pages
def sidebar_projects():
"""The newest projects with their open task counts, for the sidebar (two queries)."""
counts = {}
for project_id in Task.where(done=False).pluck("project_id"):
counts[project_id] = counts.get(project_id, 0) + 1
return [{"id": p.id, "name": p.name, "open": counts.get(p.id)} for p in Project.query().order("-id").limit(12)]
@app.layout
def Shell(children):
user = auth.user()
team = my_team() if user else None
projects = sidebar_projects() if user else []
<SidebarProvider>
if user:
<Sidebar label="Teamboard">
<SidebarHeader>
<a class="brand" href="/projects">Teamboard</a>
<a class="team" href="/orgs" title="Switch or manage teams">{team.name}</a>
</SidebarHeader>
<SidebarContent>
<SidebarGroup>
<SidebarMenu>
<SidebarMenuItem>
<SidebarMenuButton href="/projects" icon="▦">All projects</SidebarMenuButton>
</SidebarMenuItem>
</SidebarMenu>
</SidebarGroup>
<SidebarGroup label="Projects">
<SidebarMenu>
for p in projects:
<SidebarMenuItem>
<SidebarMenuButton href={f"/projects/{p['id']}"} icon="#" badge={p["open"]}>
{p["name"]}
</SidebarMenuButton>
</SidebarMenuItem>
</SidebarMenu>
</SidebarGroup>
</SidebarContent>
<SidebarFooter>
<DropdownMenu side="top">
<DropdownMenuTrigger variant="ghost" class_="user-button">{user.name or user.email}</DropdownMenuTrigger>
<DropdownMenuContent>
<MenuLabel>{user.email}</MenuLabel>
<MenuItem href="/account">Account</MenuItem>
<MenuItem href={f"/orgs/{team.slug}"}>Team and invitations</MenuItem>
<MenuItem href="/account/tokens">API tokens</MenuItem>
if "admin" in (user.roles or []):
<MenuItem href="/admin">Admin</MenuItem>
<MenuSeparator />
<MenuItem href="/logout">Sign out</MenuItem>
</DropdownMenuContent>
</DropdownMenu>
</SidebarFooter>
</Sidebar>
<SidebarInset>
<header class="topbar">
if user:
<SidebarTrigger />
else:
<a class="brand" href="/">Teamboard</a>
<span class="spacer"></span>
<Button href="/login" variant="ghost">Sign in</Button>
<Button href="/signup">Sign up</Button>
</header>
{children}
</SidebarInset>
</SidebarProvider>
@app.page("/")
def Home():
<div class="page hero" animate="fade-up">
<Badge variant="secondary">Live updates · background jobs · admin</Badge>
<h1>Plan the week with your team</h1>
<p class="muted">Projects and tasks that update live in every open tab.</p>
<div class="row">
<Button href="/signup" size="lg">Create an account</Button>
<Button href="/projects" size="lg" variant="outline">Open your projects</Button>
</div>
</div>
@app.page("/projects", login=True, title="Projects")
def Projects():
projects = Project.query().order("-id").live()
<div class="page">
<div class="page-head">
<h1>Projects</h1>
<Dialog>
<DialogTrigger id="new-project">New project</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>New project</DialogTitle>
<DialogDescription>It starts with a short checklist, made by a background job.</DialogDescription>
</DialogHeader>
<Form action={create_project} redirect="/projects/{id}">
<Input name="name" placeholder="Website relaunch" />
<Textarea name="about" rows="2" placeholder="What is it about? (optional)" />
<Submit>Create project</Submit>
</Form>
</DialogContent>
</Dialog>
</div>
<ul id="projects" class="grid">
for p in projects:
<li animate="fade-up" layout>
<a class="pw-card project" href={f"/projects/{p['id']}"}>
<b>{p["name"]}</b>
<span class="muted">{p["about"] or "No description"}</span>
</a>
</li>
</ul>
if not projects:
<Card class_="empty-state" animate="fade">
<CardContent>
<p><b>No projects yet</b></p>
<p class="muted">Start one with New project: it comes with a checklist.</p>
</CardContent>
</Card>
</div>
@app.page("/projects/{project_id}", login=True, title="Project")
def ProjectPage(project_id: int):
project = Project.get_or_404(project_id)
tasks = Task.where(project_id=project.id).order("done", "id").live()
left = len([t for t in tasks if not t["done"]])
<div class="page">
<p><Button href="/projects" variant="ghost" size="sm">← Projects</Button></p>
<div class="page-head">
<h1>{project.name}</h1>
<Badge variant="secondary" id="left">{left} open</Badge>
</div>
if project.about:
<p class="muted">{project.about}</p>
<Card>
<CardContent>
<Form action={add_task} values={{"project_id": project.id}}>
<Input name="project_id" type="hidden" />
<Input name="title" label="" placeholder="Add a task" />
<Submit>Add</Submit>
</Form>
<ul id="tasks">
for t in tasks:
<li class={{"done": t["done"]}} initial={{"opacity": 0, "y": -6}} animate={{"opacity": 1, "y": 0}}
exit={{"opacity": 0, "x": 24}} layout>
<Checkbox checked={t["done"]} onchange={lambda: toggle(t["id"])} aria-label={"Done: " + t["title"]} />
<span>{t["title"]}</span>
<Button variant="ghost" size="icon" class_="remove" onclick={lambda: remove(t["id"])}
aria-label="Remove">×</Button>
</li>
</ul>
if not tasks:
<p class="empty">No tasks yet. Add the first one above.</p>
</CardContent>
</Card>
</div>
#What runs where
Output of pyweb inspect: every page variable, handler and server function, where it runs and why.
Output
page Home route=/
page Projects route=/projects
browser projects reactive state: a live query: the server sends new rows when its tables change; initial value computed on the server and sent
server __pw_forms__ computed per request on the server; value sent because browser code reads it
page ProjectPage route=/projects/{project_id}
server project computed per request on the server; value sent because browser code reads it
browser tasks reactive state: a live query: the server sends new rows when its tables change; initial value computed on the server and sent
browser left derived from tasks; recomputed when they change
server __pw_forms__ computed per request on the server; value sent because browser code reads it
rpc POST /__pyweb/rpc/create_project (project: Project) -> Any
rpc POST /__pyweb/rpc/add_task (project_id: int, title: str) -> Any
rpc POST /__pyweb/rpc/toggle (task_id: int) -> bool
rpc POST /__pyweb/rpc/remove (task_id: int) -> None#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";
// display.pyweb
const { Badge } = (() => {
function Badge($p) {
const variant = $p["variant"] || (() => "soft");
const tone = $p["tone"] || (() => null);
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
return [$h("span", {"class": "pw-badge", "data-part": "badge", "data-variant": () => variant(), "data-tone": () => tone(), "$spread4": () => attrs()}, () => [$dyn(() => children())])];
}
return { Badge };
})();
// 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) {
return [$h("div", {"class": "page hero", "data-pw-motion": JSON.stringify({"animate": "fade-up"}), "style": ((i, s) => s)(null, null, null, "fade-up")}, () => [
Badge({"variant": () => "secondary", "children": () => [$t("Live updates \u00b7 background jobs \u00b7 admin")]}),
$h("h1", null, () => [$t("Plan the week with your team")]),
$h("p", {"class": "muted"}, () => [$t("Projects and tasks that update live in every open tab.")]),
$h("div", {"class": "row"}, () => [
Button({"href": () => "/signup", "size": () => "lg", "children": () => [$t("Create an account")]}),
Button({"href": () => "/projects", "size": () => "lg", "variant": () => "outline", "children": () => [$t("Open your projects")]})
])
])];
}
$mount("Home", Home);
// Projects.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";
import "./forms.js";
import { subscribe as $subscribe, live as $live, join as $join } from "./live.js";
// dialog.pyweb
const { Dialog, DialogTrigger, DialogContent, DialogHeader, DialogTitle, DialogDescription } = (() => {
function Dialog($p) {
const open = $p["open"] || (() => null);
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
const state = $computed(() => ((open() == null) ? null : ($py.truth(open()) ? "true" : "false")));
return [$h("div", {"class": "pw-dialog-root", "data-pw-ui": "dialog", "data-part": "root", "data-state": "closed", "data-open": () => state(), "$spread5": () => attrs()}, () => [$dyn(() => children())])];
}
function DialogTrigger($p) {
const variant = $p["variant"] || (() => "default");
const size = $p["size"] || (() => "md");
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
return [$h("button", {"type": "button", "class": "pw-button", "data-part": "trigger", "data-variant": () => variant(), "data-size": () => size(), "$spread5": () => attrs()}, () => [$dyn(() => children())])];
}
function DialogContent($p) {
const close_button = $p["close_button"] || (() => true);
const size = $p["size"] || (() => "md");
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
return [$h("dialog", {"class": "pw-dialog", "data-part": "content", "data-state": "closed", "data-size": () => size(), "$spread4": () => attrs()}, () => [
$dyn(() => children()),
$when(() => $py.truth(close_button()), () => [$h("button", {"type": "button", "class": "pw-dialog-x", "data-part": "close", "aria-label": "Close"}, () => [$h("svg", {"width": "15", "height": "15", "viewBox": "0 0 15 15", "aria-hidden": "true"}, () => [$h("path", {"d": "M11.8 3.2 3.2 11.8M3.2 3.2l8.6 8.6", "stroke": "currentColor", "stroke-width": "1.5", "stroke-linecap": "round"})])])])
])];
}
function DialogHeader($p) {
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
return [$h("div", {"data-part": "header", "$spread1": () => attrs()}, () => [$dyn(() => children())])];
}
function DialogTitle($p) {
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
return [$h("h2", {"data-part": "title", "$spread1": () => attrs()}, () => [$dyn(() => children())])];
}
function DialogDescription($p) {
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
return [$h("p", {"data-part": "description", "$spread1": () => attrs()}, () => [$dyn(() => children())])];
}
return { Dialog, DialogTrigger, DialogContent, DialogHeader, DialogTitle, DialogDescription };
})();
// card.pyweb
const { Card, 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 CardContent($p) {
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
return [$h("div", {"data-part": "content", "$spread1": () => attrs()}, () => [$dyn(() => children())])];
}
return { Card, CardContent };
})();
function Projects($s) {
const projects = $signal("projects" in $s ? $s["projects"] : null, "projects");
const __pw_forms__ = $s["__pw_forms__"];
$live(projects, $s["$live:projects"]);
return [$h("div", {"class": "page"}, () => [
$h("div", {"class": "page-head"}, () => [
$h("h1", null, () => [$t("Projects")]),
Dialog({"children": () => [
DialogTrigger({"children": () => [$t("New project")], "$rest": () => ({"id": "new-project"})}),
DialogContent({"children": () => [
DialogHeader({"children": () => [
DialogTitle({"children": () => [$t("New project")]}),
DialogDescription({"children": () => [$t("It starts with a short checklist, made by a background job.")]})
]}),
$h("form", {"method": "post", "action": $py.at($py.at(__pw_forms__, "create_project"), "url"), "enctype": $py.at($py.at(__pw_forms__, "create_project"), "enctype"), "data-pw-form": "create_project", "data-pw-rules": $py.at($py.at(__pw_forms__, "create_project"), "rules")}, () => [
$list(() => $py.at($py.at(__pw_forms__, "create_project"), "hidden"), (__pw_h) => [$h("input", {"type": "hidden", "name": $py.at(__pw_h, "name"), "value": $py.at(__pw_h, "value")})]),
$h("p", {"class": "pw-form-error", "role": "alert", "data-pw-form-error": true, "hidden": !$py.truth($py.at($py.at(__pw_forms__, "create_project"), "error"))}, () => [$t($py.text($py.at($py.at(__pw_forms__, "create_project"), "error")))]),
$h("div", {"class": "pw-field pw-input", "data-pw-field": "name"}, () => [
$h("label", {"for": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "id")}, () => [$t($py.text($py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "label")))]),
$h("input", {"id": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "id"), "name": "name", "required": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "required"), "aria-invalid": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "invalid"), "aria-describedby": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "describedby"), "type": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "type"), "value": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "value"), "checked": (($py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "type") === "checkbox") ? $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "checked") : null), "minlength": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "minlength"), "maxlength": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "maxlength"), "min": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "min"), "max": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "max"), "step": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "step"), "placeholder": "Website relaunch"}),
$when(() => $py.truth($py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "help")), () => [$h("p", {"class": "pw-help"}, () => [$t($py.text($py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "help")))])]),
$h("p", {"class": "pw-error", "id": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "describedby"), "hidden": !$py.truth($py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "error"))}, () => [$t($py.text($py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "name"), "error")))])
]),
$h("div", {"class": "pw-field pw-textarea", "data-pw-field": "about"}, () => [
$h("label", {"for": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "about"), "id")}, () => [$t($py.text($py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "about"), "label")))]),
$h("textarea", {"id": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "about"), "id"), "name": "about", "required": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "about"), "required"), "aria-invalid": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "about"), "invalid"), "aria-describedby": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "about"), "describedby"), "minlength": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "about"), "minlength"), "maxlength": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "about"), "maxlength"), "rows": "2", "placeholder": "What is it about? (optional)"}, () => [$t($py.text($py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "about"), "value")))]),
$when(() => $py.truth($py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "about"), "help")), () => [$h("p", {"class": "pw-help"}, () => [$t($py.text($py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "about"), "help")))])]),
$h("p", {"class": "pw-error", "id": $py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "about"), "describedby"), "hidden": !$py.truth($py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "about"), "error"))}, () => [$t($py.text($py.at($py.at($py.at($py.at(__pw_forms__, "create_project"), "fields"), "about"), "error")))])
]),
$h("button", {"type": "submit", "data-pw-submit": true}, () => [$t("Create project")])
])
]})
]})
]),
$h("ul", {"id": "projects", "class": "grid"}, () => [$list(() => projects(), (p) => [$h("li", {"data-pw-motion": JSON.stringify({"animate": "fade-up", "layout": true}), "style": ((i, s) => s)(null, null, null, "fade-up")}, () => [$h("a", {"class": "pw-card project", "href": `/projects/${$py.str($py.at(p, "id"))}`}, () => [
$h("b", null, () => [$t($py.text($py.at(p, "name")))]),
$h("span", {"class": "muted"}, () => [$t($py.text($py.or($py.at(p, "about"), () => "No description")))])
])])])]),
$when(() => !$py.truth(projects()), () => [Card({"children": () => [CardContent({"children": () => [
$h("p", null, () => [$h("b", null, () => [$t("No projects yet")])]),
$h("p", {"class": "muted"}, () => [$t("Start one with New project: it comes with a checklist.")])
]})], "$rest": () => ({"class_": "empty-state", "data-pw-motion": JSON.stringify({"animate": "fade"})})})])
])];
}
$mount("Projects", Projects);
// ProjectPage.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";
import "./forms.js";
import { subscribe as $subscribe, live as $live, join as $join } from "./live.js";
// 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 };
})();
// display.pyweb
const { Badge } = (() => {
function Badge($p) {
const variant = $p["variant"] || (() => "soft");
const tone = $p["tone"] || (() => null);
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
return [$h("span", {"class": "pw-badge", "data-part": "badge", "data-variant": () => variant(), "data-tone": () => tone(), "$spread4": () => attrs()}, () => [$dyn(() => children())])];
}
return { Badge };
})();
// card.pyweb
const { Card, 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 CardContent($p) {
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
return [$h("div", {"data-part": "content", "$spread1": () => attrs()}, () => [$dyn(() => children())])];
}
return { Card, CardContent };
})();
// controls.pyweb
const { Checkbox } = (() => {
function Checkbox($p) {
const label = $p["label"] || (() => null);
const checked = $p["checked"] || (() => false);
const indeterminate = $p["indeterminate"] || (() => false);
const name = $p["name"] || (() => null);
const value = $p["value"] || (() => null);
const disabled = $p["disabled"] || (() => false);
const children = $p["children"] || (() => null);
const attrs = $p["$rest"] || (() => ({}));
return [$h("label", {"class": "pw-check", "data-part": "root", "data-disabled": () => ($py.truth(disabled()) ? "" : null)}, () => [
$h("input", {"type": "checkbox", "class": "pw-checkbox", "data-pw-ui": "checkbox", "data-part": "checkbox", "name": () => name(), "value": () => value(), "checked": () => checked(), "disabled": () => disabled(), "data-indeterminate": () => ($py.truth(indeterminate()) ? "true" : null), "$spread9": () => attrs()}),
$when(() => $py.truth(label()), () => [$h("span", {"data-part": "label"}, () => [$dyn(() => label())])]),
$dyn(() => children())
])];
}
return { Checkbox };
})();
function ProjectPage($s) {
const project = $s["project"];
const tasks = $signal("tasks" in $s ? $s["tasks"] : null, "tasks");
const left = $computed(() => $py.len($py.iter(tasks()).filter((t) => !$py.truth($py.at(t, "done")))));
const __pw_forms__ = $s["__pw_forms__"];
$live(tasks, $s["$live:tasks"]);
return [$h("div", {"class": "page"}, () => [
$h("p", null, () => [Button({"href": () => "/projects", "variant": () => "ghost", "size": () => "sm", "children": () => [$t("\u2190 Projects")]})]),
$h("div", {"class": "page-head"}, () => [
$h("h1", null, () => [$t($py.text(project.name))]),
Badge({"variant": () => "secondary", "children": () => [
$dyn(() => left()),
$t(" open")
], "$rest": () => ({"id": "left"})})
]),
$when(() => $py.truth(project.about), () => [$h("p", {"class": "muted"}, () => [$t($py.text(project.about))])]),
Card({"children": () => [CardContent({"children": () => [
$h("form", {"method": "post", "action": $py.at($py.at(__pw_forms__, "add_task"), "url"), "enctype": $py.at($py.at(__pw_forms__, "add_task"), "enctype"), "data-pw-form": "add_task", "data-pw-rules": $py.at($py.at(__pw_forms__, "add_task"), "rules")}, () => [
$list(() => $py.at($py.at(__pw_forms__, "add_task"), "hidden"), (__pw_h) => [$h("input", {"type": "hidden", "name": $py.at(__pw_h, "name"), "value": $py.at(__pw_h, "value")})]),
$h("p", {"class": "pw-form-error", "role": "alert", "data-pw-form-error": true, "hidden": !$py.truth($py.at($py.at(__pw_forms__, "add_task"), "error"))}, () => [$t($py.text($py.at($py.at(__pw_forms__, "add_task"), "error")))]),
$h("input", {"type": "hidden", "name": "project_id", "value": $py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "project_id"), "value")}),
$h("div", {"class": "pw-field pw-input", "data-pw-field": "title"}, () => [
$h("input", {"id": $py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "id"), "name": "title", "required": $py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "required"), "aria-invalid": $py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "invalid"), "aria-describedby": $py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "describedby"), "type": $py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "type"), "value": $py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "value"), "checked": (($py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "type") === "checkbox") ? $py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "checked") : null), "minlength": $py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "minlength"), "maxlength": $py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "maxlength"), "min": $py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "min"), "max": $py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "max"), "step": $py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "step"), "placeholder": "Add a task", "aria-label": $py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "label")}),
$when(() => $py.truth($py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "help")), () => [$h("p", {"class": "pw-help"}, () => [$t($py.text($py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "help")))])]),
$h("p", {"class": "pw-error", "id": $py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "describedby"), "hidden": !$py.truth($py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "error"))}, () => [$t($py.text($py.at($py.at($py.at($py.at(__pw_forms__, "add_task"), "fields"), "title"), "error")))])
]),
$h("button", {"type": "submit", "data-pw-submit": true}, () => [$t("Add")])
]),
$h("ul", {"id": "tasks"}, () => [$list(() => tasks(), (t) => [$h("li", {"class": {"done": $py.at(t, "done")}, "data-pw-motion": JSON.stringify({"initial": {"opacity": 0, "y": (-6)}, "animate": {"opacity": 1, "y": 0}, "exit": {"opacity": 0, "x": 24}, "layout": true}), "style": ((i, s) => s)({"opacity": 0, "y": (-6)}, null, null, {"opacity": 1, "y": 0})}, () => [
Checkbox({"checked": () => $py.at(t, "done"), "$rest": () => ({"onchange": (async () => ((await $rpc("toggle", {"task_id": $py.at(t, "id")})))), "aria-label": $py.add("Done: ", $py.at(t, "title"))})}),
$h("span", null, () => [$t($py.text($py.at(t, "title")))]),
Button({"variant": () => "ghost", "size": () => "icon", "onclick": () => (async () => ((await $rpc("remove", {"task_id": $py.at(t, "id")})))), "children": () => [$t("\u00d7")], "$rest": () => ({"class_": "remove", "aria-label": "Remove"})})
])])]),
$when(() => !$py.truth(tasks()), () => [$h("p", {"class": "empty"}, () => [$t("No tasks yet. Add the first one above.")])])
]})]})
])];
}
$mount("ProjectPage", ProjectPage);