asciitree¶
asciitree draws tree structures using characters.
ASCII Trees¶
asciitree
 +-- sometimes
 |   +-- you
 +-- just
 |   +-- want
 |       +-- to
 |       +-- draw
 +-- trees
 +-- in
     +-- your
         +-- terminal
from asciitree import LeftAligned
from collections import OrderedDict as OD
tree = {
    'asciitree': OD([
        ('sometimes',
            {'you': {}}),
        ('just',
            {'want': OD([
                ('to', {}),
                ('draw', {}),
            ])}),
        ('trees', {}),
        ('in', {
            'your': {
                'terminal': {}
            }
        })
    ])
}
tr = LeftAligned()
print(tr(tree))
Read the documentation at http://pythonhosted.org/asciitree
Fancier examples:
from collections import OrderedDict as OD
from copy import deepcopy
from asciitree import LeftAligned
from asciitree.drawing import BoxStyle, BOX_DOUBLE, BOX_BLANK
tr = LeftAligned()
# a basic tree
# OrderedDict is used in some places where node order is important, otherwise
# a normal dict is used for the sake of readabilitiy
tree = {
    'asciitree': OD([
        ('sometimes',
            {'you': {}}),
        ('just',
            {'want': OD([
                ('to', {}),
                ('draw', {}),
            ])}),
        ('trees', {}),
        ('in', {
            'your': {
                'terminal': {}
            }
        })
    ])
}
print(tr(tree))
# construct a more complex tree by copying the tree and grafting it onto itself
tree2 = deepcopy(tree)
tree2['asciitree']['trees'] = deepcopy(tree2['asciitree'])
print(tr(tree2))
# use a box style
box_tr = LeftAligned(draw=BoxStyle(gfx=BOX_DOUBLE, horiz_len=1))
print(box_tr(tree))
# more airy
air_tr = LeftAligned(draw=BoxStyle(gfx=BOX_BLANK,
                                   label_space=0,
                                   label_format='[{}]',
                                   indent=0))
print(air_tr(tree))
API¶
- 
class 
asciitree.LeftAligned(**kwargs)¶ Creates a renderer for a left-aligned tree.
Any attributes of the resulting class instances can be set using constructor arguments.
- 
__call__(tree)¶ Render the tree into string suitable for console output.
Parameters: tree – A tree. 
- 
render(node)¶ Renders a node. This function is used internally, as it returns a list of lines. Use
__call__()instead.
- 
 
- 
class 
asciitree.traversal.AttributeTraversal(**kwargs)¶ Attribute traversal.
Uses an attribute of a node as its list of children.
- 
attribute= 'children'¶ Attribute to use.
- 
 
- 
class 
asciitree.traversal.DictTraversal(**kwargs)¶ Traversal suitable for a dictionary. Keys are tree labels, all values must be dictionaries as well.
- 
class 
asciitree.traversal.Traversal(**kwargs)¶ Traversal method.
Used by the tree rendering functions like
LeftAligned.- 
get_children(node)¶ Return a list of children of a node.
- 
get_root(tree)¶ Return a node representing the tree root from the tree.
- 
get_text(node)¶ Return the text associated with a node.
- 
 
- 
asciitree.drawing.BOX_ASCII= {'UP_AND_RIGHT': u'+', 'HORIZONTAL': u'-', 'VERTICAL': u'|', 'VERTICAL_AND_RIGHT': u'+'}¶ Unicode box-drawing glyphs, using only ascii
|+-characters.
- 
asciitree.drawing.BOX_BLANK= {'UP_AND_RIGHT': u' ', 'HORIZONTAL': u' ', 'VERTICAL': u' ', 'VERTICAL_AND_RIGHT': u' '}¶ Unicode box-drawing glyphs, using only spaces.
- 
asciitree.drawing.BOX_DOUBLE= {'UP_AND_RIGHT': u'\u255a', 'HORIZONTAL': u'\u2550', 'VERTICAL': u'\u2551', 'VERTICAL_AND_RIGHT': u'\u2560'}¶ Unicode box-drawing glyphs, double-line style
- 
asciitree.drawing.BOX_HEAVY= {'UP_AND_RIGHT': u'\u2517', 'HORIZONTAL': u'\u2501', 'VERTICAL': u'\u2503', 'VERTICAL_AND_RIGHT': u'\u2523'}¶ Unicode box-drawing glyphs, heavy style
- 
asciitree.drawing.BOX_LIGHT= {'UP_AND_RIGHT': u'\u2514', 'HORIZONTAL': u'\u2500', 'VERTICAL': u'\u2502', 'VERTICAL_AND_RIGHT': u'\u251c'}¶ Unicode box-drawing glyphs, light style
- 
class 
asciitree.drawing.BoxStyle(**kwargs)¶ A rendering style that uses box draw characters and a common layout.
- 
gfx= {'UP_AND_RIGHT': u'+', 'HORIZONTAL': u'-', 'VERTICAL': u'|', 'VERTICAL_AND_RIGHT': u'+'}¶ Glyhps to use.
- 
horiz_len= 2¶ Length of horizontal lines
- 
indent= 1¶ Indent for subtrees
- 
label_space= 1¶ Space between glyphs and label.
- 
 
- 
class 
asciitree.drawing.Style(**kwargs)¶ Rendering style for trees.
- 
child_head(label)¶ Render a node label into final output.
- 
child_tail(line)¶ Render a node line that is not a label into final output.
- 
label_format= u'{}'¶ Format for labels.
- 
last_child_head(label)¶ Like
child_head()but only called for the last child.
- 
last_child_tail(line)¶ Like
child_tail()but only called for the last child.
- 
node_label(text)¶ Render a node text into a label.
-