Skip to content

Dev server (CLI)

wybthon.dev

dev

The wyb command: develop, build, and preview Wybthon projects.

A project is a directory with a wybthon.toml (create one with wyb init). wyb dev builds it in development mode, serves the build (rendering pages per request and serving server functions when the project sets server = true), and rebuilds when a source changes. Over Server-Sent Events, it replaces an edited application module in place in connected pages (see wybthon._hmr) and reloads them for any other change. wyb build writes a production build; wyb preview serves a static one and wyb serve a server one.

Use the main function for CLI entry, or call serve directly to embed the server.

Functions:

Name Description
serve

Build a project in development mode, serve it, and rebuild on change.

main

CLI entry point for wyb.

SSEHandler

Bases: SimpleHTTPRequestHandler

Serves a development build and a /__sse endpoint for reload events.

Class attributes

watchers: wfile objects of connected SSE clients. root: The build output directory. app_base: The application's base path; other paths return 404, and unknown paths under it serve the client shell.

Methods:

Name Description
end_headers

Append no-cache headers to every response to avoid stale assets.

do_GET

Serve the SSE stream or a file from the build.

translate_path

Map a request under the app base to the build, falling back to the client shell.

notify_reload

Send a reload SSE event to every connected client.

notify_hot

Send an hmr SSE event: the changed sources and the modules to replace in place.

end_headers

end_headers() -> None

Append no-cache headers to every response to avoid stale assets.

do_GET

do_GET() -> None

Serve the SSE stream or a file from the build.

translate_path

translate_path(path: str) -> str

Map a request under the app base to the build, falling back to the client shell.

notify_reload classmethod

notify_reload() -> None

Send a reload SSE event to every connected client.

notify_hot classmethod

notify_hot(files: list[dict[str, str]], modules: list[str]) -> None

Send an hmr SSE event: the changed sources and the modules to replace in place.

serve

serve(directory: str | Path = '.', host: str = '127.0.0.1', port: int = 8000, open_browser: bool = False, open_path: str | None = None) -> None

Build a project in development mode, serve it, and rebuild on change.

Parameters:

Name Type Description Default
directory str | Path

The project directory (containing wybthon.toml).

'.'
host str

Bind host.

'127.0.0.1'
port int

Preferred port. If busy, the server tries the next 20 ports before failing.

8000
open_browser bool

When True, open a browser tab to the app after binding.

False
open_path str | None

Path to open instead of the app's base path.

None

Raises:

Type Description
ValueError

directory isn't a Wybthon project.

run_script

run_script(path: str, argv: list[str]) -> int

Run path as __main__ with its templates (and its imports') compiled.

The script's own html(t"...") calls are compiled before it runs, and the import hook compiles every module it imports, so a script behaves exactly as it would inside an application.

main

main(argv: list[str] | None = None) -> int

CLI entry point for wyb.

Parameters:

Name Type Description Default
argv list[str] | None

Optional argument list (defaults to sys.argv[1:]).

None

Returns:

Type Description
int

Process exit code: 0 on success, non-zero on usage errors.

What's in this module

dev is the wyb command. It works on projects: a directory with a wybthon.toml, created by wyb init. wyb dev builds the project in development mode, serves the build, rebuilds when a source file changes, and pushes a reload event over Server-Sent Events at /__sse so open pages refresh. wyb build writes a production build, which turns dev mode off, and wyb preview serves one. Both wyb dev and wyb build run every application module through the compiler, so a malformed template fails the build with its file and line, and they ship the compiled bytecode (when the build's Python matches the Pyodide runtime's; see Deployment), so the page never parses a template at run time. wyb run runs a single script with its templates compiled. Every built page carries its build manifest inline (<script type="application/json" id="wyb-manifest">) and preloads what boot needs: a <link rel="modulepreload"> for Pyodide's pyodide.mjs, and <link rel="preload" as="fetch" crossorigin> for the runtime and application archives, so they download in parallel with Pyodide.

Name Description
main CLI entry point (wyb init, wyb dev, wyb build, wyb preview, wyb run, wyb check).
serve serve(directory=".", host="127.0.0.1", port=8000, open_browser=False, open_path=None): the wyb dev server, callable from Python.
run_script run_script(path, argv): what wyb run does, callable from Python.
wyb init my-app
wyb dev --dir my-app --port 8000 --open
wyb build --dir my-app --base /app/
wyb preview --dir my-app/dist
wyb run scripts/seed.py --verbose
wyb check app --checker pyright
Command Flag Default Description
wyb dev --dir . Project directory (containing wybthon.toml).
wyb dev --host 127.0.0.1 Host interface to bind.
wyb dev --port 8000 Starting port (tries the next 20 on conflict).
wyb dev --open off Open a browser at the app's base path.
wyb dev --open-path none Path to open instead of the base path.
wyb build --dir . Project directory.
wyb build --out <dir>/dist Output directory.
wyb build --base from wybthon.toml Base URL path the app is served from.
wyb preview --dir, --host, --port dist, 127.0.0.1, 8000 Serve a production build.
wyb run script [args ...] none Run a Python script as __main__ with its templates compiled; the arguments after it are passed to the script.
wyb check paths ... . Files and directories to type-check, including component tag props (see Typing).
wyb check --checker mypy mypy or pyright.

Live reload: wyb dev watches the app directory, public/, index.html, and wybthon.toml, rebuilds on any change, and broadcasts reload; the snippet it injects into each served page reloads it. The static-directory mode of earlier versions, with its /__manifest endpoint and --mount and --watch flags, is gone; every served directory is a project. See the dev server guide.

wyb run script.py is for code outside a project: a script that renders components with wybthon.testing or wybthon.server, a benchmark, or a quick experiment. python script.py compiles the __main__ module before Wybthon is imported, so its templates aren't compiled, and dev mode raises TemplateError when a dynamic expression in one of them was evaluated once. wyb run compiles the script itself, installs the import hook for the modules it imports, puts the script's directory first on sys.path, and sets sys.argv, so the script behaves as it would inside an application.

wyb run render_page.py --out page.html  # sys.argv[1:] == ["--out", "page.html"]

See also