anywidget

reusable interactive widgets made easy

pip install anywidget
mywidget/__init__.py code example
 

mywidget/__init__.py

import anywidget
import traitlets


class CounterWidget(anywidget.AnyWidget):
    _esm = """
    function render({ model, el }) {
      let count = () => model.get("value");
      let btn = document.createElement("button");
      btn.innerHTML = `count is ${count()}`;
      btn.addEventListener("click", () => {
        model.set("value", count() + 1);
        model.save_changes();
      });
      model.on("change:value", () => {
        btn.innerHTML = `count is ${count()}`;
      });
      el.appendChild(btn);
    }
    export default { render };
    """
    value = traitlets.Int(0).tag(sync=True)

Beyond Frameworks, a Widget Standard

My talk from SciPy 2024 about how anywidget sets a standard for portable widgets while maintaining ecosystem compatibility.