DOM
wybthon.dom¶
dom
¶
Lightweight DOM wrapper utilities for Pyodide/browser environments.
This module exposes a thin Pythonic facade over the browser's DOM API, designed to feel familiar to JavaScript developers while integrating cleanly with Wybthon's reactive renderer:
Elementwraps a single DOM node and offers ergonomic helpers for attributes, classes, styles, events, and queries.Refis a mutable container used by the renderer to hand out a reference to a mounted element.
The wrapper deliberately mirrors familiar DOM property names (e.g.
value, checked, files) so that event handlers can read state
exactly as they would in JavaScript:
See Also
Classes:
| Name | Description |
|---|---|
Element |
Thin wrapper around a DOM node with convenience methods. |
Ref |
Mutable container holding a reference to an |
Element
¶
Thin wrapper around a DOM node with convenience methods.
Element can be constructed in three ways:
- With a tag name to create a brand-new node
(
Element("div")). - With a CSS selector and
existing=Trueto wrap an existing node (Element("#root", existing=True)). - With an opaque
nodevalue to wrap a node returned by another API (used internally by query helpers).
The wrapper proxies common form-input properties (value,
checked, files) so handlers can read state from
e.target.value exactly as in React or SolidJS.
Create a new element, wrap an existing one, or wrap a raw node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
Optional[str]
|
Tag name ( |
None
|
existing
|
bool
|
If |
False
|
node
|
Any
|
Raw underlying DOM node to wrap. When provided,
|
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither |
Methods:
| Name | Description |
|---|---|
set_text |
Replace the text content of this element. |
append_to |
Append this element to |
append |
Append an |
remove |
Detach this element from its parent (no-op if already detached). |
load_html |
Fetch HTML from |
set_html |
Replace this element's content with the provided HTML string. |
set_attr |
Set an attribute on this element, with text-node fallbacks. |
get_attr |
Return the attribute value for |
remove_attr |
Remove an attribute from this element. |
set_style |
Set CSS properties using a dict and/or keyword arguments. |
add_class |
Add one or more CSS classes to this element. |
remove_class |
Remove one or more CSS classes from this element. |
toggle_class |
Toggle a CSS class, optionally forcing on/off. |
has_class |
Return |
on |
Add an event listener and track it for later cleanup. |
off |
Remove matching event listeners previously attached via |
cleanup |
Remove all tracked event listeners from this element. |
query |
Query a single element by CSS selector. |
query_all |
Query all matching elements by CSS selector. |
find |
Return the first matching descendant |
find_all |
Return all matching descendant elements as a list. |
attach_ref |
Store this element on |
Attributes:
| Name | Type | Description |
|---|---|---|
value |
Any
|
Current value of an |
checked |
bool
|
Checked state of a checkbox or radio input. |
files |
Any
|
|
append
¶
Append an Element or a text string as a child node.
load_html
async
¶
load_html(url: str) -> None
Fetch HTML from url and assign it to innerHTML.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to fetch via the browser's |
required |
set_html
¶
set_html(html: str) -> None
Replace this element's content with the provided HTML string.
Caution
This bypasses the renderer's diffing and does not sanitize input. Avoid passing untrusted HTML.
set_attr
¶
get_attr
¶
Return the attribute value for name, or None when absent.
set_style
¶
set_style(styles: Optional[Dict[str, Union[str, int]]] = None, **style_kwargs: Union[str, int]) -> None
Set CSS properties using a dict and/or keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
styles
|
Optional[Dict[str, Union[str, int]]]
|
Optional mapping of CSS property name to value. |
None
|
**style_kwargs
|
Union[str, int]
|
Additional CSS properties (last write wins if a key appears in both). |
{}
|
remove_class
¶
remove_class(*class_names: str) -> None
Remove one or more CSS classes from this element.
toggle_class
¶
has_class
¶
Return True if the element currently has the given class.
on
¶
on(event_type: str, handler: Callable[[Any], Any], *, options: Optional[Dict[str, Any]] = None) -> None
Add an event listener and track it for later cleanup.
Listeners attached through on are remembered and removed by
off or
cleanup. The handler is wrapped in
a Pyodide proxy so it can be released on removal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_type
|
str
|
DOM event name (e.g. |
required |
handler
|
Callable[[Any], Any]
|
Callback invoked with the DOM event object. |
required |
options
|
Optional[Dict[str, Any]]
|
Optional |
None
|
off
¶
Remove matching event listeners previously attached via on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_type
|
Optional[str]
|
If given, only remove listeners of this type. |
None
|
handler
|
Optional[Callable[[Any], Any]]
|
If given, only remove listeners with this exact callback identity. |
None
|
When both arguments are None, all tracked listeners are
removed.
query
classmethod
¶
Query a single element by CSS selector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
str
|
CSS selector string. |
required |
within
|
Optional[Element]
|
Optional parent |
None
|
Returns:
| Type | Description |
|---|---|
Optional[Element]
|
The first matching |
query_all
classmethod
¶
Query all matching elements by CSS selector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
str
|
CSS selector string. |
required |
within
|
Optional[Element]
|
Optional parent |
None
|
Returns:
| Type | Description |
|---|---|
List[Element]
|
A list of wrapped |
find
¶
Return the first matching descendant Element, or None.
find_all
¶
Return all matching descendant elements as a list.