02: Echo
An input that echoes whatever the user types. Introduces the two patterns that show up in every Weaverlet app from here on: declaring IDs with
Identifier(), and registering callbacks viaregister_callbacks(self, app).
The script
from weaverlet.base import WeaverletComponent, WeaverletApp, Identifier
from dash.dependencies import Input, Output
from dash import html, dcc
class EchoComponent(WeaverletComponent):
text_input_id = Identifier()
echo_div_id = Identifier()
def __init__(self, **kwargs):
super().__init__(**kwargs)
def get_layout(self):
return html.Div([
dcc.Input(id=self.text_input_id,
type='text'),
html.Div(id=self.echo_div_id)
])
def register_callbacks(self, app):
@app.callback(
Output(self.echo_div_id, 'children'),
Input(self.text_input_id, 'value')
)
def update_echo_div(text_value):
return f'{text_value}'
greeting_component = EchoComponent()
wapp = WeaverletApp(root_component=greeting_component)
wapp.app.run(port=8089)
Expected output
Typing into the input field updates the <div> underneath in real time.
Notes
Identifier()is a class-level descriptor.self.text_input_idreturns a unique Dash ID perEchoComponentinstance. Instantiate twice and the two copies don't collide.- Callbacks always live inside
register_callbacks(self, app).appisWeaverletApp.app, aDashProxyunder the hood. SoTrigger,Serverside(...), andgroup=multi-output callbacks all work without extra setup. - Reference IDs as
self.<identifier>, never as string literals. - This script imports
InputandOutputfromdash.dependencies. The modern import isfrom dash_extensions.enrich import Input, Output: it unlocks the enrich-only features above. Both forms work; prefer the enrich import in new code.
Try it locally
pip install weaverlet
python echo.py
See also
Identifier: conceptIdentifier: API reference- Quickstart. The same script with modern imports.