pywry.widget_protocol¶
Widget protocol interfaces and native window handle.
pywry.widget_protocol.BaseWidget
¶
Bases: Protocol
Protocol that all PyWry widgets must implement.
This enables a unified API across different rendering backends: - InlineWidget: FastAPI server + IFrame (notebook fallback) - PyWryWidget: anywidget with traitlet sync (best notebook performance) - Native windows: Desktop application rendering
Examples:
>>> widget = create_widget(...) # Returns any BaseWidget implementation
>>> widget.on("click", lambda data, event_type, label: print(data))
>>> widget.emit("update", {"value": 42})
>>> widget.display()
Functions¶
on
¶
on(event_type: str, callback: Callable[[dict[str, Any], str, str], Any]) -> BaseWidget
Register a callback for events from JavaScript.
| PARAMETER | DESCRIPTION |
|---|---|
event_type
|
Event name (e.g., 'plotly:click', 'toggle', 'grid:cell-click').
TYPE:
|
callback
|
Handler function receiving (data, event_type, label). - data: Event payload from JavaScript - event_type: Same as event_type parameter - label: Widget identifier
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BaseWidget
|
Self for method chaining. |
Examples:
emit
¶
Send an event from Python to JavaScript.
| PARAMETER | DESCRIPTION |
|---|---|
event_type
|
Event name that JS listeners can subscribe to.
TYPE:
|
data
|
JSON-serializable payload to send to JavaScript.
TYPE:
|
Examples:
update
¶
Update the widget's HTML content.
| PARAMETER | DESCRIPTION |
|---|---|
html
|
New HTML content to render. Should include necessary |