Skip to content

textual.reactive

This module contains the Reactive class which implements reactivity.

Reactive

Reactive(
    default,
    *,
    layout=False,
    repaint=True,
    init=False,
    always_update=False,
    compute=True,
    recompose=False,
    bindings=False
)

Bases: Generic[ReactiveType]

Reactive descriptor.

Parameters:

Name Type Description Default
default ReactiveType | Callable[[], ReactiveType]

A default value or callable that returns a default.

required
layout bool

Perform a layout on change.

False
repaint bool

Perform a repaint on change.

True
init bool

Call watchers on initialize (post mount).

False
always_update bool

Call watchers even when the new value equals the old value.

False
compute bool

Run compute methods when attribute is changed.

True
recompose bool

Compose the widget again when the attribute changes.

False
bindings bool

Refresh bindings when the reactive changes.

False

owner property

owner: Type[MessageTarget]

The owner (class) where the reactive was declared.

ReactiveError

Bases: Exception

Base class for reactive errors.

TooManyComputesError

Bases: ReactiveError

Raised when an attribute has public and private compute methods.

reactive

reactive(
    default,
    *,
    layout=False,
    repaint=True,
    init=True,
    always_update=False,
    recompose=False,
    bindings=False
)

Bases: Reactive[ReactiveType]

Create a reactive attribute.

Parameters:

Name Type Description Default
default ReactiveType | Callable[[], ReactiveType]

A default value or callable that returns a default.

required
layout bool

Perform a layout on change.

False
repaint bool

Perform a repaint on change.

True
init bool

Call watchers on initialize (post mount).

True
always_update bool

Call watchers even when the new value equals the old value.

False
bindings bool

Refresh bindings when the reactive changes.

False

var

var(
    default, init=True, always_update=False, bindings=False
)

Bases: Reactive[ReactiveType]

Create a reactive attribute (with no auto-refresh).

Parameters:

Name Type Description Default
default ReactiveType | Callable[[], ReactiveType]

A default value or callable that returns a default.

required
init bool

Call watchers on initialize (post mount).

True
always_update bool

Call watchers even when the new value equals the old value.

False
bindings bool

Refresh bindings when the reactive changes.

False

await_watcher async

await_watcher(obj, awaitable)

Coroutine to await an awaitable returned from a watcher

invoke_watcher

invoke_watcher(
    watcher_object, watch_function, old_value, value
)

Invoke a watch function.

Parameters:

Name Type Description Default
watcher_object Reactable

The object watching for the changes.

required
watch_function WatchCallbackType

A watch function, which may be sync or async.

required
old_value object

The old value of the attribute.

required
value object

The new value of the attribute.

required