MCPcopy
hub / github.com/Textualize/textual / data_bind

Method data_bind

src/textual/dom.py:302–349  ·  view source on GitHub ↗

Bind reactive data so that changes to a reactive automatically change the reactive on another widget. Reactives may be given as positional arguments or keyword arguments. See the [guide on data binding](/guide/reactivity#data-binding). Example: ```python

(
        self,
        *reactives: Reactive[Any],
        **bind_vars: Reactive[Any] | object,
    )

Source from the content-addressed store, hash-verified

300 reactive._set(self, value, always=True)
301
302 def data_bind(
303 self,
304 *reactives: Reactive[Any],
305 **bind_vars: Reactive[Any] | object,
306 ) -> Self:
307 """Bind reactive data so that changes to a reactive automatically change the reactive on another widget.
308
309 Reactives may be given as positional arguments or keyword arguments.
310 See the [guide on data binding](/guide/reactivity#data-binding).
311
312 Example:
313 ```python
314 def compose(self) -> ComposeResult:
315 yield WorldClock("Europe/London").data_bind(WorldClockApp.time)
316 yield WorldClock("Europe/Paris").data_bind(WorldClockApp.time)
317 yield WorldClock("Asia/Tokyo").data_bind(WorldClockApp.time)
318 ```
319
320 Raises:
321 ReactiveError: If the data wasn't bound.
322
323 Returns:
324 Self.
325 """
326 _rich_traceback_omit = True
327
328 parent = active_message_pump.get()
329
330 if self._reactive_connect is None:
331 self._reactive_connect = {}
332 bind_vars = {**{reactive.name: reactive for reactive in reactives}, **bind_vars}
333 for name, reactive in bind_vars.items():
334 if name not in self._reactives:
335 raise ReactiveError(
336 f"Unable to bind non-reactive attribute {name!r} on {self}"
337 )
338 if isinstance(reactive, Reactive) and not isinstance(
339 parent, reactive.owner
340 ):
341 raise ReactiveError(
342 f"Unable to bind data; {reactive.owner.__name__} is not defined on {parent.__class__.__name__}."
343 )
344 self._reactive_connect[name] = (parent, reactive)
345 if self._is_mounted:
346 self._initialize_data_bind()
347 else:
348 self.call_later(self._initialize_data_bind)
349 return self
350
351 def _initialize_data_bind(self) -> None:
352 """initialize a data binding.

Callers 12

composeMethod · 0.80
composeMethod · 0.80
composeMethod · 0.80
composeMethod · 0.80
composeMethod · 0.80
composeMethod · 0.80
composeMethod · 0.80
test_data_bindingFunction · 0.80
composeMethod · 0.80
composeMethod · 0.80
composeMethod · 0.80
composeMethod · 0.80

Calls 5

_initialize_data_bindMethod · 0.95
ReactiveErrorClass · 0.90
itemsMethod · 0.80
call_laterMethod · 0.80
getMethod · 0.45

Tested by 4

composeMethod · 0.64
composeMethod · 0.64
test_data_bindingFunction · 0.64
composeMethod · 0.64