Skip to main content

A package for JavaScript-Python communication in Jupyter notebooks

Project description

jupyter-jscomm

A Python package to work around JavaScript-Python communication difficulties in Jupyter notebooks. Basic strategy is to use a hidden widget holding a json payload with the variables you want to keep.

This code can for instance be used as a hack to copy the content of bokeh JS variables into these JS-Python accessible variables.

Installation

pip install jupyter-jscomm

Usage from a Jupyter notebook

from jupyter_jscomm import JSComm

# Initialize the communication
comm = JSComm()

# Or initialize with custom class name to avoid collisions
comm1 = JSComm(class_name="my-comm-1")
comm2 = JSComm(class_name="my-comm-2")

# Each instance maintains its own state
comm1.add("var1", "Value for comm1")
comm2.add("var1", "Value for comm2")

# Add data from Python
comm1.add("var1", "Hello from Python")
comm2.add("var2", {"nested": "data"})

# Get data from JavaScript
value = comm1.get("var1")

# Update existing data
comm1.update("var1", "Updated value")

# Remove data
comm1.remove("var1")

# Clear all data
comm1.clear_all()

Change JS variable from JS context

While this package ensures the creation of a "magic" JS variable (default class name "comm-class") where you can write from Python to JS and get the value back from JS to Python, one of the goal is to map an already existing variable to this "magic" JS variable

// var my_existing_variable already contains the value I want to retrieve and expose in Python

// For default class name
var parent = document.getElementsByClassName("comm-class")[0];

// For custom class name
var parent = document.getElementsByClassName("my-comm-1")[0];

var input = parent.querySelector(".widget-input");
input.value = '{"var1": ' + JSON.stringify(my_existing_variable + '}';
input.dispatchEvent(new Event('input', { bubbles: true }));

License

This project is licensed under the MIT License - see the LICENSE file for details.

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_jscomm-0.1.3.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

jupyter_jscomm-0.1.3-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file jupyter_jscomm-0.1.3.tar.gz.

File metadata

  • Download URL: jupyter_jscomm-0.1.3.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for jupyter_jscomm-0.1.3.tar.gz
Algorithm Hash digest
SHA256 fb313ddcf848402ed7ce42c8e470acb2dfed9034b2c7ac9bcf4c4292c50e6ef7
MD5 6791d382a9c262e55b350ae4e64308fa
BLAKE2b-256 7f6c7cab8f3fb680c3b408c2dfbb9fbc85c9e124a0e5dc9684cf29e49c1663ff

See more details on using hashes here.

File details

Details for the file jupyter_jscomm-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: jupyter_jscomm-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for jupyter_jscomm-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ac6243f30cc9146f6dc73d20c93329c50191007dd551f5b44203f1fafae1bc1f
MD5 0dfa2e4b438b2831e077583f18183a0a
BLAKE2b-256 e954ab7d732b9e392e3f78e57217d8a2bc7935ea818150bc12651b6c9f0a3e8a

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