Serving Modes
Every WebComPy deployment delivers three distinct runtime layers to the browser: the PyScript/Pyodide JavaScript and WebAssembly runtime, third-party Python dependencies (pure-Python or WASM-compiled), and your application plus webcompy core wheels.
Use WebComPyBuildConfig fields (or matching start / generate / prebuild CLI flags) to trade CDN caching speed during open-internet deployments against full self-hosting for firewalled, air-gapped, or offline environments, and pair with webcompy prebuild + create_prebuilt_asgi_app() when deploying to read-only containers or serverless runtimes.
Dependency classification#
When generating webcompy-lock.json, WebComPy inspects your declared dependencies against the pinned Pyodide package lock (pyscript_version / pyodide_version) and assigns each package to one of three mutually exclusive PackageKind categories:
PackageKind |
How it is detected | How it ships to the browser |
|---|---|---|
WASM |
Listed in the Pyodide lock with a wasm32 wheel filename (for example numpy) |
Loaded by Pyodide as a WASM wheel (from Pyodide CDN or /_webcompy-assets/packages/) |
CDN_PURE_PYTHON |
Listed in the Pyodide lock without wasm32 (for example packaging or httpx) |
Extracted and bundled into the app wheel when serve_all_deps=True, or loaded from CDN when False |
LOCAL_PURE_PYTHON |
Not in the Pyodide lock and contains no .so, .pyd, or .dylib native extensions |
Always bundled from your local Python environment into the app wheel |
If an explicit dependency is missing from both the Pyodide lock and the local environment — or exists locally with compiled .so / .pyd / .dylib extensions and no Pyodide WASM build — lock resolution fails with an actionable error before any server starts or static site builds. Transitive dependencies are walked recursively across both the Pyodide lock and local package metadata.
Runtime, WASM, and dependency serving matrix#
Four options on WebComPyBuildConfig (all overridable on python -m webcompy start and generate) govern where browser assets load from:
| Option | Default | Effect |
|---|---|---|
runtime_serving |
"cdn" (or "local" under standalone=True) |
"cdn" loads PyScript CSS/JS and Pyodide core from external CDN; "local" downloads, verifies, and serves them under /_webcompy-assets/{pyscript_version}/ |
wasm_serving |
"cdn" (or "local" under standalone=True) |
"cdn" fetches WASM wheels and pyodide-lock.json from CDN; "local" serves downloaded WASM wheels from /_webcompy-assets/packages/ |
serve_all_deps |
True |
True downloads, SHA-256 checks, and bundles CDN_PURE_PYTHON packages into the app wheel; False delegates them to py-config.packages on CDN |
standalone |
False |
Convenience switch: forces serve_all_deps=True and sets any unset wasm_serving or runtime_serving field to "local" |
Explicitly passing wasm_serving="cdn" or runtime_serving="cdn" alongside standalone=True honors the explicit field override while keeping serve_all_deps=True (passing standalone=True with serve_all_deps=False forces serve_all_deps=True with a warning on stderr).
Common deployment profiles:
| Scenario | Recommended settings | External network requests at browser boot? |
|---|---|---|
| Standard web deploy (fast small app wheel, CDN runtime) | standalone=False, runtime_serving="cdn", wasm_serving="cdn", serve_all_deps=True |
Yes (PyScript CDN + Pyodide WASM wheels if any) |
| Minimal app wheel (rely on Pyodide CDN for pure-Python deps) | serve_all_deps=False (--no-serve-all-deps) |
Yes (PyScript CDN + Pyodide CDN pure-Python & WASM) |
| Full self-hosted / zero-CDN deploy | standalone=True (--standalone) |
No (all HTML, runtime, WASM, UI CSS, and wheels served from same origin) |
In production mode (mode="prod"), locally served runtime assets carry Cache-Control: public, max-age=31536000, immutable keyed under the pinned pyscript_version prefix (/_webcompy-assets/{pyscript_version}/). Locally served WASM wheels (/_webcompy-assets/packages/) and content-hashed browser wheels (/_webcompy-app-package/) also emit Cache-Control: public, max-age=31536000, immutable in production mode, while framework UI stylesheets (/_webcompy-ui/) and static files receive deterministic ?v=<hash> cache-busting query parameters in production SSR and SSG HTML (and inside /_webcompy-ui/index.css's @import rules).
Wheel modes: bundled vs split#
Set wheel_mode on WebComPyBuildConfig (or --wheel-mode bundled|split) to control how PEP 427 browser wheels are emitted under /_webcompy-app-package/:
| Mode | Emitted wheels | Trade-offs |
|---|---|---|
"bundled" (default) |
Single <app>.whl containing your app, webcompy core, and bundled pure-Python dependencies |
Single wheel HTTP request at startup; any app code edit rebuilds and invalidates the full archive |
"split" |
Separate framework webcompy-<version>.whl (extra_wheel_filenames) and lighter <app>.whl |
Better caching when application code changes more often than webcompy; during webcompy start --dev, the framework wheel uses Cache-Control: max-age=86400, must-revalidate while the app wheel uses Cache-Control: no-cache |
Static generation (webcompy generate) copies every wheel produced by either mode into dist/_webcompy-app-package/. Build-once prebuilding (webcompy prebuild) produces the same layout inside <prebuilt_dir>/public/_webcompy-app-package/. Only webcompy core and bundled browser dependencies enter browser wheels — webcompy-server, webcompy-cli, and webcompy-testing remain server-only.
Prebuilt deployment & edge asset offload#
Running python -m webcompy prebuild writes three things cleanly:
<prebuilt_dir>/.gitignore(*) so build outputs stay untracked without editing project-root ignore rules.<prebuilt_dir>/public/mirroring URL paths 1:1 (_webcompy-app-package/, rewritten_webcompy-ui/,_webcompy-resource/, copied static files, materializedStaticRoutes, pre-rendered HTML pages, and optional PWA outputs).<prebuilt_dir>/prebuild.json(PrebuildManifest) recording asset hashes,resource_version, resolvedloading_template_html,prerenderdecisions, andssr_enabled.
At startup, create_prebuilt_asgi_app() from webcompy_server (without importing webcompy_cli, uvicorn, sse-starlette, or aiofiles) validates prebuild.json in O(1) time without writing .webcompy_modules/, touching webcompy-lock.json, or hashing assets:
| Target environment | Prebuild command | Runtime entrypoint |
|---|---|---|
Uvicorn / Docker container (public/ served by Python) |
python -m webcompy prebuild --out .webcompy-build |
python -m webcompy start --prebuilt .webcompy-build or create_prebuilt_asgi_app(app, ".webcompy-build") |
| CPython FaaS (AWS Lambda, Cloud Run) | python -m webcompy prebuild --out build --prerender static |
create_prebuilt_asgi_app(app) with WEBCOMPY_PREBUILT_DIR=build |
Edge CDN + Workers Python / zero-filesystem (Emscripten) |
python -m webcompy prebuild --prerender all --emit-python prebuilt_manifest.py |
Deploy public/ to Static Assets CDN and pass create_prebuilt_asgi_app(app, PREBUILD, serve_assets=False) |
When serve_assets=False and ssr_enabled=False, create_prebuilt_asgi_app() omits asset routes and the HTML catch-all completely, keeping only your mounts, runtime static_routes overrides, and /_webcompy-rpc. Under Emscripten runtimes (IS_EMSCRIPTEN == True), webcompy_server avoids Starlette FileResponse threads and asyncio.to_thread() while keeping ENVIRONMENT == "other" so SSR executes safely without pyscript or POSIX threads.
Cache integrity and .webcompy_modules/#
WebComPy caches downloaded Pyodide locks, CDN wheels, WASM wheels, and PyScript/Pyodide runtime assets inside <app_package_path>/.webcompy_modules/, automatically ensuring a .gitignore file ignores its contents.
When runtime_serving="local" runs, verify_and_update_runtime_assets() hashes every runtime file (SHA-256) and verifies it against hashes previously recorded under runtime_assets in webcompy-lock.json (raising RuntimeDownloadError on mismatch before writing any output). WASM wheels and CDN_PURE_PYTHON wheels are likewise verified against pyodide_sha256 / sha256 from the lockfile during download.
Offline and firewall deployment checklist#
Follow this checklist when deploying inside corporate firewalls, air-gapped networks, or offline-capable PWAs:
- Enable standalone delivery: Set
standalone=Trueinwebcompy_config.py(or pass--standalonetostart/generate). Combine withwheel_mode="split"andPWAConfig(precache_runtime=True)when browsers also need offline restarts:
import my_app.app as app_module
from webcompy_cli.config import ManifestConfig, PWAConfig, WebComPyBuildConfig
config = WebComPyBuildConfig(
app_module,
standalone=True,
wheel_mode="split",
pwa=PWAConfig(
enabled=True,
manifest=ManifestConfig(name="Intranet Console"),
precache_runtime=True,
),
)
- Generate lock and warm cache on a connected build machine: Run
python -m webcompy lockfollowed bypython -m webcompy generateso.webcompy_modules/caches all runtime assets andwebcompy-lock.jsonrecordsruntime_assetsSHA-256 checksums. - Inspect generated
dist/HTML: Verify generated<script>and<link>tags point only to same-origin/_webcompy-assets/...,/_webcompy-app-package/..., and/_webcompy-ui/...paths (no external CDN URLs remain). - Test offline boot in headless Chromium: With
PWAConfig(enabled=True, precache_runtime=True), verify Service Worker registration and cached reload while disconnected (see Progressive Web App).
Next steps#
- See an isolated iframe loading NumPy and Matplotlib WASM wheels on demand in the Matplotlib example.
- Pair self-hosted serving with precaching and runtime cache rules in Progressive Web App.
- Configure
standalone,wheel_mode, andlockfile_sync_configin Project Configuration. - Override serving flags per run from the CLI in CLI Reference.
- Reduce initial page payload and navigation requests with
resource_transfer="all-text"in Hydration & Data Transfer. - Full API: Server, CLI, RPC & Ports API.