clitree is a library to draw tree structures like the CLI utility tree does.
Install it with:
pip install clitreeExample usage:
>>> from clitree import tree
>>>
>>> data = {
... "name": "root",
... "children": [
... {"name": "docs", "children": [
... {"name": "api.md", "children": []},
... {"name": "guide.md", "children": []}
... ]},
... {"name": "src", "children": [
... {"name": "main.py", "children": []},
... {"name": "utils", "children": [
... {"name": "helpers.py", "children": []},
... {"name": "config.py", "children": []}
... ]}
... ]},
... {"name": "tests", "children": []}
... ]
... }
>>>
>>> print(tree(data))
root
├── docs
│ ├── api.md
│ └── guide.md
├── src
│ ├── main.py
│ └── utils
│ ├── helpers.py
│ └── config.py
└── testsname: How to extract the name of a node. Can be:- A string key (uses
node.get(name)orgetattr(node, name)) - A callable that returns a string
- A string key (uses
children: How to obtain the children of a node. Can be:- A string key (uses
node.get(children)orgetattr(node, children)) - A callable that returns an iterable of nodes (including generators)
- Returns
Noneor empty iterable for leaf nodes
- A string key (uses
When using string keys for name and children, nodes are typically:
- Dictionaries with a
getmethod, or - Objects with attributes accessible via
getattr
However, since name and children can be callables, nodes can be of any type, as the callables are responsible for extracting the required information.
Requirements:
- The name must resolve to a string value
- Children must be iterable or None
ValueError: Raised when a node's name resolves to NoneTypeError: Raised when children are not iterable