custom jupyter widgets made easy
- 🛠️ create widgets without complicated cookiecutter templates
- 📚 publish to PyPI like any other Python package
- 🤖 prototype within
.ipynb
or.py
files - 🚀 run in Jupyter, JupyterLab, Google Colab, VSCode, and more
- ⚡ develop with instant HMR, like modern web frameworks
Learn more in the announcement.
Warning: anywidget is new and under active development. It is not yet ready for production as APIs are subject to change.
anywidget is available on PyPI and
may be installed with pip
:
pip install "anywidget[dev]"
It is also available on conda-forge. If you have Anaconda or Miniconda installed on your computer, you can install anywidget with the following command:
conda install -c conda-forge anywidget
import anywidget
import traitlets
class CounterWidget(anywidget.AnyWidget):
# Widget front-end JavaScript code
_esm = """
export function render(view) {
let getCount = () => view.model.get("count");
let button = document.createElement("button");
button.innerHTML = `count is ${getCount()}`;
button.addEventListener("click", () => {
view.model.set("count", getCount() + 1);
view.model.save_changes();
});
view.model.on("change:count", () => {
button.innerHTML = `count is ${getCount()}`;
});
view.el.appendChild(button);
}
"""
# Stateful property that can be accessed by JavaScript & Python
count = traitlets.Int(0).tag(sync=True)
Front-end code can also live in separate files (recommend):
import pathlib
import anywidget
import traitlets
class CounterWidget(anywidget.AnyWidget):
_esm = pathlib.Path("index.js")
_css = pathlib.Path("styles.css")
count = traitlets.Int(0).tag(sync=True)
Read the documentation to learn more.
This is a monorepo, meaning the repo holds multiple packages. It requires the use of pnpm. You can install pnpm with:
npm i -g pnpm
Then, create a Python virtual environment with a complete development install:
pip install -e ".[dev,test]"
or alternatively use the hatch
CLI:
hatch shell
If you are using the classic Jupyter Notebook you need to install the nbextension:
jupyter nbextension install --py --symlink --sys-prefix anywidget
jupyter nbextension enable --py --sys-prefix anywidget
Note for developers:
- the
-e
pip option allows one to modify the Python code in-place. Restart the kernel in order to see the changes. - the
--symlink
argument on Linux or OS X allows one to modify the JavaScript code in-place. This feature is not available with Windows.
For developing with JupyterLab:
jupyter labextension develop --overwrite anywidget
There are a few guidelines we follow:
- For JavaScript, internal variables are written with
snake_case
while external APIs are written withcamelCase
(if applicable). - For Python, ensure
black --check .
andruff .
pass. You can runblack .
andruff --fix .
to format and fix linting errors.
For changes to be reflected in package changelogs, run npx changeset
and
follow the prompts.
Note not every PR requires a changeset. Since changesets are focused on releases and changelogs, changes to the repository that don't effect these won't need a changeset (e.g., documentation, tests).
The Changesets GitHub action will create and update a PR that applies changesets and publishes new versions of anywidget to NPM and PyPI.