Skip to main content

A library to draw tree structures like the CLI utility 'tree' does

Project description

CliTree

PyPI version Python Version License: MIT CI/CD Ruff

clitree is a library to draw tree structures like the CLI utility tree does.

Install it with:

pip install clitree

Example 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
└── 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clitree-0.1.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

clitree-0.1.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file clitree-0.1.0.tar.gz.

File metadata

  • Download URL: clitree-0.1.0.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for clitree-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9174bcb71bd18e2601770903dcae89753b271cc425424ae3b124815b86d4dff6
MD5 e5d302d8f611dfd37bb979f13800782b
BLAKE2b-256 b8901cda0dd9ce23cffcaab78abf1c260a42af1a91c1bfdd253e19ce0d5b9991

See more details on using hashes here.

File details

Details for the file clitree-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: clitree-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for clitree-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a62bda59ffe29e81effd22f6765a999eda6289592709db73d9c9e6596017c91d
MD5 50d9a57eacaf9d5db2353021dec2234c
BLAKE2b-256 d3152fcd3573014aa5124d7c7a149b365a3cb6d74e0b917641abb6f2d7f33b49

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page