项目作者: tsupei

项目描述 :
Lite python package for plotting tree stucture in bash manner
高级语言: Python
项目地址: git://github.com/tsupei/treeviz.git
创建时间: 2019-11-01T07:43:40Z
项目社区:https://github.com/tsupei/treeviz

开源协议:MIT License

下载


treeviz

Visualize tree structure in bash manner using python

install

  1. pip install treeviz

Structure

  1. .
  2. ├── MANIFEST
  3. ├── MANIFEST.in
  4. ├── README.md
  5. ├── setup.py
  6. ├── test.py
  7. └── treeviz
  8. ├── __init__.py
  9. └── treeviz.py
  10. # showcases of usage
  11. python test.py

Usage

Create Your tree structure

  1. from treeviz.treeviz import Node
  2. root = Node("Jason")
  3. child1 = Node("Mary")
  4. child2 = Node("John")
  5. grandson1 = Node("Kevin")
  6. grandson2 = Node("Doris")
  7. grandson3 = Node("James")
  8. grandson4 = Node("Momo")
  9. grandgrandson1 = Node("Baby")
  10. root.add_child(child1)
  11. root.add_child(child2)
  12. child1.add_child(grandson1)
  13. child1.add_child(grandson2)
  14. child2.add_child(grandson3)
  15. child2.add_child(grandson4)
  16. grandson1.add_child(grandgrandson1)
  17. root.visualize()
  1. Jason
  2. ├── Mary
  3. ├── Kevin
  4. └── Baby
  5. └── Doris
  6. └── John
  7. ├── James
  8. └── Momo

You can also print the sub-tree structure

  1. child1.visualize()
  1. Mary
  2. ├── Kevin
  3. └── Baby
  4. └── Doris

Two options are provided, to print in terminal or print to file

  1. # print to file
  2. # Giving path as the first parameter
  3. root.visualize(".")
  4. # If path is empty, then print to terminal
  5. root.visualize()
  6. # Default name is treeviz.txt, if the filename already exists, a number will be appended to it.

New Features

(2019.11.1) Adding max_len parameter to visualize(), which splits the message into multiple lines.

  1. from treeviz.treeviz import Node
  2. root = Node("Jason is our grandfather")
  3. child1 = Node("Mary is Kevin's mother")
  4. child2 = Node("John is James and Momo's father")
  5. grandson1 = Node("Kevin")
  6. grandson2 = Node("Doris")
  7. grandson3 = Node("James")
  8. grandson4 = Node("Momo")
  9. grandgrandson1 = Node("Baby")
  10. root.add_child(child1)
  11. root.add_child(child2)
  12. child1.add_child(grandson1)
  13. child1.add_child(grandson2)
  14. child2.add_child(grandson3)
  15. child2.add_child(grandson4)
  16. grandson1.add_child(grandgrandson1)
  17. root.visualize(max_len=10)
  1. Jason is our grandfather
  2. ├── Mary is Ke
  3. vin's moth
  4. │ er
  5. │ ├── Kevin
  6. │ │ └── Baby
  7. │ └── Doris
  8. └── John is Ja
  9. mes and Mo
  10. mo's fathe
  11. r
  12. ├── James
  13. └── Momo

(2019.11.2) Adding line_space parameter to visualize(), which enables to set space between each branch

  1. root.visualize(line_space=2)
  1. Jason is our grandfather
  2. ├── Mary is Kevin's mother
  3. │ │
  4. │ │
  5. │ ├── Kevin
  6. │ │ │
  7. │ │ │
  8. │ │ └── Baby
  9. │ │
  10. │ │
  11. │ └── Doris
  12. └── John is James and Momo's father
  13. ├── James
  14. └── Momo

(2019.11.4) Adding ‘filename’ parameter to visualize, which enables users to defince own filename