Skip to main content

A lightweight anywidget-based IPython widget that renders arbitrary JavaScript inside Jupyter notebooks

Project description

JSWidget

A lightweight anywidget-based IPython widget that renders arbitrary JavaScript (Canvas 2D, WebGL, etc.) inside Jupyter notebooks. It passes JSON data and binary buffers (mesh vertices, normals, indices) over the ipywidgets comm API, with live-update callbacks so Python can push new data without re-executing the cell.

Works in VS Code, JupyterLab, and classic Jupyter notebooks — no extra frontend build step required.

Installation

pip install jswidget

Or install from source in development mode:

git clone https://github.com/DannyRuijters/JSWidget.git
cd JSWidget
pip install -e .

Quick Start

from jswidget import JSWidget

w = JSWidget(width=600, height=400)
w.js_code = '''
    const canvas = document.createElement("canvas");
    canvas.width = opts.width;
    canvas.height = opts.height;
    el.appendChild(canvas);
    const ctx = canvas.getContext("2d");
    ctx.fillStyle = "#e94560";
    ctx.font = "24px sans-serif";
    ctx.fillText("Hello from JSWidget!", 20, 50);
'''
w.show()

JavaScript API

The JavaScript code passed to js_code receives the following variables:

Variable Description
el Container DOM element to render into
data JSON data dict set from Python via w.data
getBuffer(name) Returns a named binary buffer as an ArrayBuffer
opts {width, height} of the widget
setState(obj) Save state that persists across re-renders
getState() Retrieve previously saved state
onData(fn) Register a callback for live data updates from Python
onBuffers(fn) Register a callback for live buffer updates from Python

Passing Data

JSON data

w.data = {'values': [10, 40, 80], 'color': '#4ecdc4'}
# Update later (triggers onData callbacks in JS):
w.send_data({'values': [90, 20, 55], 'color': '#ff6b6b'})

Binary buffers

import numpy as np

vertices = np.array([[0,0,0],[1,0,0],[0,1,0]], dtype=np.float32)
normals  = np.array([[0,0,1],[0,0,1],[0,0,1]], dtype=np.float32)
indices  = np.array([0, 1, 2], dtype=np.uint32)

w.set_buffers(vertices=vertices, normals=normals, indices=indices)

Buffers are accessible in JavaScript via getBuffer('vertices'), etc., and return an ArrayBuffer that can be wrapped in a typed array (e.g. new Float32Array(getBuffer('vertices'))).

Demo Notebook

See JSWidget_demo.ipynb for full examples including:

  1. Canvas 2D drawing — gradient backgrounds and text rendering
  2. Bar chart with live updates — pass data from Python via send_data() and re-draw with onData()
  3. Interactive WebGL mesh viewer — sphere and torus rendering with mouse-drag rotation and scroll zoom, using binary buffers for mesh data

Files

File Description
jswidget.py The JSWidget class (anywidget-based DOMWidget with ESM frontend)
JSWidget_demo.ipynb Demo notebook with Canvas 2D, bar chart, and WebGL examples
LICENSE BSD 3-Clause License

License

BSD 3-Clause — see LICENSE.

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

jswidget-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

jswidget-0.1.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file jswidget-0.1.0.tar.gz.

File metadata

  • Download URL: jswidget-0.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for jswidget-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ca843fcf57d0c43582caa2e4e22b0572700f7722686c1eef946752e7f918973d
MD5 5fdef57d7aeecd24e4fede62c3f6c854
BLAKE2b-256 198a9fbe456f4c7911f56995c8896bf7637e02c28e06aa01e3aa83d427822156

See more details on using hashes here.

File details

Details for the file jswidget-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jswidget-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for jswidget-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5703263d10246c8bebab1fdc18c3558c4609d105bfa9903f535f599ee33c23bc
MD5 8a79fae4f38a11bea825b1741070bb05
BLAKE2b-256 c5518c371ec0ac5be9da2b7ada756df0c41cb7fc5cff5e42e74c4e59b6e632e8

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