Migrating from Jinja2 + FastAPI

WebComPy lets Python teams replace request-scoped Jinja2 views and ad-hoc JavaScript with reactive Python components while keeping an existing FastAPI backend running throughout the transition.

Important framing: WebComPy's render_template() compiles an intentional reactive subset — it is not a drop-in Jinja2 engine. Inheritance directives ({% extends %}, {% block %}, {% macro %}, {% include %}, {% set %}) raise WebComPyException at compile time because Python components, props, and slots take over composition. See Jinja2 Compatibility Notes for every exact error string and replacement.

Architecture shift#

Moving from Jinja2 + FastAPI to WebComPy changes where UI composition happens, how state evolves after initial load, and how server routes collaborate with templates:

Concern Jinja2 + FastAPI WebComPy
Rendering lifecycle Request-scoped: server renders HTML string per HTTP request Dual-environment: same Python components pre-render SSR/SSG HTML on server and hydrate into live PyScript DOM in browser
Template composition {% extends %}, {% block %}, {% include %}, {% macro %} @define_component() functions, typed props, slots, and RouterView()
Local state & updates Stateless strings on server; manual JS/HTMX mutations in browser Reactive Signal, Computed, ReactiveList, ReactiveDict, use_* composables
URL routing FastAPI @app.get("/...") returning TemplateResponse Backend keeps /api/* routes; frontend owns page routes via Router and RouterLink
Initial page data Context dict passed directly into templates.TemplateResponse(...) Component fetches /api/* via HttpClient / use_async_result() (or typed RPC), baked into #__webcompy_data__ during SSR
Forms & mutations HTML form POST + server validation + HTTP 303 redirect (PRG) Client validation with use_field() / use_form() + :bind, submitting asynchronously to JSON/RPC endpoints
Styling Global CSS stylesheets referenced in <head> Per-component scoped_style dicts or css_text() in webcompy-scope + theme tokens
Automated tests FastAPI TestClient checking response HTML strings Browserless TestRenderer + create_test_asgi_app() + real-browser Playwright E2E

Choosing an integration strategy#

Pick the hosting topology that matches how much of your application is moving to WebComPy right now:

Strategy Process owner When to choose Trade-offs
1. Standalone WebComPy python -m webcompy start or static host (webcompy generate) New frontend with external APIs or pure static/client-side app Simplest setup; backend routes live in another service or are mocked via middleware
2. Mount backend under WebComPy WebComPy CLI (WebComPyServerConfig.mounts) Green-field or UI-first app that only needs a handful of lightweight ASGI API endpoints WebComPy owns server lifecycle and generate SSR fetches; mounted apps cannot shadow / or /_webcompy*
3. Host mounts WebComPy (Recommended for migration) Existing FastAPI host (uvicorn host:app) Migrating an existing FastAPI + Jinja2 monolith cleanly route by route Zero disruption to auth, DB pools, middleware, background tasks, or legacy HTML routes

Strategy 1: Standalone WebComPy#

When a frontend only talks to already-deployed JSON services or runs fully statically, configure your project normally with WebComPyApp and serve via webcompy start or webcompy generate:

from webcompy.app import WebComPyApp

app = WebComPyApp(root_component=AppRoot, router=router)

Strategy 2: Mount FastAPI under WebComPy (mounts)#

When WebComPy already owns the server command, attach FastAPI routers under an /api prefix in webcompy_config.py using WebComPyServerConfig.mounts:

from fastapi import FastAPI
from webcompy_cli.config import WebComPyBuildConfig, WebComPyServerConfig

import my_app.app as app_module

api = FastAPI()


@api.get("/health")
def health():
    return {"status": "ok"}


config = WebComPyBuildConfig(
    app_module,
    server=WebComPyServerConfig(port=8080, mounts=lambda: {"/api": api}),
)

See Mounting ASGI apps under WebComPy for validation rules and static generation interaction.

Strategy 3: Host mounts WebComPy (Strangler pattern)#

When migrating an active Jinja2 + FastAPI app, keep FastAPI in charge and mount WebComPy under a dedicated prefix such as /app first, while legacy Jinja2 routes continue serving untouched paths:

from fastapi import FastAPI
from webcompy.app import WebComPyApp, WebComPyAppConfig
from webcompy_cli import create_asgi_app
from webcompy_cli.config import WebComPyBuildConfig
from webcompy_server import configure_server_context

import frontend.app as app_module

frontend_app = WebComPyApp(
    root_component=AppRoot,
    router=frontend_router,
    config=WebComPyAppConfig(base_url="/app/"),
)

serving = create_asgi_app(frontend_app, WebComPyBuildConfig(app_module, app_var="frontend_app"), mode="prod")

host = FastAPI()


@host.get("/api/users")
async def list_users():
    return [...]


host.mount("/app", serving.asgi)
configure_server_context(frontend_app, root_app=host)

Pair base_url with the mount prefix: WebComPyAppConfig(base_url="/app/") MUST match host.mount("/app", ...). If they drift, emitted <base> tags, PyScript wheel URLs, /_webcompy-ui/ stylesheets, and history-mode router links resolve outside the mount prefix. See the complete runnable host-mount snippet and checklist in Embedding WebComPy in a FastAPI or Starlette host.

Below is a small user-directory page implemented the traditional Jinja2 + FastAPI way (Before), followed immediately by its reactive WebComPy equivalent running live in your browser (After). Both share the exact same /api/users data contract.

Before: Jinja2 templates + FastAPI route#

<!-- templates/base.html  [1] -->
<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}App{% endblock %}</title>
    <link rel="stylesheet" href="/static/app.css"> <!-- [6] -->
</head>
<body>
    <header class="shell-header">
        <h1>{% block heading %}Directory{% endblock %}</h1>
    </header>
    <main>{% block content %}{% endblock %}</main>
</body>
</html>
<!-- templates/users.html  [2] -->
{% extends "base.html" %}
{% block title %}Team Directory{% endblock %}
{% block heading %}Team Directory{% endblock %}

{% block content %}
<form method="get" action="/users" class="controls"> <!-- [3] -->
    <input type="search" name="q" value="{{ q }}" placeholder="Filter by name or email...">
    <input type="hidden" name="role" value="{{ role }}">
    <button type="submit">Filter</button>
</form>

{% for user in users %} <!-- [5] -->
    <p>{{ user.name }} ({{ user.role | title }})</p>
{% else %}
    <p class="empty-state">No matching users.</p>
{% endfor %}
{% endblock %}
# main.py  [4]
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates

app = FastAPI()
templates = Jinja2Templates(directory="templates")

USERS = [
    {"id": 1, "name": "Alice Chen", "email": "[email protected]", "role": "admin", "active": True},
    {"id": 2, "name": "Bob Martinez", "email": "[email protected]", "role": "editor", "active": True},
]


@app.get("/users")
def users_page(request: Request, q: str = "", role: str = "all"):
    matched = [
        u
        for u in USERS
        if (role == "all" or u["role"] == role)
        and (not q or q.lower() in u["name"].lower() or q.lower() in u["email"].lower())
    ]
    return templates.TemplateResponse(
        request,
        "users.html",
        {"users": matched, "q": q, "role": role},
    )


@app.get("/api/users")
def list_users_api():
    return USERS

The interactive WebComPy version below replaces TemplateResponse with PageShell, use_async_result, HttpClient.get("/api/users", response_type=list[User]), and :bind + Computed. On this static documentation site, create_users_mock() intercepts /api/users through FetchMiddleware so filtering and reloads complete locally with zero failing network requests.

WebComPy Live Demo — Team Directory

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

from webcompy.ajax import HttpClient
from webcompy.app import WebComPyApp
from webcompy.components import ComponentContext, define_component, use_async_result
from webcompy.elements import html, switch
from webcompy.ports import (
    FetchMiddleware,
    FetchNext,
    FetchRequest,
    FetchStream,
    Response,
    add_fetch_middleware,
)
from webcompy.signal import Computed, ReactiveList, use_reactive_list, use_state
from webcompy.template import render_template


@dataclass
class User:
    id: int
    name: str
    email: str
    role: str
    active: bool


_SEEDED_USERS: list[dict[str, Any]] = [
    {"id": 1, "name": "Alice Chen", "email": "[email protected]", "role": "admin", "active": True},
    {"id": 2, "name": "Bob Martinez", "email": "[email protected]", "role": "editor", "active": True},
    {"id": 3, "name": "Carol Okafor", "email": "[email protected]", "role": "viewer", "active": True},
    {"id": 4, "name": "David Kim", "email": "[email protected]", "role": "editor", "active": False},
    {"id": 5, "name": "Elena Novak", "email": "[email protected]", "role": "admin", "active": True},
    {"id": 6, "name": "Frank Sato", "email": "[email protected]", "role": "viewer", "active": True},
]


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_users_mock() -> FetchMiddleware:
    async def users_mock(request: FetchRequest, next: FetchNext) -> Response | FetchStream:
        path = request.url.split("?", 1)[0]
        if path == "/api/users" and request.method == "GET":
            return _json_response(list(_SEEDED_USERS))
        return await next(request)

    return users_mock


class PageShellProps(TypedDict):
    title: str


@define_component()
def PageShell(context: ComponentContext[PageShellProps]):
    return html.DIV(
        {"class": "page-shell"},
        html.HEADER(
            {"class": "shell-header"},
            html.H1({}, context.props["title"]),
            html.SPAN({"class": "shell-badge"}, "Live SPA"),
        ),
        html.MAIN({"class": "shell-main"}, context.slots("default")),
    )


PageShell.scoped_style = {
    ".page-shell": {
        "font-family": "var(--font-sans, sans-serif)",
        "padding": "var(--space-3)",
        "color": "var(--color-fg)",
        "max-width": "42rem",
    },
    ".shell-header": {
        "display": "flex",
        "align-items": "center",
        "justify-content": "space-between",
        "padding-bottom": "var(--space-2)",
        "border-bottom": "1px solid var(--color-border)",
        "margin-bottom": "var(--space-3)",
    },
    ".shell-header h1": {
        "margin": "0",
        "font-size": "var(--font-size-xl, 1.25rem)",
    },
    ".shell-badge": {
        "font-size": "0.75rem",
        "padding": "0.15rem 0.5rem",
        "border-radius": "var(--radius-sm)",
        "border": "1px solid var(--color-border)",
        "background": "var(--color-bg-elevated)",
        "color": "var(--color-link)",
        "font-weight": "600",
    },
}


@define_component()
def UserDirectory(_: ComponentContext[None]):
    query = use_state(lambda: "")
    role_filter = use_state(lambda: "all")
    users: ReactiveList[User] = use_reactive_list(lambda: [])

    async def _load_users() -> list[User]:
        loaded = await HttpClient.get("/api/users", response_type=list[User])
        users.value = loaded
        return loaded

    users_result = use_async_result(_load_users)

    filtered_users = Computed(
        lambda: [
            user
            for user in users.value
            if (role_filter.value == "all" or user.role == role_filter.value)
            and (
                not query.value.strip()
                or query.value.strip().lower() in user.name.lower()
                or query.value.strip().lower() in user.email.lower()
            )
        ]
    )

    summary_text = Computed(lambda: f"Showing {len(filtered_users.value)} of {len(users.value)} users")

    def _set_role(role: str):
        def _handler(_ev: Any) -> None:
            role_filter.value = role

        return _handler

    def _clear_filters(_ev: Any) -> None:
        query.value = ""
        role_filter.value = "all"

    def _render_results():
        return render_template(
            """
            <div class="users-results">
                <ul class="user-list">
                    {% for user in filtered_users %}
                        <li class="user-item">
                            <div class="user-primary">
                                <strong>{{ user.name }}</strong>
                                <span class="user-email">{{ user.email }}</span>
                            </div>
                            <div class="user-badges">
                                <span class="role-badge">{{ user.role | title }}</span>
                                <span class="status-badge">{{ "Active" if user.active else "Inactive" }}</span>
                            </div>
                        </li>
                    {% endfor %}
                </ul>
                {% if not filtered_users %}
                    <p class="empty-state">No matching users.</p>
                {% endif %}
            </div>
            """,
            {"filtered_users": filtered_users},
        )

    role_buttons = [
        html.BUTTON(
            {
                "type": "button",
                "class": Computed(lambda r=role: "role-btn active" if role_filter.value == r else "role-btn"),
                "@click": _set_role(role),
            },
            label,
        )
        for role, label in [("all", "All"), ("admin", "Admin"), ("editor", "Editor"), ("viewer", "Viewer")]
    ]

    return html.DIV(
        {"class": "users-demo"},
        html.DIV(
            {"class": "controls"},
            html.INPUT(
                {
                    "type": "search",
                    "class": "search-input",
                    "placeholder": "Filter by name or email...",
                    "aria-label": "Filter users",
                    ":bind": query,
                }
            ),
            html.BUTTON({"type": "button", "class": "clear-btn", "@click": _clear_filters}, "Clear"),
        ),
        html.DIV({"class": "role-tabs", "role": "group", "aria-label": "Filter by role"}, *role_buttons),
        html.P({"class": "user-summary"}, summary_text),
        switch(
            {"case": users_result.is_success, "generator": _render_results},
            default=lambda: html.P({"class": "loading-state"}, "Loading users..."),
        ),
    )


UserDirectory.scoped_style = {
    ".controls": {
        "display": "flex",
        "gap": "var(--space-2)",
        "margin-bottom": "var(--space-2)",
    },
    ".search-input": {
        "flex": "1",
        "min-width": "0",
        "padding": "0.4rem 0.6rem",
        "border": "1px solid var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "background": "var(--color-bg)",
        "color": "var(--color-fg)",
    },
    ".clear-btn, .role-btn": {
        "padding": "0.35rem 0.7rem",
        "border": "1px solid var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "background": "var(--color-bg-elevated)",
        "color": "var(--color-fg)",
        "cursor": "pointer",
    },
    ".role-tabs": {
        "display": "flex",
        "flex-wrap": "wrap",
        "gap": "var(--space-2)",
        "margin-bottom": "var(--space-2)",
    },
    ".role-btn.active": {
        "background": "var(--color-accent)",
        "border-color": "var(--color-accent)",
        "color": "var(--color-on-accent)",
        "font-weight": "600",
    },
    ".user-summary": {
        "font-size": "var(--font-size-sm, 0.875rem)",
        "color": "var(--color-fg-muted)",
        "margin": "0 0 var(--space-2) 0",
    },
    ".user-list": {
        "list-style": "none",
        "padding": "0",
        "margin": "0",
    },
    ".user-list:empty": {
        "display": "none",
    },
    ".user-item": {
        "display": "flex",
        "justify-content": "space-between",
        "align-items": "center",
        "flex-wrap": "wrap",
        "gap": "var(--space-2)",
        "padding": "var(--space-2) var(--space-3)",
        "border": "1px solid var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "margin-bottom": "var(--space-2)",
        "background": "var(--color-bg-card)",
        "color": "var(--color-fg)",
    },
    ".user-primary": {
        "display": "flex",
        "flex-direction": "column",
        "gap": "0.15rem",
    },
    ".user-email": {
        "font-size": "var(--font-size-sm, 0.875rem)",
        "color": "var(--color-fg-muted)",
    },
    ".user-badges": {
        "display": "flex",
        "gap": "var(--space-2)",
        "font-size": "0.75rem",
    },
    ".role-badge, .status-badge": {
        "padding": "0.15rem 0.5rem",
        "border": "1px solid var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "background": "var(--color-bg-elevated)",
        "color": "var(--color-fg)",
    },
    ".empty-state, .loading-state": {
        "padding": "var(--space-3)",
        "border": "1px dashed var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "color": "var(--color-fg-muted)",
    },
}


@define_component()
def MigrationUsersApp(_: ComponentContext[None]):
    add_fetch_middleware(create_users_mock())
    return PageShell(
        {"title": "Team Directory"},
        slots={"default": lambda: UserDirectory(None)},
    )


app = WebComPyApp(root_component=MigrationUsersApp)
app.run()

Why each line changed#

Each numbered callout in the "Before" code corresponds directly to a pattern in the WebComPy source shown under Code above:

  1. base.html → PageShell component with slots [1]: Instead of HTML file inheritance, layout structure becomes a typed Python @define_component() that places context.slots("default") inside <main>. During SSR/SSG and client routing, the shell renders real Light DOM wrappers without re-parsing shared HTML fragments.
  2. {% extends %} / {% block %} → parent wrapper call [2]: MigrationUsersApp calls PageShell({"title": "Team Directory"}, slots={"default": ...}). Multi-page apps go one step further and put a persistent nav shell around RouterView().
  3. <form method="get"> full-page reload → :bind + Computed [3]: Typing into the search input updates query immediately through :bind="query". Recomputing filtered_users refreshes only the matching list rows in the DOM without any HTTP round-trip or page flicker.
  4. TemplateResponse context dict → HttpClient + use_async_result() [4]: The backend simplifies to a typed JSON endpoint (@app.get("/api/users")), while the component loads data asynchronously via HttpClient.get("/api/users", response_type=list[User]). Notice that keeping /api/users mocked via FetchMiddleware lets the demo and unit tests run offline before rewiring to FastAPI.
  5. {% for %}...{% else %} → reactive {% for %} loop + sibling {% if not filtered_users %} [5]: WebComPy rejects {% else %} directly inside {% for %} ({% else %} outside of {% if %} block). Rendering the <ul class="user-list"> loop alongside a separate {% if not filtered_users %}...{% endif %} guard inside render_template() keeps the reactive list mounted while toggling the empty-state message cleanly whenever filters match zero rows.
  6. Global app.css → PageShell.scoped_style / UserDirectory.scoped_style [6]: Component CSS classes are isolated automatically in webcompy-scope, preventing selector leaks across migrated and unmigrated screens.

Directive-by-directive conversion table#

Use this quick lookup while porting .html templates into components and render_template():

Jinja2 construct Status WebComPy replacement Reference
{% extends "base.html" %} Rejected Root layout component wrapping RouterView() or shell component with slots Components / Routing
{% block body %}...{% endblock %} Rejected Named/default entries in slots={"body": lambda: ...} read via context.slots(...) Components
{% include "part.html" %} Rejected Extract a @define_component() and render via Python call or <my-part> tag HTML Templates
{% macro %} / {% call %} Rejected Define a helper function returning html.* or a @define_component() with slots Components
{% import %} / {% from ... import %} Rejected Standard Python import statements at module or setup scope Components
{% set x = ... %} / {% with %} Rejected Python local variables or Computed(...) / use_computed(...) signals passed in context Reactive State
{% filter upper %}...{% endfilter %} Rejected Apply expression filter {{ text | upper }} or Python helper function Compatibility Notes
{{ val | upper }} (16 core filters) Supported / Renamed Built-in filter pipe (trim, length, join, default, etc.) or Python helper in context Compatibility Notes
{% if %} / {% elif %} / {% else %} Supported Same directive syntax; binds reactive SwitchElement when reading a Signal HTML Templates
{% for x in items %} & loop.* Supported Same loop syntax + loop.index, index0, revindex, revindex0, first, last, length HTML Templates
{% for x in items %}...{% else %} Rejected Reactive {% for ... %}{% endfor %} loop paired with a sibling {% if not items %} empty-state guard Compatibility Notes
{# comment #} & {% raw %} Supported Stripped comments and verbatim {% raw %}...{% endraw %} blocks work identically HTML Templates
{{ val | safe }} Rejected raw_html(...) element helper for sanitized HTML Elements & Events
{{ url_for("name", id=1) }} Replaced RouterLink(to="/items/{id}", path_params=...) + WebComPyAppConfig.base_url Routing
{% trans %} / {% pluralize %} Rejected use_i18n() translation catalog and cookie locale helpers Internationalization

Concern-by-concern migration#

1. Routing split: pages vs data routes#

Separate browser navigation from HTTP data APIs cleanly:

  • Keep FastAPI endpoints under JSON prefixes such as /api/..., health checks (/healthz), webhooks, and OAuth callbacks on the host.
  • Move HTML page routes (GET /dashboard, GET /users/{id}) into WebComPy's client Router using mode="history". During host embedding at /app, client routes map /app/ → "/" and /app/users/{id} → "/users/{id}" inside WebComPy once base_url="/app/" is paired with host.mount("/app", ...).
  • Learn flat routing and query/path params in Routing, and nested layouts, navigation guards (before_route_change), and lazy() code splitting in Routing Deep Dive.

2. Context dicts → HttpClient, typed RPC, and hydration baking#

In FastAPI + Jinja2, your route handler prepares a dict synchronously and passes it to TemplateResponse. In WebComPy, components request data themselves with HttpClient.get(...) + use_async_result() or declarative RPC Contracts:

  1. During SSR on the server, configure_server_context(app, root_app=host) routes internal HttpClient requests (such as /api/users) in-process directly against your FastAPI host via ASGI transport — zero loopback TCP overhead.
  2. Resolved AsyncResult states, HTTP response caches, and use_* signal values are baked into <script id="__webcompy_data__"> inside the SSR HTML.
  3. When PyScript boots in the browser, initial components hydrate immediately from that baked payload without re-fetching /api/users on initial load.

Read HTTP & Async for client calls and Hydration & Data Transfer for payload gating, factory skip rules, and collision-safe transfer IDs.

3. Forms: POST-Redirect-GET → use_field / use_form + :bind#

Replace server form postbacks and flash redirects with typed reactive validation and JSON submission:

@host.post("/api/users")
async def create_user(payload: CreateUserPayload):
    ...

Bind fields interactively using use_field(use_state(...), validators=[...]), aggregate with use_form(...), wire inputs with :bind or webcompy.ui controls (Input, FormField, Select, Checkbox), and call HttpClient.post("/api/users", json=values) inside form.submit(...). See Forms and UI Primitives.

4. Global CSS → component scoped_style and cascade layers#

Break monolithic global CSS files into per-component scoped_style dictionaries (or multi-rule CSS strings parsed via css_text()):

  • Styles attached to Component.scoped_style isolate selectors by webcompy-cid-* attribute inside the webcompy-scope layer.
  • Shared typography, spacing, and color tokens come from --color-* and --space-* CSS variables documented in Styling & Themes and Styling Deep Dive.

5. Auth and sessions via cookies + middleware headers#

Existing cookie-based sessions and bearer headers carry straight over because WebComPy runs on the same origin under your FastAPI host:

  • HttpOnly session cookies: Browser fetch() sends same-origin cookies automatically; during SSR, ServerFetchPort forwards request context cleanly against root_app=host.
  • Bearer / CSRF headers: Register one shared FetchMiddleware (or RpcMiddleware for typed RPC calls) inside root setup/plugin setup to attach Authorization or X-CSRF-Token headers uniformly on every client request. See Middleware.

6. Reusing FastAPI SSE and WebSocket endpoints#

Existing FastAPI StreamingResponse(..., media_type="text/event-stream") and @app.websocket("/ws") endpoints need zero rewrite on the backend — consume them directly with use_event_source() and use_websocket() in WebComPy components. See Server-Sent Events and WebSocket.

Phased strangler checklist#

Migrate safely in incremental deploys, verifying exit criteria before moving to the next phase:

Phase Goal Key mechanics & docs Exit criteria
Phase 0: Host mount smoke Mount empty shell at /app beside untouched Jinja2 routes ASGI Integration uvicorn host:app serves legacy / normally and /app/ returns 200 SSR HTML + hydrates without console errors
Phase 1: Shell & first read-only view Port base.html to layout component + one read-only page Components, Routing Page shell matches visual chrome; RouterLink navigation works client-side and refreshes directly at /app/<route>
Phase 2: Data routes & SSR transfer Move route context dict into FastAPI /api/* endpoint + use_async_result() HTTP & Async, Hydration & Data Transfer Initial HTML includes #__webcompy_data__; browser does not issue a duplicate GET /api/... on first load
Phase 3: Interactive forms Convert PRG forms to use_form() + :bind + JSON POST/PATCH Forms, Middleware Inline client validation runs before request; auth/CSRF headers attach via middleware; server mutation updates reactive view
Phase 4: Realtime & typed RPC Connect live dashboards/feeds to SSE/WebSocket or typed RPC contracts Server-Sent Events, RPC Contracts Shared FastAPI SSE/WebSocket endpoints stream cleanly into use_event_source / use_websocket with reconnect recovery
Phase N: Cut over base_url="/" Flip WebComPy mount to root once Jinja2 page routes are empty ASGI Integration, Serving Modes Remove Jinja2Templates dependency for UI routes; update WebComPyAppConfig(base_url="/") and mount serving.asgi at "/"

Gotchas and what stays server-side#

Common migration gotchas#

  • Self-site recursion guards (blocked_paths): During SSR, WebComPy blocks HttpClient from fetching WebComPy's own HTML page paths (blocked_paths), preventing recursive render loops. Always fetch JSON /api/* endpoints hosted by FastAPI rather than HTML routes — see blocked_paths matching rules.
  • base_url / mount prefix mismatch: Never change host.mount("/app", ...) without updating WebComPyAppConfig(base_url="/app/") in lockstep — see URL prefixing, base_url, and blocked paths.
  • Hyphenated component tag derivation: Calling a component declaratively inside render_template() requires a hyphenated tag matching the PascalCase function name (<page-shell> → PageShell). Unmatched hyphenated tags raise WebComPyException immediately; non-hyphenated tags stay plain HTML elements. See Component tags in templates.
  • Avoid passing locals() as template context: Jinja2 teams sometimes pass {**locals()} in prototypes. In WebComPy, pass an explicit context dictionary containing only the Signals, Computeds, plain data, and event handlers the template references so temporary setup variables never leak and KeyError catches typos cleanly.

What stays server-side (honest scope note)#

WebComPy is an interactive browser SPA + pre-rendering framework — not a general-purpose string template library. Keep Jinja2 (or plain Python formatting/f-strings) on your FastAPI server for:

  • Transactional email bodies (multipart HTML/plain emails sent by background workers)
  • PDF, invoice, and downloadable report generation fed to WeasyPrint / wkhtmltopdf
  • XML feeds, RSS, sitemaps, and server-to-server webhook payloads
  • Static email/fragment partials that never execute Python in the browser

Coming from Django templates? Django template syntax shares many constructs with Jinja2 ({{ var }}, {% if %}, {% for %}, filters, extends/block/include). The conversion tables above apply conceptually to Django templates too, though Django ORM views and WSGI adapters are outside this guide's FastAPI + ASGI scope.

Next steps#