Skip to content

cointoss.notebook

notebook

JupyterLab as a woodglue UI at /ui/notebook/, for ad hoc analysis over the Dataset API.

woodglue has no Jupyter kind, so the UI is a command: entry (woodglue.config.UiEntry) that runs this module with this environment's Python; woodglue fills in {port} and {base_path} and proxies HTTP and websockets to it. cointoss init prints the entry, ui_entry, rather than adding it: a kernel runs any code as the service user, so it is opt-in, for dev, and for the admin only. No Role but the built-in admin should hold view ui:notebook.

The launcher:

  • takes the data directory from --data-dir, else the working directory, which woodglue sets to its own data directory for a UI process, and exports it as $COINTOSS_HOME, so a kernel's cointoss.client finds this instance and its admin token;
  • keeps notebooks in <data dir>/notebooks/, copying the starter notebook there when it is missing, and never over an edited one;
  • then becomes JupyterLab (exec), so woodglue's SIGTERM reaches it directly.

JupyterLab listens on 127.0.0.1 only, with its token and password off, since woodglue has authenticated the viewer before proxying. allow_remote_access is on because the proxy keeps the browser's Host, which is not a loopback name when woodglue is reached over the network; the listening address alone keeps anyone else out.

Needs the notebook extra (JupyterLab and pandas).

jupyterlab_installed()

Whether this environment, the one woodglue runs the launcher with, has JupyterLab.

Source code in src/cointoss/notebook.py
def jupyterlab_installed() -> bool:
    """Whether this environment, the one woodglue runs the launcher with, has JupyterLab."""
    return importlib.util.find_spec("jupyterlab") is not None

ui_entry(python=None)

The uis.notebook entry running this install's launcher under python.

python defaults to this environment's, named by absolute path since a systemd unit's PATH does not hold the virtualenv. The Delegated Token woodglue sends JupyterLab is limited to calling the cointoss namespace, though no kernel can read it.

>>> ui_entry(Path("/venv/bin/python"))["command"][:3]
['/venv/bin/python', '-m', 'cointoss.notebook']
Source code in src/cointoss/notebook.py
def ui_entry(python: Path | None = None) -> dict[str, Any]:
    """The `uis.notebook` entry running this install's launcher under `python`.

    `python` defaults to this environment's, named by absolute path since a systemd unit's
    `PATH` does not hold the virtualenv. The Delegated Token woodglue sends JupyterLab is
    limited to calling the cointoss namespace, though no kernel can read it.

    >>> ui_entry(Path("/venv/bin/python"))["command"][:3]
    ['/venv/bin/python', '-m', 'cointoss.notebook']
    """
    return {
        "command": [
            str(python or Path(sys.executable)),
            "-m",
            __name__,
            "--port={port}",
            "--base-path={base_path}",
        ],
        "label": "Notebook",
        "grants": [f"call {NAMESPACE}"],
    }

jupyter_args(root, port, base_path)

JupyterLab's arguments: loopback only, no token or password, served under base_path.

>>> args = jupyter_args(Path("/d/notebooks"), 8601, "/ui/notebook")
>>> [a for a in args if "port" in a or "base_url" in a or "ip=" in a]
['--ServerApp.ip=127.0.0.1', '--ServerApp.port=8601', '--ServerApp.port_retries=0', '--ServerApp.base_url=/ui/notebook/']
Source code in src/cointoss/notebook.py
def jupyter_args(root: Path, port: int, base_path: str) -> list[str]:
    """JupyterLab's arguments: loopback only, no token or password, served under `base_path`.

    >>> args = jupyter_args(Path("/d/notebooks"), 8601, "/ui/notebook")
    >>> [a for a in args if "port" in a or "base_url" in a or "ip=" in a]
    ['--ServerApp.ip=127.0.0.1', '--ServerApp.port=8601', '--ServerApp.port_retries=0', '--ServerApp.base_url=/ui/notebook/']
    """
    return [
        "--no-browser",
        "--ServerApp.ip=127.0.0.1",
        f"--ServerApp.port={port}",
        "--ServerApp.port_retries=0",
        f"--ServerApp.base_url={base_path.rstrip('/')}/",
        f"--ServerApp.root_dir={root}",
        "--IdentityProvider.token=",
        "--PasswordIdentityProvider.hashed_password=",
        "--ServerApp.allow_remote_access=True",
    ]

prepare(data_dir)

Create <data_dir>/notebooks/ with the starter notebook in it if missing; return the dir.

Source code in src/cointoss/notebook.py
def prepare(data_dir: Path) -> Path:
    """Create `<data_dir>/notebooks/` with the starter notebook in it if missing; return the dir."""
    root = data_dir / NOTEBOOKS_DIR
    root.mkdir(parents=True, exist_ok=True)
    starter = root / STARTER
    if not starter.exists():
        with as_file(files("cointoss").joinpath(STARTER)) as shipped:
            shutil.copyfile(shipped, starter)
    return root