08: Signal trigger
Same setup as Example 07, but the consumer doesn't care about the payload. It just reacts to the edge. Demonstrates
SignalTrigger(no-payload consumer) paired with a producer that returns{}.
The script
from dash import html
from dash_extensions.enrich import Output, Trigger
from weaverlet.base import SignalOutput, SignalTrigger, WeaverletComponent, WeaverletApp, Identifier
from weaverlet.components import SignalComponent
class SignalTriggerComponent(WeaverletComponent):
trigger_button_id = Identifier()
label_p_id = Identifier()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.signal_component = SignalComponent()
def get_layout(self):
return html.Div(
[
self.signal_component(), # child component
html.Button('Click to trigger signal',
id=self.trigger_button_id),
html.P(id=self.label_p_id)
]
)
def register_callbacks(self, app):
@app.callback(
SignalOutput(self.signal_component),
Trigger(self.trigger_button_id, 'n_clicks')
)
def button_click():
return {}
@app.callback(
Output(self.label_p_id, 'children'),
SignalTrigger(self.signal_component)
)
def label_update():
return 'Signal triggered!'
signal_trigger_component = SignalTriggerComponent()
wapp = WeaverletApp(root_component=signal_trigger_component)
wapp.app.run(port=8089)
Expected output
- The page shows a button and an empty paragraph.
- Each click updates the paragraph to "Signal triggered!"
Notes
- The producer still returns a dict (
{}); Dash callbacks must return something. Use empty dict when there's no meaningful payload. SignalTrigger(sig)is the no-payload consumer adapter. The callback signature isdef consume():(no argument).- Don't confuse
Trigger(fromdash_extensions.enrich, turns any Dash prop into a no-payload input) withSignalTrigger(fromweaverlet, turns a signal into a no-payload input). They're different adapters for different sources. - This pattern is good for "something happened, refresh that view". Modal open, toast show, data reload.
Try it locally
pip install weaverlet
python signal_trigger_app.py