ComponentTemplate

class dasha.web.templates.ComponentTemplate(*args, **kwargs)[source]

Bases: dasha.web.templates.Template

A class that wraps a Dash component type.

Instances of this class is typically created from calling the child() method, which allows declaring a tree of components with automatic unique ids.

Instances of this class serves as a lazy evaluation proxy around a native Dash components. Dash component properties on the template instance are passed to the actual component constructor when the layout property is queried.

This allows one to define a set of interrelated components as a “template” and use multiple instances of it in a single page application, without the need to worrying about possible confliction in the ids.

Furthermore, the easy-to-create tree structure through (chaining of) child() allows arbitrarily complex layouts to be created in a relatively compact syntax.

Attributes Summary

children

All child nodes.

id

The unique id.

layout

The layout generated from traversing the component tree.

parent

Parent Node.

Methods Summary

setup_layout(self, app)

Implement this to declare layout components and their callbacks.

Attributes Documentation

children

All child nodes.

>>> from anytree import Node
>>> n = Node("n")
>>> a = Node("a", parent=n)
>>> b = Node("b", parent=n)
>>> c = Node("c", parent=n)
>>> n.children
(Node('/n/a'), Node('/n/b'), Node('/n/c'))

Modifying the children attribute modifies the tree.

Detach

The children attribute can be updated by setting to an iterable.

>>> n.children = [a, b]
>>> n.children
(Node('/n/a'), Node('/n/b'))

Node c is removed from the tree. In case of an existing reference, the node c does not vanish and is the root of its own tree.

>>> c
Node('/c')

Attach

>>> d = Node("d")
>>> d
Node('/d')
>>> n.children = [a, b, d]
>>> n.children
(Node('/n/a'), Node('/n/b'), Node('/n/d'))
>>> d
Node('/n/d')

Duplicate

A node can just be the children once. Duplicates cause a TreeError:

>>> n.children = [a, b, d, a]
Traceback (most recent call last):
    ...
anytree.node.exceptions.TreeError: Cannot add node Node('/n/a') multiple times as child.
id

The unique id.

layout

The layout generated from traversing the component tree.

The traversing is depth first.

Note

Properties with callable values are evaluated the time the property is queried.

parent

Parent Node.

On set, the node is detached from any previous parent node and attached to the new node.

>>> from anytree import Node, RenderTree
>>> udo = Node("Udo")
>>> marc = Node("Marc")
>>> lian = Node("Lian", parent=marc)
>>> print(RenderTree(udo))
Node('/Udo')
>>> print(RenderTree(marc))
Node('/Marc')
└── Node('/Marc/Lian')

Attach

>>> marc.parent = udo
>>> print(RenderTree(udo))
Node('/Udo')
└── Node('/Udo/Marc')
    └── Node('/Udo/Marc/Lian')

Detach

To make a node to a root node, just set this attribute to None.

>>> marc.is_root
False
>>> marc.parent = None
>>> marc.is_root
True

Methods Documentation

setup_layout(self, app)[source]

Implement this to declare layout components and their callbacks.