Skip to main content

Make kernel-local HTTP/WS servers reachable from the notebook browser with zero user config.

Project description

jupyter-loopback

Make kernel-local HTTP and WebSocket servers reachable from the notebook browser. Zero user config.

If your library runs a web server inside a Jupyter kernel (a tile server, a trame app, Bokeh, Dash, a custom debug UI, anything bound to 127.0.0.1:<port>), your users hit the same wall every time. Works locally. Breaks on JupyterHub, MyBinder, VS Code Remote, Colab, Shiny. The usual fix is a README paragraph telling users to install jupyter-server-proxy, export LIBRARY_CLIENT_PREFIX='proxy/{port}', and prepend $JUPYTERHUB_SERVICE_PREFIX on Hub. Most users skip it, get a broken notebook, and file an issue.

jupyter-loopback replaces that paragraph. Libraries register once. End users configure nothing.

Who this is for

Library authors who spin up an HTTP or WebSocket server inside a Jupyter kernel and need the browser to reach it without asking every user to configure a proxy. If you've ever written "make sure to install jupyter-server-proxy and set FOO_CLIENT_PREFIX='proxy/{port}'" in your README, this is for you.

Install

pip install jupyter-loopback            # HTTP/WS proxy
pip install jupyter-loopback[comm]      # + anywidget comm bridge fallback

The 30-second demo

docker build -t jupyter-loopback-demo .
docker run --rm -it -p 8888:8888 jupyter-loopback-demo

Open the printed token URL, run example.ipynb. You'll see:

  • A JSON response fetched through the proxy.
  • A red square rendered inline (binary-body correctness check).
  • A live WebSocket echo box you can type into.

All of it flowing through <base_url>/loopback-demo-proxy/<port>/… with no config.

For library authors

Suppose your library is mylib and it spins up a server at 127.0.0.1:<port> inside the kernel. Three files wire it up.

1. Server-side: register the proxy

# mylib/_jupyter/__init__.py
from jupyter_loopback import setup_proxy_handler

def _jupyter_server_extension_points():
    return [{"module": "mylib._jupyter"}]

def _load_jupyter_server_extension(server_app):
    setup_proxy_handler(server_app.web_app, namespace="mylib")

2. Auto-enable the extension

Ship jupyter-config/jupyter_server_config.d/mylib.json:

{
  "ServerApp": {
    "jpserver_extensions": {
      "mylib._jupyter": true
    }
  }
}

And wire it into pyproject.toml:

[tool.setuptools.data-files]
"etc/jupyter/jupyter_server_config.d" = [
  "jupyter-config/jupyter_server_config.d/mylib.json",
]

3. Kernel-side: build browser-reachable URLs

from jupyter_loopback import autodetect_prefix

def browser_url(port: int, path: str) -> str:
    prefix = autodetect_prefix("mylib")  # None outside Jupyter
    if prefix is None:
        return f"http://127.0.0.1:{port}/{path.lstrip('/')}"
    return f"{prefix.format(port=port)}/{path.lstrip('/')}"

That's it. In a local Python REPL, autodetect_prefix returns None and you hit 127.0.0.1 directly. In JupyterLab, Hub, Binder, or any jupyter-server environment, it returns mylib-proxy/{port} (with any per-user Hub prefix already attached) and the browser loads through the proxy.

For users on VS Code Remote, Colab, Shiny, Solara, marimo

These frontends don't run a jupyter-server, so the HTTP proxy above isn't available. They do have kernel comms (the WebSocket the notebook widgets use). jupyter-loopback ships an anywidget that tunnels request/response pairs over that comm channel.

Users enable it once at the top of a notebook:

import jupyter_loopback
jupyter_loopback.enable_comm_bridge()

Library authors register handlers:

from jupyter_loopback import on_request

@on_request("mylib", "get_tile")
def _(data, buffers):
    z, x, y = data["z"], data["x"], data["y"]
    return {"ok": True}, [make_tile(z, x, y)]   # (json, buffers)

Frontend code calls through window.__jupyter_loopback__:

const { status, data, buffers } = await window.__jupyter_loopback__.request(
  "mylib",
  "get_tile",
  { z: 8, x: 71, y: 110 },
);
if (status === "ok") {
  const blob = new Blob([buffers[0]], { type: "image/png" });
  imgElement.src = URL.createObjectURL(blob);
}

The bridge carries JSON plus binary buffers. Use it for anything request/response shaped. Streaming and server-push are out of scope; use the WS proxy for that.

What works where

Environment Path User does
Local notebook direct loopback nothing
JupyterLab / Notebook 7+ HTTP/WS proxy nothing
JupyterHub / MyBinder HTTP/WS proxy nothing
VS Code Remote comm bridge enable_comm_bridge()
Google Colab comm bridge enable_comm_bridge()
Shiny for Python / Solara comm bridge enable_comm_bridge()
marimo comm bridge enable_comm_bridge()

Relationship to jupyter-server-proxy

jupyter-server-proxy proxies arbitrary subprocesses. It handles HTTP/WS wire formatting, subprocess lifecycle, URL rewriting, auth.

jupyter-loopback does less, on purpose:

  • Proxies loopback only. No cross-host surface.
  • No subprocess management. You bring your own server on any port.
  • Autodetects the URL prefix from Jupyter's own env vars.
  • Ships a comm-based fallback for frontends without a jupyter-server.

The two can coexist. Set LIBRARY_CLIENT_PREFIX explicitly and jupyter-loopback's autodetect steps out of the way.

Status

Extracted from localtileserver after the same pattern solved its long tail of remote-Jupyter issues. Generalized so other libraries can adopt it without reinventing the wheel.

See ARCHITECTURE.md for the design walkthrough. MIT licensed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jupyter_loopback-0.3.2.tar.gz (205.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jupyter_loopback-0.3.2-py3-none-any.whl (36.2 kB view details)

Uploaded Python 3

File details

Details for the file jupyter_loopback-0.3.2.tar.gz.

File metadata

  • Download URL: jupyter_loopback-0.3.2.tar.gz
  • Upload date:
  • Size: 205.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for jupyter_loopback-0.3.2.tar.gz
Algorithm Hash digest
SHA256 db7108b382d30f1434d66f141dce4a98ca13b891cc25ebcb0aa1101919c7d1c0
MD5 d8a7c5262d117ebcbfbdf3c18fa02c88
BLAKE2b-256 036543b786dbdd8927d04fe7f8c14c57bae26794265507227823900ebec9e04d

See more details on using hashes here.

Provenance

The following attestation bundles were made for jupyter_loopback-0.3.2.tar.gz:

Publisher: release.yml on banesullivan/jupyter-loopback

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file jupyter_loopback-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for jupyter_loopback-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 38616da73f3c6bff2eff23cbd49cbcb408a498aa11d327b76b2eb3e112bb6f81
MD5 8db8118f97507e4604813b2ea2ad01c9
BLAKE2b-256 c332167f52961395583637f04fce75ab2e71f85bf6ce44847b954fcc131ce9c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for jupyter_loopback-0.3.2-py3-none-any.whl:

Publisher: release.yml on banesullivan/jupyter-loopback

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page