Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/salpreh/lxtree
Package to draw tree structures
https://github.com/salpreh/lxtree
pretty-print python tree-structure
Last synced: 12 days ago
JSON representation
Package to draw tree structures
- Host: GitHub
- URL: https://github.com/salpreh/lxtree
- Owner: salpreh
- License: mit
- Created: 2019-04-14T20:32:10.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-26T20:53:57.000Z (about 2 years ago)
- Last Synced: 2025-01-01T09:16:41.000Z (20 days ago)
- Topics: pretty-print, python, tree-structure
- Language: Python
- Homepage:
- Size: 44.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lxtree
[![PyPI version](https://badge.fury.io/py/lxtree.svg)](https://badge.fury.io/py/lxtree)
[![PyPI version](https://img.shields.io/github/license/salpreh/lxtree.svg)](https://img.shields.io/github/license/salpreh/lxtree.svg)**Package to draw tree structures (tree linux command style)**
---
## Basic usage
Build the tree structure using `TreeNode`s. A `TreeNode` can contain other `TreeNode`s
to represent the tree. When builded you car get a string representation of it,
or get string of a sub-tree using some child as root.### Code samples
```py
from lxtree import TreeNode# Creating root
root = TreeNode('root')# Adding a list of nodes
root.children = [TreeNode('branch1'), TreeNode('branch3')]# Insert node
root.insert_child(TreeNode('branch2'), 1)# Appending to a branch 1 by index
root[0].append_child(TreeNode('branch11'))# Using index to assign children to branch11
root[0][0] = [TreeNode('leaf111'), TreeNode('leaf112')]# Use `set_children` to add nodes as argv
root[1].set_children(TreeNode('leaf21'), TreeNode('leaf22'), TreeNode('leaf23'))# Print tree
print(root)
```##### Creating all structure at once
```py
root = TreeNode('root').set_children(
TreeNode('branch1').append_child(
TreeNode('branch11').set_children(
TreeNode('leaf111'),
TreeNode('leaf112')
)
),
TreeNode('branch2').set_children(
TreeNode('leaf21'),
TreeNode('leaf22'),
TreeNode('leaf23')
),
TreeNode('branch3')
)print(root)
```
##### Creating the tree from a dict
```py
tree_data = {
'root': {
'branch1': {
'branch11': {
'leaf111': None,
'leaf112': None
}
},
'branch2': {
'leaf21': None,
'leaf22': None,
'leaf23': None
},
'branch3': None
}
}print(TreeNode.tree_from_dict(tree_data))
```
#### Output