Template¶
-
class
dash_component_template.template.
Template
(parent=None)[source]¶ Bases:
dash_component_template.idtree.IdTree
An abstract base class that encapsulate layout and callbacks into a reusable structure.
The core of this class is the
child()
method which creates and maintains a tree of instances whose nodes have unique ids assigned.Actual Dash components are typically not instantiated when template instances are created. Instead, the template instances hold the factories and the arguments, and they only get called when
layout
property is queried. This call will be propagated down to all the children of the calling instance, result in a tree of Dash component objects readily be consumed by the Dashapp
instance.Complex layout can be built in a declarative fashion with repeated call to the
child()
factory function from any node in the tree. All the node have their unique id (the Dash component id) managed automatically and they can be accessed with theid
property, which can then be consumed in callback definitions.Subclass of this base class shall implement method
setup_layout()
, within which one can declare a layout usingchild()
, and define any callbacks for them components.Subclasses defined this way will act as re-usable templates that can be plugged anywhere in other trees of templates. Through this mechanism, very complex layout can be built in a fully modularized fashion.
Attributes Summary
id
The unique id. layout
Implement this to return a valid Dash layout object. Methods Summary
after_setup_layout
(app)Hook that run after the setup_layout function call. before_setup_layout
(app)Hook that run before the setup_layout function call. child
(factory, *args, **kwargs)Return a child template instance. grid
(nrows, ncols[, squeeze])Return a dash bootstrap component grid. setup_layout
(app)Implement this to declare layout components and their callbacks. Attributes Documentation
-
id
¶ The unique id.
-
layout
¶ Implement this to return a valid Dash layout object.
Methods Documentation
-
child
(factory, *args, **kwargs)[source]¶ Return a child template instance.
The actual type of child template is delegated to the appropriate subclass based on the type of factory:
1. factory is a Template instance. The instance is added as-is as the child of this object. ValueError is raised if args or kwargs are passed.
2. factory is a Dash component class, (e.g., ~dash_html_components.Div). A ComponentTemplate object is created and returned. args and kwargs are passed to the constructor of the ComponentTemplate, which is passed down to the Dash component class when actual Dash component is created.
3. factory is a Dash component instance. The instance is wrapped in a WrappedComponent object and returned. ValueError is raised if args or kwargs are passed.
ValueError is raised if factory does not conform to the cases listed above.
-
grid
(nrows, ncols, squeeze=True)[source]¶ Return a dash bootstrap component grid.
Parameters: - nrows (int) – The number of rows.
- ncols (int) – The number of cols per row.
- squeeze (bool, optional) – If True, insignificant dimensions are removed from the returned array.
-
setup_layout
(app)[source]¶ Implement this to declare layout components and their callbacks.
This base implementation has to be called in the subclass implementation to ensure any child templates also get properly setup. This is particularly important for templates that contain templates in their descendants.
The convention is to structure the implementation in the following way:
def setup_layout(self, app): child0 = self.child(some_dash_type, ...) child1 = child0.child(some_template_cls, ...) # This will trigger `setup_layout` call to all the children, # which may make available some attributes super().setup_layout(app) @app.callback(...) def some_callback(...): return
-