Skip to main content

Observable JavaScript notebooks as reusable Python widgets

Project description

pyobservablejs

CI PyPI Python versions License

pyobservablejs embeds reactive Observable notebooks in Python. Author cells or load an existing notebook, render all or selected cells, synchronize Python values, read structured browser results, and inspect the dependency graph.

An authored notebook with an interactive filter and plot

It works in JupyterLab, marimo, VS Code notebooks, Google Colab, and other environments that support anywidget.

Install or try

Add pyobservablejs to an existing Python 3.11 or newer environment:

uv pip install pyobservablejs

Or open a disposable marimo environment with the package available:

uvx --with pyobservablejs marimo edit notebook.py

This command installs marimo and pyobservablejs in the temporary environment.

Quickstart

Paste this into the marimo editor opened above:

import observablejs as obs

control = obs.ojs(
    """
    viewof threshold = Inputs.range([0, 1], {
      value: 0.5,
      step: 0.1,
      label: "Threshold"
    })
    """,
    key="threshold",
)

result = obs.js(
    "html`<strong>Threshold: ${threshold}</strong>`",
    key="result",
)

notebook = obs.Notebook(
    control,
    result,
    variables={"threshold": 0.5},
)

notebook.view()

Each cell key is its portable Python identity. Render one result with notebook.view("result").

Capabilities

Task API
Author JavaScript, Observable JavaScript, Markdown, and HTML cells obs.js, obs.ojs, obs.md, obs.html
Load Notebook Kit HTML or an ObservableHQ notebook Notebook.from_html, Notebook.from_observablehq
Render a whole notebook or selected keyed cells notebook.view() and notebook.view("chart")
Update Python-owned variables notebook.update_variables({"threshold": 0.8})
Read pending, successful, and failed browser results view.state and view.state.result("chart")
Inspect dependencies or export a diagram view.state.graph, to_mermaid(), and to_d2()

Views from one Notebook share controller variables and supported browser inputs. Each view keeps its own result state plus input and settled revision metadata.

Use notebook.view(capture_state=False) when the rendered output is all you need and Python will not read view.state. The view remains interactive and avoids sending result snapshots to Python. See Display views.

Python and browser values updating one notebook

Import notebooks

Notebook cells execute as JavaScript in the host page. Treat imported Notebook Kit HTML, ObservableHQ notebooks, and remote modules as trusted code. See Browser execution.

import observablejs as obs

notebook = obs.Notebook.from_observablehq("@d3/world-tour")
notebook.view()

An imported ObservableHQ notebook

Documentation

Read the documentation for the quickstart, task guides, recipes, troubleshooting, and API reference.

Acknowledgements

Thanks to the Observable team for Notebook Kit, which provides the notebook format and runtime. pyobsplot informed the Python variable API. Trevor Manz's anywidget composition demo shaped the shared-session design.

License

MIT

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

pyobservablejs-0.0.9.tar.gz (1.5 MB view details)

Uploaded Source

Built Distribution

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

pyobservablejs-0.0.9-py3-none-any.whl (1.5 MB view details)

Uploaded Python 3

File details

Details for the file pyobservablejs-0.0.9.tar.gz.

File metadata

  • Download URL: pyobservablejs-0.0.9.tar.gz
  • Upload date:
  • Size: 1.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyobservablejs-0.0.9.tar.gz
Algorithm Hash digest
SHA256 cd99acf5c7375a9899c49096076606d5846a7b48fd50aaf2b554e62e8d36216e
MD5 3efb50ebe2b179f586c6585da5cd45ca
BLAKE2b-256 bf61fb72dc728c972d01662305bd2e328765c424493dc7a16a73f67e55418f76

See more details on using hashes here.

File details

Details for the file pyobservablejs-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: pyobservablejs-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyobservablejs-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 f6d54287ed1c3f6effb8385d0871380af82b97206e5aceae4ba646c3e8de562d
MD5 4b6ee62219d979ec4cb68ca9270372b4
BLAKE2b-256 f87e96f06480e67df8d1357ec073c4f68029034faa8c071bd878d86db36fe4db

See more details on using hashes here.

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