bootstack.Signal#

class bootstack.Signal(value, name=None, master=None)#

Bases: Generic[T]

A reactive value that widgets can bind to.

Holds a typed value and notifies subscribers when it changes. Read it by calling the signal, update it with set(), derive new signals with map(), and react to changes with subscribe().

Bind a signal to a widget by passing it as textsignal= for text-bearing widgets, or signal= for boolean and numeric widgets.

property type: Type[T]#

The original type of the signal value.

Returns:

A Python type (e.g., int, str).

__call__()#

Get the current value of the signal.

Returns:

The current typed value.

Return type:

T

map(transform)#

Create a derived signal that transforms this signal’s value.

The derived signal recomputes whenever this signal changes. It is held weakly, so keep a reference to it — for example, by binding it to a widget — or it will stop updating once garbage-collected.

Parameters:

transform (Callable[[T], U]) – A function applied to the current and future values.

Returns:

A new read-only signal that stays updated with the transformed value.

Return type:

Signal[U]

set(value)#

Set the signal to a new value and notify subscribers.

Parameters:

value (T) – The new value. Must match the signal’s type, except that an int may be set on a float-typed signal (it is widened).

Raises:

TypeError – If the value type does not match the signal’s type (and is not an int widened to a float).

subscribe(callback, *, immediate=False)#

Subscribe to value changes of this signal.

Parameters:
  • callback (Callable[[T], Any]) – A function called with the new value whenever it changes.

  • immediate (bool) – When True, also call callback once with the current value at subscription time. Defaults to False.

Returns:

A subscription id — pass it to unsubscribe() to stop listening.

Return type:

str

unsubscribe(funcid)#

Remove a previously registered subscriber.

Parameters:

funcid (str) – The function id returned from subscribe().

unsubscribe_all()#

Remove all currently subscribed callbacks.