Dom node
A DOMNode is a base class for any object within the Textual Document Object Model, which includes all Widgets, Screens, and Apps.
WalkMethod
module-attribute
¶
Valid walking methods for the DOMNode.walk_children
method.
BadIdentifier
class
¶
Bases: Exception
Exception raised if you supply a id
attribute or class name in the wrong format.
DOMNode
class
¶
Bases: MessagePump
The base class for object that can be in the Textual DOM (App and Widget)
SCOPED_CSS
class-attribute
¶
Should default css be limited to the widget type?
ancestors
property
¶
ancestors_with_self
property
¶
auto_refresh
writable
property
¶
Number of seconds between automatic refresh, or None
for no automatic refresh.
background_colors
property
¶
children
property
¶
classes
instance-attribute
class-attribute
¶
CSS class names for this node.
colors
property
¶
css_identifier_styled
property
¶
css_path_nodes
property
¶
css_tree
property
¶
A Rich tree to display the DOM, annotated with the node's CSS.
Log this to visualize your app in the textual console.
Returns
Type | Description |
---|---|
Tree
|
A Tree renderable. |
display
writable
property
¶
displayed_children
property
¶
parent
property
¶
The parent node.
All nodes have parent once added to the DOM, with the exception of the App which is the root node.
rich_style
property
¶
screen
property
¶
The screen containing this node.
Returns
Type | Description |
---|---|
'Screen[object]'
|
A screen object. |
Raises
Type | Description |
---|---|
NoScreen
|
If this node isn't mounted (and has no screen). |
text_style
property
¶
Get the text style object.
A widget's style is influenced by its parent. for instance if a parent is bold, then the child will also be bold.
Returns
Type | Description |
---|---|
Style
|
A Rich Style. |
tree
property
¶
A Rich tree to display the DOM.
Log this to visualize your app in the textual console.
Returns
Type | Description |
---|---|
Tree
|
A Tree renderable. |
visible
writable
property
¶
Is this widget visible in the DOM?
If a widget hasn't had its visibility set explicitly, then it inherits it from its DOM ancestors.
This may be set explicitly to override inherited values.
The valid values include the valid values for the visibility
rule and the booleans
True
or False
, to set the widget to be visible or invisible, respectively.
When a node is invisible, Textual will reserve space for it, but won't display anything.
action_toggle
async
¶
Toggle an attribute on the node.
Assumes the attribute is a bool.
Parameters
Parameter | Default | Description |
---|---|---|
attribute_name
str
|
required | Name of the attribute. |
add_class
method
¶
data_bind
method
¶
Bind reactive data so that changes to a reactive automatically change the reactive on another widget.
Reactives may be given as positional arguments or keyword arguments. See the guide on data binding.
Example
Raises
Type | Description |
---|---|
ReactiveError
|
If the data wasn't bound. |
Returns
Type | Description |
---|---|
Self
|
Self. |
get_component_styles
method
¶
Get a "component" styles object (must be defined in COMPONENT_CLASSES classvar).
Parameters
Parameter | Default | Description |
---|---|---|
name
str
|
required | Name of the component. |
Raises
Type | Description |
---|---|
KeyError
|
If the component class doesn't exist. |
Returns
Type | Description |
---|---|
RenderStyles
|
A Styles object. |
get_pseudo_classes
method
¶
has_class
method
¶
has_pseudo_class
method
¶
has_pseudo_classes
method
¶
notify_style_update
method
¶
Called after styles are updated.
Implement this in a subclass if you want to clear any cached data when the CSS is reloaded.
query
method
¶
query_one
method
¶
Get a widget from this widget's children that matches a selector or widget type.
Parameters
Parameter | Default | Description |
---|---|---|
selector
str | type[QueryType]
|
required | A selector or widget type. |
expect_type
type[QueryType] | None
|
None
|
Require the object be of the supplied type, or None for any type. |
Raises
Type | Description |
---|---|
WrongType
|
If the wrong type was found. |
NoMatches
|
If no node matches the query. |
TooManyMatches
|
If there is more than one matching node in the query. |
Returns
Type | Description |
---|---|
QueryType | Widget
|
A widget matching the selector. |
remove_class
method
¶
run_worker
method
¶
def run_worker(
self,
work,
name="",
group="default",
description="",
exit_on_error=True,
start=True,
exclusive=False,
thread=False,
):
Run work in a worker.
A worker runs a function, coroutine, or awaitable, in the background as an async task or as a thread.
Parameters
Parameter | Default | Description |
---|---|---|
work
WorkType[ResultType]
|
required | A function, async function, or an awaitable object to run in a worker. |
name
str | None
|
''
|
A short string to identify the worker (in logs and debugging). |
group
str
|
'default'
|
A short string to identify a group of workers. |
description
str
|
''
|
A longer string to store longer information on the worker. |
exit_on_error
bool
|
True
|
Exit the app if the worker raises an error. Set to |
start
bool
|
True
|
Start the worker immediately. |
exclusive
bool
|
False
|
Cancel all workers in the same group. |
thread
bool
|
False
|
Mark the worker as a thread worker. |
Returns
Type | Description |
---|---|
Worker[ResultType]
|
New Worker instance. |
set_class
method
¶
set_classes
method
¶
set_reactive
method
¶
Sets a reactive value without invoking validators or watchers.
Parameters
Parameter | Default | Description |
---|---|---|
name
|
required | Name of reactive attribute. |
value
ReactiveType
|
required | New value of reactive. |
Raises
Type | Description |
---|---|
AttributeError
|
If the first argument is not a reactive. |
set_styles
method
¶
sort_children
method
¶
Sort child widgets with an optional key function.
If key
is not provided then widgets will be sorted in the order they are constructed.
Parameters
Parameter | Default | Description |
---|---|---|
key
Callable[[Widget], SupportsRichComparison] | None
|
None
|
A callable which accepts a widget and returns something that can be sorted,
or |
reverse
bool
|
False
|
Sort in descending order. |
toggle_class
method
¶
Toggle class names on this Node.
Parameters
Parameter | Default | Description |
---|---|---|
*class_names
str
|
()
|
CSS class names to toggle. |
Returns
Type | Description |
---|---|
Self
|
Self. |
walk_children
method
¶
Walk the subtree rooted at this node, and return every descendant encountered in a list.
Parameters
Parameter | Default | Description |
---|---|---|
filter_type
type[WalkType] | None
|
None
|
Filter only this type, or None for no filter. |
with_self
bool
|
False
|
Also yield self in addition to descendants. |
method
WalkMethod
|
'depth'
|
One of "depth" or "breadth". |
reverse
bool
|
False
|
Reverse the order (bottom up). |
Returns
Type | Description |
---|---|
list[DOMNode] | list[WalkType]
|
A list of nodes. |
watch
method
¶
Watches for modifications to reactive attributes on another object.
Example
Here's how you could detect when the app changes from dark to light mode (and vice versa).
Parameters
Parameter | Default | Description |
---|---|---|
obj
DOMNode
|
required | Object containing attribute to watch. |
attribute_name
str
|
required | Attribute to watch. |
callback
WatchCallbackType
|
required | A callback to run when attribute changes. |
init
bool
|
True
|
Check watchers on first call. |