03: Greeting
A component that takes a constructor argument and renders it. Shows how to parameterize a component without resorting to props-as-strings or factory functions. You just pass kwargs to
__init__and store them onself.
The script
from weaverlet.base import WeaverletComponent, WeaverletApp
from dash import html
class GreetingComponent(WeaverletComponent):
def __init__(self, greeting_name, **kwargs):
super().__init__(**kwargs)
self.greeting_name = greeting_name
def get_layout(self):
return html.Div(f'hello {self.greeting_name}!')
greeting_component = GreetingComponent(greeting_name='Oscar')
wapp = WeaverletApp(root_component=greeting_component)
wapp.app.run(port=8089)
Expected output
The page shows the text "hello Oscar!".
Notes
- Components are regular Python classes. They receive arguments via
__init__like any other. - Always call
super().__init__(**kwargs)first to giveWeaverletComponenta chance to set up its DAG bookkeeping. Pass**kwargsthrough so callers can still setname=...and other base-class options. - The instance is a regular Python object: store data on
self, reference it inget_layout. There's no equivalent of React props because there's no equivalent of React re-rendering.
Try it locally
pip install weaverlet
python greeting.py