Basic Usage

Building Data Trees

Working with datatree begins with the Tree class:

tree = Tree()

To add a new node to the tree you simply call a function with the name of the new node that you wish to add. So to add a root node to the tree you could:


All nodes are also context managers. This allows for very logical building of the tree like so:

with tree.root() as root:"Bob Smith")

There are some various options when adding nodes, although most of them are only relevent if you are creating XML. When adding a node, any **kwargs argument passed in is converted to an attribute:

with tree.root() as root:"Bob Smith", gender='Male')

Emitting Formatted Data

Renderers are used to output your datatree into a dataformat. These are simple classes that translate the Tree into a specific format. By default XML is used. However an alias can be used for a different format. Additionaly all of the **kwargs are used to pass specific options to the renderer. To output pretty XML:

tree = Tree()
with tree.root() as root:"Bob Smith", full=True)
print tree('xml', pretty=True)


    <name full="True">Bob Smith</name>

The aliases for the formats are as follows:

Renderer Alias(s)
XML xml, [blank]
JSON json, jsn
YAML yaml, yml
Python dict dict, dictionary