Project Configuration
WebComPy separates browser-visible runtime configuration (WebComPyAppConfig on WebComPyApp), dependency declarations (pyproject.toml), and server/build configuration (WebComPyBuildConfig in webcompy_config.py). Keeping build concerns out of the webcompy core package ensures server paths, mount callables, and lockfile settings never ship into the browser wheel.
Use this page when configuring how dependencies resolve, which package files are exposed as resources, and how webcompy-lock.json stays synchronized with your project manifest.
The two-file configuration model#
A standard WebComPy project splits configuration across two Python/TOML files plus the application object:
| File / object | Owns | Included in browser wheel? |
|---|---|---|
pyproject.toml |
Package metadata, [project.dependencies], optional browser dependency group (e.g. browser = [...]) |
No |
webcompy_config.py (WebComPyBuildConfig, WebComPyServerConfig, PWAConfig, LockfileSyncConfig) |
Where app lives, dependency source, wheel & asset serving mode, resource globs, dist/, mounts, pwa |
No |
app/app.py (WebComPyAppConfig) |
base_url, mount selector, hydrate, scroll_restoration, loading, theme, plugins, scripts, compression_threshold, on_error, profile |
Yes |
Config discovery rules#
The CLI calls discover_config(module_path=None) from webcompy_cli:
- When
--configis omitted, WebComPy imports the top-level modulewebcompy_config. - When
--config my_project.webcompy_configis passed, WebComPy imports that dotted module path. - The imported module must export a top-level variable named
configholding aWebComPyBuildConfiginstance; otherwiseWebComPyCliExceptionis raised.
Here is a complete webcompy_config.py combining browser dependency discovery, lockfile sync, resource filtering, and a custom server port:
import my_app.app as app_module
from webcompy_cli.config import (
LockfileSyncConfig,
PWAConfig,
WebComPyBuildConfig,
WebComPyServerConfig,
)
config = WebComPyBuildConfig(
app_module,
app_var="app",
dependencies=None,
dependencies_from="browser",
resource_exclude=["**/drafts/**"],
resource_transfer="used",
dist="dist",
static_files_dir="static",
lockfile_sync_config=LockfileSyncConfig(sync_group="browser"),
server=WebComPyServerConfig(port=8080),
pwa=PWAConfig(enabled=False),
)
WebComPyBuildConfig field reference#
All fields live on WebComPyBuildConfig (webcompy_cli.config):
| Field | Default | Purpose |
|---|---|---|
app_module |
required | Imported Python module containing your WebComPyApp instance |
app_var |
"app" |
Attribute name of the WebComPyApp inside app_module |
dependencies |
None |
Explicit list of browser package names; when None, resolved from pyproject.toml |
dependencies_from |
None |
Optional group name under [project.optional-dependencies] in pyproject.toml; when None, reads [project.dependencies] |
resources |
None |
Glob patterns for package resource allow-list; None uses default extensions, [] disables resources |
resource_exclude |
None |
Glob patterns excluding matched paths from the resource allow-list |
version |
None |
Application version embedded into generated wheels and service workers (None uses "0.0.0") |
serve_all_deps |
True |
Bundle all pure-Python browser dependencies (including CDN-available ones) into the app wheel |
wasm_serving |
None |
"cdn" or "local" for WASM wheels; defaults via standalone when None |
runtime_serving |
None |
"cdn" or "local" for PyScript/Pyodide runtime files; defaults via standalone when None |
standalone |
False |
Serve runtime, WASM packages, and pure-Python dependencies locally without external CDNs |
wheel_mode |
"bundled" |
"bundled" (single combined wheel) or "split" (separate framework and app wheels) |
resource_transfer |
"used" |
"used" embeds only per-route loaded resources; "all-text" embeds all allow-listed text resources during pre-rendering |
prebuild_dir |
".webcompy-build" |
Default output directory for webcompy prebuild and webcompy start --prebuilt |
prerender |
"none" |
Build-time prerender depth policy for webcompy prebuild: "none", "static", or "all" |
dist |
"dist" |
Output directory path for webcompy generate (resolved relative to app_package_path unless --dist overrides) |
static_files_dir |
"static" |
Static asset directory relative to app_package_path |
lockfile_sync_config |
None |
Optional LockfileSyncConfig governing requirements.txt and pyproject.toml sync |
server |
WebComPyServerConfig() |
Serving options (port, dev, mounts) |
pwa |
PWAConfig() |
Progressive Web App manifest, service worker, and runtime caching rules (see Progressive Web App) |
static_routes |
None |
Zero-argument callable returning virtual StaticRoute (exported from webcompy_server and webcompy_cli.config) endpoints served at exact paths and materialized during build/export |
In __post_init__, WebComPyBuildConfig validates resource_transfer ("used" or "all-text") and prerender ("none", "static", or "all"), computes app_package_path = Path(app_module.__file__).parent, resolves app = getattr(app_module, app_var), and calls resolve_standalone() so CLI overrides re-evaluate cleanly.
WebComPyServerConfig and LockfileSyncConfig#
WebComPyServerConfig#
| Field | Default | Purpose |
|---|---|---|
port |
8080 |
TCP port used by python -m webcompy start when --port is omitted |
dev |
False |
Enables /_webcompy_reload SSE endpoint and uvicorn reload (overridden by --dev) |
mounts |
None |
Zero-argument callable returning dict[str, ASGIApp] mapping URL prefixes to mounted ASGI sub-apps |
See ASGI Integration for mount prefix rules and SSR/SSG behavior.
LockfileSyncConfig#
| Field | Default | Purpose |
|---|---|---|
requirements_path |
None |
Path (absolute or relative to app_package_path) to requirements.txt; None discovers requirements.txt beside pyproject.toml |
sync_group |
None |
Optional group under [project.optional-dependencies] checked by webcompy lock --sync; aligns with dependencies_from |
Setting dependencies_from="browser" and LockfileSyncConfig(sync_group="browser") keeps generation and --sync checks targeting the exact same pyproject.toml dependency list; mismatches between the two print a warning to stderr.
Runtime vs build configuration boundary#
Settings that affect HTML/DOM hydration or browser behavior live on WebComPyAppConfig (webcompy):
base_url(defaults to"/", normalized to leading + trailing slash)selector("#webcompy-app")hydrate(True) andcompression_threshold(1024,Noneor0disables gzip payload compression)scroll_restoration(True)theme,loading,plugins,scripts,on_error,profile
Because WebComPyAppConfig travels into the browser wheel while WebComPyBuildConfig never does, host secrets, server mount callbacks, and build paths remain strictly server-side.
Resource allow-list and static files#
WebComPy distinguishes verbatim static assets, virtual static routes, and application resources:
- Static files (
static_files_dir): Served directly at/<relative_path>(and{base_url}<relative_path>whenbase_urlis non-root) and copied unchanged intodist/. Put stylesheets, images, favicons, androbots.txthere. - Virtual static routes (
static_routes+StaticRoute): Programmatic endpoints generated on demand increate_asgi_app(), materialized inwebcompy generate(dist/<path>) andwebcompy prebuild(<prebuild_dir>/public/<path>, recorded asPrebuildStaticRouteMetainprebuild.json), or overridden dynamically increate_prebuilt_asgi_app():- Configure
static_routesas a zero-argument callable returning a sequence ofStaticRoute(path, handler, media_type=None, cache_control=None)entries (handlermay be sync or async and returnstrorbytes). - Registered as exact ASGI routes (never prefix mounts) before HTML fallback routes, returning
200 OKwithout trailing-slash redirects and inferring UTF-8text/*media types whenmedia_typeisNone. - Paths are validated at startup/build: empty/root paths,
..traversal,{/}braces, trailing slashes,/_webcompy*reserved prefixes,index.html/404.html, duplicates, directory-prefix overlaps, and collisions with page routes, PWA outputs,WebComPyServerConfig.mountsprefixes, orstatic_files_dirfiles fail fast withWebComPyException. - In
devmode, handlers are evaluated per request (Cache-Control: no-cache, must-revalidateunless overridden); inprodmode, responses cache the rendered bytes per route (Cache-Control: public, max-age=0, must-revalidate) and synchronous handlers run off the event loop;webcompy generatewrites each response body todist/<path>andwebcompy prebuildwrites it to<prebuild_dir>/public/<path>before PWA precache enumeration.
- Configure
- Package resources (
resources+resource_exclude): Walked insideapp_package_pathat startup/build to construct an explicit allow-list forServerResourcePort(load_text/load_bytes) and/_webcompy-resource/{path}:- When
resources=None, the default allow-list matches**/*.html,**/*.css,**/*.md,**/*.svg, and**/*.txt(matching both top-level files and nested subdirectories). - Setting
resources=[]disables resource scanning and yields an empty allow-list. - Directories named
__pycache__,.git,.webcompy_modules,.webcompy-build,.staging-*, plus.pycand.tmpfiles (and the configuredprebuild_dir/distdirectories) are always excluded from both resource scanning and wheel packaging. - Requests for paths outside the allow-list receive HTTP
404, and traversal outsideapp_package_pathis rejected with403. - Setting
resource_transfer="all-text"pre-embeds allow-listed text files (.md,.markdown,.txt,.json,.csv,.yaml,.yml,.toml,.svg,.html,.xml) into every SSG HTML payload so route transitions avoid extra network requests (emitting a warning if any single text file exceeds 256 KiB or the combined set exceeds 1 MiB).
- When
Lockfile lifecycle (webcompy-lock.json)#
Running python -m webcompy lock (or letting start/generate initialize the build) writes webcompy-lock.json inside app_package_path using lockfile schema version 2:
{
"version": 2,
"pyodide_version": "314.0.3",
"pyscript_version": "2026.7.3",
"wasm_serving": "cdn",
"runtime_serving": "cdn",
"standalone": false,
"wasm_packages": {},
"pure_python_packages": {}
}
Key lifecycle guarantees:
- Automatic staleness detection: At build time, WebComPy verifies that
version == 2, that the union ofexplicitpackages inwasm_packagesandpure_python_packagesmatches resolveddependencies, and thatpyscript_versionandpyodide_versionmatch the active runtime pin. Version 1 lockfiles or mismatched dependency/runtime pins trigger automatic regeneration. - Environment validation: Non-CDN pure-Python packages missing locally or matching a different installed version fail the build with exact
pip install <pkg>==<ver>remediation instructions. CDN pure-Python or WASM version differences emitstderrwarnings noting the local version runs during SSR/SSG while the locked version runs in the browser. - Version control and cache files: Commit
webcompy-lock.jsonalongside your code for reproducible builds. Build caches live separately in<app_package_path>/.webcompy_modules/, where WebComPy automatically writes a.gitignorecontaining*.
Next steps#
- Review the directory layout created by
webcompy initin Project Structure. - Inspect all flags for
lock,start, andgeneratein CLI Reference. - Configure CDN vs offline asset delivery in Serving Modes.
- Tune resource transfer and payload compression in Hydration & Data Transfer.
- Add manifest and offline worker settings via
PWAConfigin Progressive Web App. - Full API: Server, CLI, RPC & Ports API and App & Components API.