Skip to content

trrackpy

Track the state of anywidget widgets.

Click the counter, undo, and click again:

This is the code that runs above:

import anywidget
import traitlets

from trrack_widget import Trrackable

class Counter(anywidget.AnyWidget):
    _esm = """
    export default {
      render({ model, el }) {
        const button = document.createElement("button");
        const draw = () => (button.textContent = `count: ${model.get("count")}`);
        button.onclick = () => {
          model.set("count", model.get("count") + 1);
          model.save_changes();
        };
        model.on("change:count", draw);
        draw();
        el.append(button);
      },
    };
    """
    count = traitlets.Int(0).tag(sync=True)

Trrackable(Counter())
  • Persisted saves a widget's state and restores it the next time it loads.
  • Trrackable records every change as a history you can navigate.
  • A store decides where the state lives: a file, a database, the URL. Write your own with two methods.

Where to start

  1. Install the package.
  2. Follow the Quickstart.
  3. Read the guides for Persisted and Trrackable.
  4. Run an example notebook.