Step 6 — Deploy

In this final step (~10 minutes), you add a protective ErrorBoundary around your routed views and generate a deployable static site bundle with webcompy generate. You also review the difference between pre-rendered HTML produced at build time and live API requests run in the browser.

The live interactive demo and complete source for this step appear at the bottom of the page.

Guarding runtime views with ErrorBoundary#

Wrap RouterView() inside ErrorBoundary (webcompy.elements) with a fallback generator receiving (error, reset) so an unexpected exception inside a route view offers recovery without breaking the surrounding shell:

from webcompy.elements import ErrorBoundary, html
from webcompy.router import RouterView

ErrorBoundary(
    children=lambda: RouterView(),
    fallback=lambda err, reset: html.DIV(
        {"class": "status error"},
        html.P({}, f"Something went wrong: {err}"),
        html.BUTTON({"type": "button", "@click": lambda _ev: reset()}, "Try again"),
    ),
    catch_events=True,
)

Async failures from use_async_result() are still handled inline through load_result.is_error, while ErrorBoundary catches rendering or event-callback exceptions in child components.

Building static output with webcompy generate#

From your project root, run:

uv run python -m webcompy generate

WebComPy pre-renders every configured route to HTML files inside dist/, bundles your Python application package wheel, copies files from static/, and prepares PyScript runtime assets for static hosting.

Deployment mode Command / output When to use
Dev / ASGI server (webcompy start) Live Python ASGI server (start --dev for hot reload) Local development or SSR apps serving live API endpoints alongside HTML
Static site (webcompy generate) Static dist/ directory Any static CDN, GitHub Pages, Netlify, or S3 bucket

Note that static generation renders one index.html per declared route; apps using mode="hash" work out of the box on any static host even when routes are dynamic or client-only.

What pre-rendering bakes vs what fetches at runtime#

When webcompy generate pre-renders your app:

  • Baked into initial HTML: static structure (heading, form markup, toolbar chrome) and initial state from use_state / use_reactive_list / use_async_result (which triggers hydration transfer when pre-rendering completes).
  • Executed after browser boot: interactive event listeners, form validation on user input, client-side HttpClient mutations (POST, PATCH), and any browser-only work registered in @on_mounted.

Because Step 5's tasks_api middleware runs inside the browser as well as on the server, the /api/tasks demo behaves identically whether served dynamically or deployed from dist/.

Next steps#

Congratulations on completing the ToDo application tutorial! Continue exploring WebComPy by topic:

Step 6 Live Demo

Demo
Code
import json
from dataclasses import dataclass
from typing import Any, TypedDict

from webcompy.aio import AsyncWrapper
from webcompy.ajax import HttpClient
from webcompy.app import WebComPyApp
from webcompy.components import ComponentContext, define_component, use_async_result
from webcompy.elements import ErrorBoundary, html, repeat, switch
from webcompy.forms import min_length, required, use_field, use_form
from webcompy.ports import (
    FetchMiddleware,
    FetchNext,
    FetchRequest,
    FetchStream,
    Response,
    add_fetch_middleware,
)
from webcompy.router import Router, RouterContext, RouterLink, RouterView
from webcompy.signal import ReactiveList, Signal, SignalBase, use_computed, use_state
from webcompy.ui import Checkbox, FormField, Input


@dataclass
class Task:
    id: int
    title: str
    done: bool


class TaskItem(TypedDict):
    id: int
    title: str
    done: Signal[bool]


class ToolbarProps(TypedDict):
    remaining: SignalBase[int]


def _json_response(payload: Any, status_code: int = 200, status_text: str = "OK") -> Response:
    return Response(
        text=json.dumps(payload),
        headers={"Content-Type": "application/json"},
        status_code=status_code,
        status_text=status_text,
        ok=status_code < 400,
    )


def create_tasks_api() -> FetchMiddleware:
    store: list[dict[str, Any]] = [
        {"id": 1, "title": "Read Getting Started", "done": True},
        {"id": 2, "title": "Connect to /api/tasks", "done": False},
        {"id": 3, "title": "Prepare for static deploy", "done": False},
    ]

    async def tasks_api(request: FetchRequest, next: FetchNext) -> Response | FetchStream:
        path = request.url.split("?", 1)[0]
        raw_body = request.body.decode("utf-8") if isinstance(request.body, bytes) else (request.body or "{}")
        if path == "/api/tasks" and request.method == "GET":
            return _json_response(store)
        if path == "/api/tasks" and request.method == "POST":
            payload = json.loads(raw_body)
            title = str(payload.get("title", "")).strip()
            if not title:
                return _json_response({"error": "title required"}, 400, "Bad Request")
            created = {"id": max((t["id"] for t in store), default=0) + 1, "title": title, "done": False}
            store.append(created)
            return _json_response(created, 201, "Created")
        if path.startswith("/api/tasks/") and request.method == "PATCH":
            task_id = int(path.rsplit("/", 1)[-1])
            payload = json.loads(raw_body)
            for item in store:
                if item["id"] == task_id:
                    item["done"] = bool(payload.get("done", False))
                    return _json_response(item)
            return _json_response({"error": "not found"}, 404, "Not Found")
        return await next(request)

    return tasks_api


tasks: ReactiveList[TaskItem] = ReactiveList([])


def _to_item(task: Task) -> TaskItem:
    return TaskItem(id=task.id, title=task.title, done=Signal(task.done))


@define_component()
def TodoItem(context: ComponentContext[TaskItem]):
    task = context.props

    @AsyncWrapper()
    async def persist_done(checked: bool) -> None:
        await HttpClient.patch(f"/api/tasks/{task['id']}", json={"done": checked}, response_type=Task)

    return html.LI(
        {"class": use_computed(lambda: "todo-item done" if task["done"].value else "todo-item")},
        Checkbox({"value": task["done"], "on_change": persist_done, "aria_label": f"Toggle {task['title']}"}),
        html.SPAN({"class": "task-title"}, task["title"]),
    )


TodoItem.scoped_style = {
    ".todo-item": {
        "display": "flex",
        "align-items": "center",
        "gap": "var(--space-2)",
        "padding": "var(--space-2) var(--space-3)",
        "border": "1px solid var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "background": "var(--color-bg-card)",
        "color": "var(--color-fg)",
        "margin-bottom": "var(--space-2)",
    },
    ".todo-item.done .task-title": {"text-decoration": "line-through", "color": "var(--color-fg-muted)"},
}


@define_component()
def TodoToolbar(context: ComponentContext[ToolbarProps]):
    remaining = context.props["remaining"]
    return html.DIV(
        {"class": "toolbar"},
        html.SPAN({"class": "remaining"}, use_computed(lambda: f"{remaining.value} remaining")),
        context.slots("default", fallback=lambda: None),
    )


TodoToolbar.scoped_style = {
    ".toolbar": {
        "display": "flex",
        "justify-content": "space-between",
        "align-items": "center",
        "gap": "var(--space-2)",
        "margin": "var(--space-3) 0",
    },
    ".filters": {"display": "flex", "gap": "var(--space-2)"},
    ".filters a": {
        "padding": "var(--space-1) var(--space-3)",
        "border": "1px solid var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "background": "var(--color-bg-card)",
        "color": "var(--color-fg)",
        "text-decoration": "none",
    },
    ".filters a.active": {
        "background": "var(--color-accent)",
        "border-color": "var(--color-accent)",
        "color": "var(--color-on-accent)",
        "font-weight": "600",
    },
}

_TASK_LIST_STYLE = {
    ".task-list": {"list-style": "none", "padding": "0", "margin": "0"},
}


def _render_task_list(mode: str):
    filtered = use_computed(
        lambda: [
            t
            for t in tasks.value
            if mode == "all" or (mode == "active" and not t["done"].value) or (mode == "done" and t["done"].value)
        ]
    )
    return html.UL(
        {"class": "task-list"},
        repeat(filtered, lambda task, _key: TodoItem(task), key=lambda task: task["id"]),
    )


@define_component()
def TodoAllPage(_: ComponentContext[RouterContext]):
    return _render_task_list("all")


TodoAllPage.scoped_style = _TASK_LIST_STYLE


@define_component()
def TodoActivePage(_: ComponentContext[RouterContext]):
    return _render_task_list("active")


TodoActivePage.scoped_style = _TASK_LIST_STYLE


@define_component()
def TodoDonePage(_: ComponentContext[RouterContext]):
    return _render_task_list("done")


TodoDonePage.scoped_style = _TASK_LIST_STYLE


@define_component()
def TodoNotFoundPage(_: ComponentContext[RouterContext]):
    return html.DIV(
        {"class": "not-found"},
        html.P({}, "View not found."),
        RouterLink(to="/", text=["Back to all tasks"]),
    )


TodoNotFoundPage.scoped_style = {
    ".not-found": {"color": "var(--color-fg)"},
    ".not-found a": {"color": "var(--color-link)"},
}


@define_component()
def TodoApp(_: ComponentContext[None]):
    add_fetch_middleware(create_tasks_api())
    tasks.value = []

    async def load_tasks() -> list[Task]:
        loaded = await HttpClient.get("/api/tasks", response_type=list[Task])
        tasks.value = [_to_item(t) for t in loaded]
        return loaded

    load_result = use_async_result(load_tasks)
    title_field = use_field(
        use_state(lambda: ""),
        validators=[required("Enter a task title"), min_length(3, "At least 3 characters")],
        name="title",
    )
    form = use_form(title=title_field)
    remaining = use_computed(lambda: sum(1 for t in tasks.value if not t["done"].value))

    async def on_submit(values: dict[str, Any]) -> None:
        created = await HttpClient.post("/api/tasks", json={"title": str(values["title"]).strip()}, response_type=Task)
        tasks.append(_to_item(created))
        form.reset()

    return html.DIV(
        {"class": "todo-shell"},
        html.H1({}, "ToDo Tutorial"),
        html.FORM(
            {"class": "todo-form", "@submit": form.submit(on_submit)},
            FormField(
                {"field": title_field, "label": "New task"},
                slots={"default": lambda: Input({"field": title_field, "placeholder": "What needs to be done?"})},
            ),
            html.BUTTON({"type": "submit", "disabled": form.submitting}, "Add Task"),
        ),
        TodoToolbar(
            {"remaining": remaining},
            slots={
                "default": lambda: html.NAV(
                    {"class": "filters"},
                    RouterLink(to="/", text=["All"], active_class="active", exact=True),
                    RouterLink(to="/active", text=["Active"], active_class="active", exact=True),
                    RouterLink(to="/done", text=["Done"], active_class="active", exact=True),
                )
            },
        ),
        switch(
            {
                "case": load_result.is_error,
                "generator": lambda: html.DIV(
                    {"class": "status error"},
                    html.P({}, f"Failed to load tasks: {load_result.error.value}"),
                    html.BUTTON({"type": "button", "@click": lambda _ev: load_result.refetch()}, "Retry"),
                ),
            },
            {
                "case": load_result.is_success,
                "generator": lambda: html.MAIN(
                    {},
                    ErrorBoundary(
                        children=lambda: RouterView(),
                        fallback=lambda err, reset: html.DIV(
                            {"class": "status error"},
                            html.P({}, f"Something went wrong: {err}"),
                            html.BUTTON({"type": "button", "@click": lambda _ev: reset()}, "Try again"),
                        ),
                        catch_events=True,
                    ),
                ),
            },
            default=lambda: html.P({"class": "status"}, "Loading tasks..."),
        ),
    )


TodoApp.scoped_style = {
    ".todo-shell": {
        "max-width": "32rem",
        "padding": "var(--space-3)",
        "font-family": "var(--font-sans)",
        "color": "var(--color-fg)",
    },
    ".todo-form": {
        "display": "flex",
        "align-items": "flex-end",
        "gap": "var(--space-2)",
        "margin-bottom": "var(--space-3)",
    },
    ".todo-form button": {
        "padding": "var(--space-2) var(--space-3)",
        "border": "1px solid var(--color-accent)",
        "border-radius": "var(--radius-sm)",
        "background": "var(--color-accent)",
        "color": "var(--color-on-accent)",
        "cursor": "pointer",
    },
    ".status": {"color": "var(--color-fg-muted)", "font-size": "var(--font-size-sm)"},
    ".status.error": {"color": "var(--color-danger)"},
    ".status.error button": {
        "padding": "var(--space-1) var(--space-3)",
        "border": "1px solid var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "background": "var(--color-bg-elevated)",
        "color": "var(--color-fg)",
        "cursor": "pointer",
    },
}

router = Router(
    {"path": "/", "component": TodoAllPage},
    {"path": "/active", "component": TodoActivePage},
    {"path": "/done", "component": TodoDonePage},
    default=TodoNotFoundPage,
    mode="hash",
)

app = WebComPyApp(root_component=TodoApp, router=router)
app.run()