Giter VIP home page Giter VIP logo

pytreemap's Introduction

树图渲染命令行工具 PyPI version License: MIT

pytreemap 封装了 antvis/g6,利用 JSON 数据生成 HTML 格式的树图

安装

pip 安装

$ pip install pytm-cli

源码安装

 $ git clone https://github.com/chenjiandongx/pytreemap.git
 $ cd pytreemap
 $ pip install -r requirements.txt
 $ python setup.py install

使用

命令行参数

C:\Users\chenjiandongx>pytm-cli
uusage: pytm-cli [-i INPUT] [-o OUTPUT] [-d DIRECTION] [-t TYPE]
                    [-I INDENT] [-v] [-h]

树图渲染命令行工具-利用 JSON 数据生成 HTML 格式的树图

optional arguments:
  -i INPUT, --input INPUT
                        JSON 数据路径.
  -o OUTPUT, --output OUTPUT
                        输出 HTML 文件路径.(默认为`.\TreeMap.html`)
  -d DIRECTION, --direction DIRECTION
                        树图的布局方向, 有 LR/RL/H/TB/BT/V 可选.(默认为 LR)
  -t TYPE, --type TYPE  树图类型, 1.分层树 2.缩进树 3.生态树.(默认为 1)
  -I INDENT, --indent INDENT
                        缩进树的缩进量.(默认为 40)
  -v, --version         版本信息
  -h, --help            帮助页面

JSON 数据

首先假设你有一份数据需要生产树图,大概长这样

     |----B     |----E----|----I
     |          |
     |----C-----|----F         |----J
A----|                         |
     |----D-----|----G----|----|----K
                |
                |----H

这时候思路就很清晰了,你需要来编写成 JSON 数据了,节点都是以 {name, children} 为基础的递归嵌套模式,如下

{
    "children": [
        {
            "children": [],
            "name": "B"
        },
        {
            "children": [
                {
                    "children": [
                        {
                            "children": [],
                            "name": "I"
                        }
                    ],
                    "name": "E"
                },
                {
                    "children": [],
                    "name": "F"
                }
            ],
            "name": "C"
        },
        {
            "children": [
                {
                    "children": [
                        {
                            "children": [],
                            "name": "J"
                        },
                        {
                            "children": [],
                            "name": "K"
                        }
                    ],
                    "name": "G"
                },
                {
                    "children": [],
                    "name": "H"
                }
            ],
            "name": "D"
        }
    ],
    "name": "A"
}

怎么样,结构很清晰吧,将文件保存为 json 格式,如 data.json

生成树图

接下来之执行 pytm-cli -i data.json -o demo.html,然后用浏览器打开根目录下的 demo.html 文件,就可以看到已经生产了这样的一张图了

当指定 -t/--type 参数分别为 2 3 时,得到的图是这样的

type 为 2

type 为 3

其余参数尝试一下就知道效果了,这里提供官网提供的 数据 生产的效果

pytm-cli -i data.json

pytm-cli -i data.json -d H

pytm-cli -i data.json -t 3

pytm-cli -i data.json -t 2 -I 100

以模块方式使用

from pytreemap import render

render(input,
       output="TreeMap.html",
       direction="LR",
       indent=40,
       type=None):

:param input: 输入 json 文件路径
:param direction: 树图方向 LR/RL/H/TB/BT/V 可选
:param output: 输出 html 文件路径
:param indent: 缩进树缩进量
:param type: 树图类型

pytreemap's People

Contributors

chenjiandongx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pytreemap's Issues

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 354389: ordinal not in range(128)

pytm-cli -i data.json
Traceback (most recent call last):
File "/usr/local/bin/pytm-cli", line 11, in
load_entry_point('pytm-cli==0.0.2', 'console_scripts', 'pytm-cli')()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 489, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 2843, in load_entry_point
return ep.load()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 2434, in load
return self.resolve()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 2440, in resolve
module = import(self.module_name, fromlist=['name'], level=0)
File "/Users/zhongjianpeng/Library/Python/2.7/lib/python/site-packages/pytreemap.py", line 9, in
TPL = Environment().from_string(TEMPLATE)
File "/Users/zhongjianpeng/Library/Python/2.7/lib/python/site-packages/jinja2/environment.py", line 941, in from_string
return cls.from_code(self, self.compile(source), globals, None)
File "/Users/zhongjianpeng/Library/Python/2.7/lib/python/site-packages/jinja2/environment.py", line 628, in compile
source = self._parse(source, name, filename)
File "/Users/zhongjianpeng/Library/Python/2.7/lib/python/site-packages/jinja2/environment.py", line 539, in _parse
return Parser(self, source, name, encode_filename(filename)).parse()
File "/Users/zhongjianpeng/Library/Python/2.7/lib/python/site-packages/jinja2/parser.py", line 45, in init
self.stream = environment._tokenize(source, name, filename, state)
File "/Users/zhongjianpeng/Library/Python/2.7/lib/python/site-packages/jinja2/environment.py", line 572, in _tokenize
source = self.preprocess(source, name, filename)
File "/Users/zhongjianpeng/Library/Python/2.7/lib/python/site-packages/jinja2/environment.py", line 565, in preprocess
text_type(source),
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 354389: ordinal not in range(128)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.