Skip to main content

Observable JavaScript notebooks as reusable Python widgets

Project description

pyobservablejs

CI PyPI Python versions License

pyobservablejs renders Observable JavaScript notebooks in Jupyter and marimo.

For JupyterLab:

pip install pyobservablejs jupyterlab

Python 3.11 or newer is required.

Notebook Kit provides the Plot library and the penguins sample. The browser loads both from jsDelivr, so it needs network access and a content security policy that permits the CDN requests.

import observablejs as obs

notebook = obs.Notebook(
    obs.js(
        """
        Plot.dot(penguins, {
          x: "culmen_length_mm",
          y: "culmen_depth_mm",
          fill: "species",
          tip: true
        }).plot({
          height: 320,
          color: {legend: true},
          x: {grid: true, label: "Bill length (mm)"},
          y: {grid: true, label: "Bill depth (mm)"}
        })
        """
    )
)

full_view = notebook.view()
full_view

For marimo, install the package with the notebook frontend and wrap the view with mo.ui.anywidget.

pip install pyobservablejs marimo
import marimo as mo

mo.ui.anywidget(full_view)

Notebooks, views, and inputs

Notebook accepts JavaScript, Observable JavaScript, Markdown, and HTML cells.

Helper Cell source
obs.js(...) Standard Notebook Kit JavaScript
obs.ojs(...) Observable JavaScript
obs.md(...) Markdown
obs.html(...) HTML

Top-level JavaScript declarations form a reactive graph across cells. The Python methods create renderable notebook views. JavaScript view(input) displays a browser input and defines its reactive value:

Call Result
notebook.view() Creates a NotebookView that selects every cell.
notebook.view(cells=[...]) Creates a composite view for selected cells and hidden dependencies.
notebook_cell.view() Creates a focused view for one cell and its hidden dependencies.
view(input) inside obs.js Displays an input and defines its reactive value in the browser.

Create a separate Python view for each frontend display location. Views from one notebook receive the same Python variable updates. Named Observable JavaScript viewof inputs can share supported interacted values when their controls use the same input type. The variables reference lists the shared value shapes.

Pass Python values through variables.

notebook = obs.Notebook(
    obs.js('html`<p>Threshold: <strong>${threshold}</strong></p>`'),
    variables={"threshold": 0.75},
)

full_view = notebook.view()
full_view

After the view renders, update a Python-owned value from Python.

notebook.update_variables(threshold=0.9)

The rendered full_view exposes synchronized values and dependency metadata through runtime_values, value(name), and graph. Create a cell view with notebook.cell_at(0).view() or use the views guide to render several selected cells together.

See Views and composition for selection, mounting, shared variables and inputs, and readback.

Existing notebooks

Load a trusted Notebook Kit HTML file:

notebook = obs.Notebook.from_html_file(
    "chart.html",
    embed_file_attachments=True,
    rewrite_imports=True,
)

Load a trusted public ObservableHQ notebook:

notebook = obs.Notebook.from_observablehq("@d3/bar-chart")

Documentation

Contributing

See the contributor guide for local setup and the checks required before review.

Acknowledgements

pyobsplot informed the Python variable API. Thanks to Trevor Manz for the composable anywidgets demo that helped shape the widget 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.1.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.1-py3-none-any.whl (1.5 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyobservablejs-0.0.1.tar.gz
  • Upload date:
  • Size: 1.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","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.1.tar.gz
Algorithm Hash digest
SHA256 0fa224de25297a5404e66d3505af795693873e30c4a468d77b9c35a261b98b63
MD5 7b558e7df082831454c32416d9eb656f
BLAKE2b-256 1690415a3fc13816470549cd0611b1750be0f82d686447ddf6d40335e96bfcf2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyobservablejs-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5209bbd26e03fa34be3f648f27c4268804327da7cd032c8e92f7466a3c72e342
MD5 03507dcb218d1adb1586af050834052f
BLAKE2b-256 59f7c87e0384e52226e961b6c20b45f930dad10fef26a9527e9ea65fb9180bbe

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