Skip to main content

ui.serve

ui.serve(root, port=None, open_browser=True, *, ready=None)

Serve the interface of the project at root until the process is stopped.

The server binds 127.0.0.1 only. A port that is taken is an error; no other port is chosen.

Parameters

Name Type Description Default
root Path The project root, inside a git repository. required
port int | None The port; None takes the one in machine.toml or the default. Port 0 lets the system choose, which the tests use. None
open_browser bool Whether to open the browser with the launch file. If not, the address with the launch token is printed. True
ready Callable[[UiServer], None] | None Called with the server after it is bound and before it serves; a test uses it to learn the port. None

A SIGTERM or Ctrl-C stops the server, and with it the long job that runs: its process group is terminated, then killed after a short grace, and the job is recorded as interrupted.

Raises

Name Type Description
ConfigurationError If root is not in a git repository, a configuration file does not load, or the port is not available.

Examples

import contextlib
import io
import subprocess
import threading
from pathlib import Path
from tempfile import TemporaryDirectory

from babregen import make_sample_repo
from babregen.ui import serve

with TemporaryDirectory() as tmp:
    root = make_sample_repo(Path(tmp) / "demo")
    subprocess.run(["git", "init", "-q"], cwd=root, check=True)

    def stop(server):
        print("serving", server.host.startswith("127.0.0.1:"))
        threading.Thread(target=server.shutdown).start()

    with contextlib.redirect_stdout(io.StringIO()):
        serve(root, 0, open_browser=False, ready=stop)
    print("stopped")
stopped