Skip to content

Persistable Widget

PersistableWidget is the base class of a widget whose state can be kept across restarts, see Persistent Storage.

import dachshund
from dachshund.widgets import Number

with dachshund.row():
    dachshund.add(Number("cpu", persist=True))

dachshund.widgets.persistable.PersistableWidget dataclass

PersistableWidget(name, *, source='', title=None, actions=list(), action_args=dict(), persist=False)

Bases: Widget, ABC

A Widget whose state can be kept across restarts.

With persist=True, every event goes through accumulate(), the state from snapshot() is saved to the dashboard's storage, and restore() seeds it on start. Without it, none of the three is called.

For state with one entry per key, subclass KeyedWidget instead.

Example

@dataclass
class Gauge(PersistableWidget):
    last: float | None = field(default=None, init=False)

    type: ClassVar[str] = "gauge"

    def accumulate(self, event):
        self.last = self.value(event, "value")

    def snapshot(self):
        return {"last": self.last}

    def restore(self, data):
        self.last = data.get("last")

Options

  • persist (bool) - Keeps the widget's state across restarts. Needs the dashboard's storage. Defaults to False.

persist class-attribute instance-attribute

persist = False

accumulate abstractmethod

accumulate(event)

Fold a new event into this widget's state.

Source code in dachshund/widgets/persistable.py
49
50
51
@abstractmethod
def accumulate(self, event: Event) -> None:
    """Fold a new event into this widget's state."""

snapshot abstractmethod

snapshot()

This widget's current state, to save.

Source code in dachshund/widgets/persistable.py
53
54
55
@abstractmethod
def snapshot(self) -> dict[str, Any]:
    """This widget's current state, to save."""

restore abstractmethod

restore(data)

Seed this widget's state from a saved snapshot.

Source code in dachshund/widgets/persistable.py
57
58
59
@abstractmethod
def restore(self, data: dict[str, Any]) -> None:
    """Seed this widget's state from a saved snapshot."""