用于从嵌套的Python对象(字典)中创建树图的Python库 [英] Python library for creating tree graphs out of nested Python objects (dicts)

查看:70
本文介绍了用于从嵌套的Python对象(字典)中创建树图的Python库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人知道任何Python库,这些库可让您简单快速地将嵌套到任意级别的对象馈入该对象,例如,沿着您在

Does anyone know of any Python libraries that allow you to simply and quickly feed it an object nested to arbitrary levels, like for example a dict tree along the lines of what you'd find in this gist, and it can spit out a workable tree graph file?

在这里,简单是关键,因为我必须能够与没有技术头脑的人一起工作.

Simplicity is key, here, since I have to be able to work with people who are not technically minded.

图树"的含义类似于以下内容,在该图中可以为它提供一个嵌套的值字典,然后它将创建树结构:

What I mean by "graph tree" is something along the lines of the following, where I could feed it a nested dictionary of values and it would then create the tree structure:


(来源:
ruby​​forge.org )


(source: rubyforge.org)

推荐答案

因此,我在此答案中为我的代码段推荐并使用的库是 not 的python库,但它是python-友好的库,通过这个库,我的意思是可以将使用该库的代码插入python模块中以处理数据,并且此 foreign 代码将在两端连接到现有的python代码,即输入和输出,我怀疑,尽管我当然不知道,但这就是"python库"标准的全部含义.因此,如果您正在编写Web应用程序,则此代码将是客户端代码.换句话说,该库不是python,但可与python一起使用.

so the library i recommend and use for my code snippet in this answer is not a python library, but it is a python-friendly library, by which i mean that code using this library can be inserted into a python module for processing data and this foreign code will connect to extant python code on both ends, ie, both input and output, and i suspect, although of course i don't know, that's all that's really meant by the "python library" criterion. So if you are writing a web app, this code would be client-side. In other words, this library is not python, but it works with python.

  1. 其输入是(几乎)原始python字典,更具体地说, json.load(a_python_dict)返回一个json数组或对象,该格式为javascript库当然可以识别;和

  1. its input is (nearly) raw python dicts, more specifically, json.load(a_python_dict) returns a json array or object, a format which this javascript library can of course recognize; and

输出格式是HTML或SVG,而不是某些对象 特定于语言的格式

the output format is either HTML or SVG, not objects in some language-specific format

您可以使用 d3.js .它具有一个,专门用于渲染树:

You can use d3.js. It has a class specifically for rendering trees:

var tree = d3.layout.tree().size([h, w]);

d3 源中的 example文件夹中,还有几个树(工作代码)示例,您可以从我提供的链接中克隆/下载它们.以上.

There is also a couple of examples of trees (working code) in the example folder in the d3 source, which you can clone/download form the link i provided above.

由于d3是一个javascript库,因此其本机数据格式为 JSON .

Because d3 is a javascript library, its native data format is JSON.

基本结构是嵌套字典,每个字典代表具有两个值的单个节点,该节点的名称及其子代(存储在数组中),键为 名称 孩子 :

The basic structure is a nested dictionary, each dictionary representing a single node with two values, the node's name and its children (stored in an array), keyed to names and children, respectively:

{"name": "a_root_node", "children": ["B", "C"]}

当然,在python字典和JSON之间进行转换很简单:

and of course it's simple to convert between python dictionaries and JSON:

>>> d = {"name": 'A', "children": ['B', 'C']}
>>> import json as JSON
>>> dj = JSON.dumps(d)
>>> dj
    '{"name": "A", "children": ["B", "C"]}'

这是一棵更大的树(大约十二个节点)的python dictionary 表示,我如上所述将其转换为json,然后在 d3 中呈现为所示的树在下图中:

here's a python dictionary representation of a larger tree (a dozen or so nodes) which i converted to json as above, and then rendered in d3 as the tree shown in the image below:

tree = {'name': 'root', 'children': [{'name': 'node 2', 'children': 
       [{'name': 'node 4', 'children': [{'name': 'node 10', 'size': 7500}, 
       {'name': 'node 11', 'size': 12000}]}, {'name': 'node 5', 'children': 
       [{'name': 'node 12', 'children': [{'name': 'node 16', 'size': 10000}, 
       {'name': 'node 17', 'size': 12000}]}, {'name': 'node 13', 'size': 5000}]}]}, 
       {'name': 'node 3', 'children': [{'name': 'node 6', 'children': 
       [{'name': 'node 14', 'size': 8000}, {'name': 'node 15', 'size': 9000}]}, 
       {'name': 'node 7', 'children': [{'name': 'node 8', 'size': 10000}, 
       {'name': 'node 9', 'size': 12000}]}]}]}

note :d3在浏览器中渲染;上图只是我的浏览器窗口的屏幕截图.

note: d3 renders in the browser; the image above is just a screen shot of my browser window.

这篇关于用于从嵌套的Python对象(字典)中创建树图的Python库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆