Skip to content

Widgets

The two wrappers in trrack_widget.

Persisted(target, traits=None, *, store, **kwargs)

Save a widget's state and restore it the next time it loads.

Wraps target and writes its tracked traits to a store whenever they change. Create it again with the same store and the widget picks up where it left off. Displaying a Persisted shows the target. If a save fails, the error appears under it.

The store decides where the state lives. Use JsonFileStore for a file, or pass any object with save(dict) and load().

Examples:

from trrack_widget import JsonFileStore, Persisted

persisted = Persisted(counter, store=JsonFileStore("state.json"))
persisted  # displays the counter

Wrap target and keep its tracked traits in a store.

Parameters:

Name Type Description Default
target Any

The widget to save. It must be an anywidget.

required
traits list[str]

Trait names to save. Defaults to the target's public synced traits.

None
store Store

Where to save.

required
**kwargs Any

Forwarded to anywidget.AnyWidget.

{}

Raises:

Type Description
ValueError

If the store holds a Trrackable graph, or a payload that is not in a known format.

state property

The current values of the tracked traits, keyed by trait name.

reset()

Put the widget back to the values it started with, and save them.

The starting values are the ones the widget had before any saved state was loaded. If the save fails, the error appears under the widget.

save()

Write to the store now.

Raises the store's error if the save fails.

close()

Stop following the target, save any pending change, and close.

Trrackable(target, traits=None, *, debounce_ms=0, store=None, restore=None, label_formatter=None, buttons=True, graph=True, **kwargs)

Record a widget's state as a branching history with undo and redo.

Wraps target and adds a node to the history each time a tracked trait changes. Displaying a Trrackable shows the target with the history controls next to it. If a save fails, the error appears under them.

The controls are a row of buttons and the history graph. Set buttons and graph to show or hide them.

Examples:

from trrack_widget import Trrackable

tracked = Trrackable(counter)
tracked  # displays the counter and its history
tracked.undo()
tracked = Trrackable(counter, graph=False)  # buttons only
tracked.graph = True  # show the graph

Wrap target and record its tracked traits.

Parameters:

Name Type Description Default
target Any

The widget to track. It must be an anywidget.

required
traits list[str]

Trait names to record. Defaults to the target's public synced traits. A loaded history keeps the trait names it was saved with.

None
debounce_ms int

Record a burst of changes within this many milliseconds as one node. 0 records every change. Useful for sliders.

0
store Store

Where to save, such as JsonFileStore("state.json"). When the store already holds a history, that history is loaded. State saved by Persisted loads as the first node of a new history.

None
restore dict

A to_dict() payload to load, in place of the store.

None
label_formatter Callable[[dict, dict], str]

Builds a node's label from the old and new tracked values. Defaults to a list of what changed, such as count: 1 → 2.

None
buttons bool

Show the row of buttons. Defaults to True.

True
graph bool

Show the history graph. Defaults to True. The History button in the row also shows and hides it.

True
**kwargs Any

Forwarded to anywidget.AnyWidget.

{}

Raises:

Type Description
ValueError

If the store or restore holds a payload that is not in a known format.

state property

The current values of the tracked traits, keyed by trait name.

undo()

Go back one step, and set the target to that state.

redo()

Go forward to the newest next step, and set the target to it.

go_to(node_id)

Jump to a node, and set the target to its state.

Parameters:

Name Type Description Default
node_id str

The id of the node to go to.

required

label(text, node_id=None)

Rename a node.

Parameters:

Name Type Description Default
text str

The new label.

required
node_id str

The node to rename. Defaults to the current node.

None

flush()

Record a pending debounced change now, if there is one.

save()

Write to the store now.

Raises the store's error if the save fails.

close()

Stop following the target, save any pending change, and close.

to_dict()

Serialize the tracked trait names and the whole history.

from_dict(data, target, *, debounce_ms=0, label_formatter=None, buttons=True, graph=True) classmethod

Rebuild a Trrackable from to_dict() output.

Parameters:

Name Type Description Default
data dict

The saved payload.

required
target Any

The widget to track. It is set to the saved state.

required
debounce_ms int

See Trrackable.

0
label_formatter Callable[[dict, dict], str]

See Trrackable.

None
buttons bool

See Trrackable.

True
graph bool

See Trrackable.

True

Returns:

Name Type Description
Trrackable Trrackable

A widget that tracks target with the saved history.