| 1 |
.. image:: https://badge.fury.io/py/anytree.svg |
| 2 |
:target: https://badge.fury.io/py/anytree |
| 3 |
|
| 4 |
.. image:: https://img.shields.io/pypi/dm/anytree.svg?label=pypi%20downloads |
| 5 |
:target: https://pypi.python.org/pypi/anytree |
| 6 |
|
| 7 |
.. image:: https://readthedocs.org/projects/anytree/badge/?version=latest |
| 8 |
:target: https://anytree.readthedocs.io/en/latest/?badge=latest |
| 9 |
|
| 10 |
.. image:: https://coveralls.io/repos/github/c0fec0de/anytree/badge.svg |
| 11 |
:target: https://coveralls.io/github/c0fec0de/anytree |
| 12 |
|
| 13 |
.. image:: https://api.codeclimate.com/v1/badges/e6d325d6fd23a93aab20/maintainability |
| 14 |
:target: https://codeclimate.com/github/c0fec0de/anytree/maintainability |
| 15 |
:alt: Maintainability |
| 16 |
|
| 17 |
.. image:: https://img.shields.io/pypi/pyversions/anytree.svg |
| 18 |
:target: https://pypi.python.org/pypi/anytree |
| 19 |
|
| 20 |
.. image:: https://img.shields.io/badge/code%20style-pep8-brightgreen.svg |
| 21 |
:target: https://www.python.org/dev/peps/pep-0008/ |
| 22 |
|
| 23 |
.. image:: https://img.shields.io/badge/code%20style-pep257-brightgreen.svg |
| 24 |
:target: https://www.python.org/dev/peps/pep-0257/ |
| 25 |
|
| 26 |
.. image:: https://img.shields.io/badge/linter-pylint-%231674b1?style=flat |
| 27 |
:target: https://www.pylint.org/ |
| 28 |
|
| 29 |
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg |
| 30 |
:target: https://github.com/psf/black |
| 31 |
|
| 32 |
.. image:: https://img.shields.io/github/contributors/c0fec0de/anytree.svg |
| 33 |
:target: https://github.com/c0fec0de/anytree/graphs/contributors/ |
| 34 |
|
| 35 |
.. image:: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square |
| 36 |
:target: http://makeapullrequest.com |
| 37 |
|
| 38 |
.. image:: https://img.shields.io/github/issues-pr/c0fec0de/anytree.svg |
| 39 |
:target: https://github.com/c0fec0de/anytree/pulls |
| 40 |
|
| 41 |
.. image:: https://img.shields.io/github/issues-pr-closed/c0fec0de/anytree.svg |
| 42 |
:target: https://github.com/c0fec0de/anytree/pulls?q=is%3Apr+is%3Aclosed |
| 43 |
|
| 44 |
|
| 45 |
Links |
| 46 |
===== |
| 47 |
|
| 48 |
* Documentation_ |
| 49 |
* PyPI_ |
| 50 |
* GitHub_ |
| 51 |
* Changelog_ |
| 52 |
* Issues_ |
| 53 |
* Contributors_ |
| 54 |
* If you enjoy anytree_ |
| 55 |
|
| 56 |
.. image:: https://github.com/c0fec0de/anytree/raw/devel/docs/static/buymeacoffee.png |
| 57 |
:target: https://www.buymeacoffee.com/1oYX0sw |
| 58 |
|
| 59 |
.. _anytree: https://anytree.readthedocs.io/en/stable/ |
| 60 |
.. _Documentation: https://anytree.readthedocs.io/en/stable/ |
| 61 |
.. _PyPI: https://pypi.org/project/anytree |
| 62 |
.. _GitHub: https://github.com/c0fec0de/anytree |
| 63 |
.. _Changelog: https://github.com/c0fec0de/anytree/releases |
| 64 |
.. _Issues: https://github.com/c0fec0de/anytree/issues |
| 65 |
.. _Contributors: https://github.com/c0fec0de/anytree/graphs/contributors |
| 66 |
|
| 67 |
.. _Node: https://anytree.readthedocs.io/en/stable/api/anytree.node.html#anytree.node.node.Node |
| 68 |
.. _RenderTree: https://anytree.readthedocs.io/en/stable/api/anytree.render.html#anytree.render.RenderTree |
| 69 |
.. _UniqueDotExporter: https://anytree.readthedocs.io/en/stable/exporter/dotexporter.html#anytree.exporter.dotexporter.UniqueDotExporter |
| 70 |
.. _NodeMixin: https://anytree.readthedocs.io/en/stable/api/anytree.node.html#anytree.node.nodemixin.NodeMixin |
| 71 |
.. _Importers: https://anytree.readthedocs.io/en/stable/importer.html |
| 72 |
.. _Exporters: https://anytree.readthedocs.io/en/stable/exporter.html |
| 73 |
|
| 74 |
Getting started |
| 75 |
--------------- |
| 76 |
|
| 77 |
.. _getting_started: |
| 78 |
|
| 79 |
Usage is simple. |
| 80 |
|
| 81 |
**Construction** |
| 82 |
|
| 83 |
>>> from anytree import Node, RenderTree |
| 84 |
>>> udo = Node("Udo") |
| 85 |
>>> marc = Node("Marc", parent=udo) |
| 86 |
>>> lian = Node("Lian", parent=marc) |
| 87 |
>>> dan = Node("Dan", parent=udo) |
| 88 |
>>> jet = Node("Jet", parent=dan) |
| 89 |
>>> jan = Node("Jan", parent=dan) |
| 90 |
>>> joe = Node("Joe", parent=dan) |
| 91 |
|
| 92 |
**Node** |
| 93 |
|
| 94 |
>>> print(udo) |
| 95 |
Node('/Udo') |
| 96 |
>>> print(joe) |
| 97 |
Node('/Udo/Dan/Joe') |
| 98 |
|
| 99 |
**Tree** |
| 100 |
|
| 101 |
>>> for pre, fill, node in RenderTree(udo): |
| 102 |
... print("%s%s" % (pre, node.name)) |
| 103 |
Udo |
| 104 |
├── Marc |
| 105 |
│ └── Lian |
| 106 |
└── Dan |
| 107 |
├── Jet |
| 108 |
├── Jan |
| 109 |
└── Joe |
| 110 |
|
| 111 |
For details see Node_ and RenderTree_. |
| 112 |
|
| 113 |
**Visualization** |
| 114 |
|
| 115 |
>>> from anytree.exporter import UniqueDotExporter |
| 116 |
>>> # graphviz needs to be installed for the next line! |
| 117 |
>>> UniqueDotExporter(udo).to_picture("udo.png") |
| 118 |
|
| 119 |
.. image:: https://anytree.readthedocs.io/en/latest/_images/udo.png |
| 120 |
|
| 121 |
The UniqueDotExporter_ can be started at any node and has various formatting hookups: |
| 122 |
|
| 123 |
>>> UniqueDotExporter(dan, |
| 124 |
... nodeattrfunc=lambda node: "fixedsize=true, width=1, height=1, shape=diamond", |
| 125 |
... edgeattrfunc=lambda parent, child: "style=bold" |
| 126 |
... ).to_picture("dan.png") |
| 127 |
|
| 128 |
.. image:: https://anytree.readthedocs.io/en/latest/_images/dan.png |
| 129 |
|
| 130 |
There are various other Importers_ and Exporters_. |
| 131 |
|
| 132 |
**Manipulation** |
| 133 |
|
| 134 |
A second tree: |
| 135 |
|
| 136 |
>>> mary = Node("Mary") |
| 137 |
>>> urs = Node("Urs", parent=mary) |
| 138 |
>>> chris = Node("Chris", parent=mary) |
| 139 |
>>> marta = Node("Marta", parent=mary) |
| 140 |
>>> print(RenderTree(mary)) |
| 141 |
Node('/Mary') |
| 142 |
├── Node('/Mary/Urs') |
| 143 |
├── Node('/Mary/Chris') |
| 144 |
└── Node('/Mary/Marta') |
| 145 |
|
| 146 |
Append: |
| 147 |
|
| 148 |
>>> udo.parent = mary |
| 149 |
>>> print(RenderTree(mary)) |
| 150 |
Node('/Mary') |
| 151 |
├── Node('/Mary/Urs') |
| 152 |
├── Node('/Mary/Chris') |
| 153 |
├── Node('/Mary/Marta') |
| 154 |
└── Node('/Mary/Udo') |
| 155 |
├── Node('/Mary/Udo/Marc') |
| 156 |
│ └── Node('/Mary/Udo/Marc/Lian') |
| 157 |
└── Node('/Mary/Udo/Dan') |
| 158 |
├── Node('/Mary/Udo/Dan/Jet') |
| 159 |
├── Node('/Mary/Udo/Dan/Jan') |
| 160 |
└── Node('/Mary/Udo/Dan/Joe') |
| 161 |
|
| 162 |
Subtree rendering: |
| 163 |
|
| 164 |
>>> print(RenderTree(marc)) |
| 165 |
Node('/Mary/Udo/Marc') |
| 166 |
└── Node('/Mary/Udo/Marc/Lian') |
| 167 |
|
| 168 |
Cut: |
| 169 |
|
| 170 |
>>> dan.parent = None |
| 171 |
>>> print(RenderTree(dan)) |
| 172 |
Node('/Dan') |
| 173 |
├── Node('/Dan/Jet') |
| 174 |
├── Node('/Dan/Jan') |
| 175 |
└── Node('/Dan/Joe') |
| 176 |
|
| 177 |
**Extending any python class to become a tree node** |
| 178 |
|
| 179 |
The entire tree magic is encapsulated by NodeMixin_ |
| 180 |
add it as base class and the class becomes a tree node: |
| 181 |
|
| 182 |
>>> from anytree import NodeMixin, RenderTree |
| 183 |
>>> class MyBaseClass(object): # Just an example of a base class |
| 184 |
... foo = 4 |
| 185 |
>>> class MyClass(MyBaseClass, NodeMixin): # Add Node feature |
| 186 |
... def __init__(self, name, length, width, parent=None, children=None): |
| 187 |
... super(MyClass, self).__init__() |
| 188 |
... self.name = name |
| 189 |
... self.length = length |
| 190 |
... self.width = width |
| 191 |
... self.parent = parent |
| 192 |
... if children: |
| 193 |
... self.children = children |
| 194 |
|
| 195 |
Just set the `parent` attribute to reflect the tree relation: |
| 196 |
|
| 197 |
>>> my0 = MyClass('my0', 0, 0) |
| 198 |
>>> my1 = MyClass('my1', 1, 0, parent=my0) |
| 199 |
>>> my2 = MyClass('my2', 0, 2, parent=my0) |
| 200 |
|
| 201 |
>>> for pre, fill, node in RenderTree(my0): |
| 202 |
... treestr = u"%s%s" % (pre, node.name) |
| 203 |
... print(treestr.ljust(8), node.length, node.width) |
| 204 |
my0 0 0 |
| 205 |
├── my1 1 0 |
| 206 |
└── my2 0 2 |
| 207 |
|
| 208 |
The `children` can be used likewise: |
| 209 |
|
| 210 |
>>> my0 = MyClass('my0', 0, 0, children=[ |
| 211 |
... MyClass('my1', 1, 0), |
| 212 |
... MyClass('my2', 0, 2), |
| 213 |
... ]) |
| 214 |
|
| 215 |
>>> for pre, fill, node in RenderTree(my0): |
| 216 |
... treestr = u"%s%s" % (pre, node.name) |
| 217 |
... print(treestr.ljust(8), node.length, node.width) |
| 218 |
my0 0 0 |
| 219 |
├── my1 1 0 |
| 220 |
└── my2 0 2 |
| 221 |
|
| 222 |
Documentation |
| 223 |
------------- |
| 224 |
|
| 225 |
Please see the Documentation_ for all details. |
| 226 |
|
| 227 |
Installation |
| 228 |
------------ |
| 229 |
|
| 230 |
To install the `anytree` module run:: |
| 231 |
|
| 232 |
pip install anytree |
| 233 |
|
| 234 |
If you do not have write-permissions to the python installation, try:: |
| 235 |
|
| 236 |
pip install anytree --user |