Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

2. Anywidgets talking to each other

Two widgets on the same page can communicate without a kernel through the static anywidget host. The LinkedCounterWidget resolves its target with host.waitForModel() using the source widget’s widget_id, so the example exercises the same shape as the AFM widget-composition API.

Below, a LinkedCounterWidget watches a CounterWidget and mirrors its value. Click the source’s +/- and the follower’s value follows.

import sys
import pathlib
sys.path.insert(0, str(pathlib.Path().absolute().parent))

from widgets.counter_widget import CounterWidget
from widgets.linked_counter import LinkedCounterWidget

Source

Same CounterWidget as before. Its widget_id is what the follower will look up.

source = CounterWidget(
    label="Source",
    widget_id="linked_source",
    value=0,
)
source

Follower

LinkedCounterWidget resolves its link_to target through host.waitForModel() and subscribes to its value changes. link_mode="mirror" copies the source’s value verbatim. (Other modes: sum, diff.)

follower = LinkedCounterWidget(
    label="Follower",
`LinkedCounterWidget` resolves its `link_to` target through `host.waitForModel()` and subscribes to its `value` changes. `link_mode="mirror"` copies the source's value verbatim. (Other modes: `sum`, `diff`.)    link_to="linked_source",
    link_mode="mirror",
    value=0,
)
follower