reactive_read

reactive_read function

Reactively read a widget trait.

Usage Source

reactive_read(widget: Widget, names: Union[str, Sequence[str]])

Takes a reactive dependency on the given trait(s), so the calling reactive context (e.g., a @reactive.Calc, @reactive.Effect, or render function) is invalidated whenever the trait’s value changes (e.g., in response to user interaction with the widget in the browser).

For outputs of @render_* decorators, prefer the output’s .read(name) method – it reads accessors and falls through to this function for bare traits. reactive_read() remains the tool for widgets outside renderer contexts (e.g. a widget you constructed yourself).

Parameters

widget : Widget

The widget to read from (often obtained via the .widget attribute of a shinywidgets render decorator).

names : Union[str, Sequence[str]]

A trait name, or a sequence of trait names, to read.

Returns

: Any

The trait’s current value, or a tuple of values if names is a sequence.

Examples

from shiny import reactive
from shinywidgets import reactive_read, render_widget
import ipyleaflet


@render_widget
def map():
    return ipyleaflet.Map(center=(51.5, -0.09), zoom=4)


@reactive.Effect
def _():
    # Re-runs whenever the map's zoom level changes
    print(reactive_read(map.widget, "zoom"))