https://github.com/jdevera/python-clitree
A Python library to draw tree structures like the CLI utility `tree` does.
https://github.com/jdevera/python-clitree
Last synced: 5 months ago
JSON representation
A Python library to draw tree structures like the CLI utility `tree` does.
- Host: GitHub
- URL: https://github.com/jdevera/python-clitree
- Owner: jdevera
- License: mit
- Created: 2025-06-15T11:58:13.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-15T22:40:19.000Z (about 1 year ago)
- Last Synced: 2025-10-12T13:41:43.424Z (10 months ago)
- Language: Python
- Size: 15.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CliTree
[](https://badge.fury.io/py/clitree)
[](https://www.python.org/downloads/)
[](https://opensource.org/licenses/MIT)
[](https://github.com/jdevera/python-clitree/actions/workflows/ci.yml)
[](https://github.com/astral-sh/ruff)
`clitree` is a library to draw tree structures like the CLI utility `tree` does.
Install it with:
```bash
pip install clitree
```
Example usage:
```python
>>> 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
└── tests
```
### Parameters
- `name`: How to extract the name of a node. Can be:
- A string key (uses `node.get(name)` or `getattr(node, name)`)
- A callable that returns a string
- `children`: How to obtain the children of a node. Can be:
- A string key (uses `node.get(children)` or `getattr(node, children)`)
- A callable that returns an iterable of nodes (including generators)
- Returns `None` or empty iterable for leaf nodes
### Node Structure
When using string keys for `name` and `children`, nodes are typically:
- Dictionaries with a `get` method, 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
### Error Handling
- `ValueError`: Raised when a node's name resolves to None
- `TypeError`: Raised when children are not iterable