跳转到主要内容

Graphviz的简单Python接口

项目描述

Latest PyPI Version License Supported Python Versions Wheel format Monthly downloads

Build Codecov Readthedocs (stable) Readthedocs (latest)

Binder (stable)

此软件包简化了从Python创建和渲染Graphviz图形绘制软件(DOT语言)的图形描述的过程。

创建一个图形对象,通过添加节点和边来组装图形,并检索其DOT源代码字符串。将源代码保存到文件,并用系统中的Graphviz安装进行渲染。

使用 view 选项/方法直接使用默认应用程序检查生成的(PDF、PNG、SVG等)文件。图表也可以在 Jupyter notebooks(以前称为 IPython notebooks示例nbviewer)以及 Jupyter QtConsole 中渲染和显示。

安装

此软件包在Python 3.8+上运行,使用 pip 安装

$ pip install graphviz

要渲染生成的DOT源代码,您还需要安装 Graphviz下载页面存档版本Windows安装步骤)。

请确保包含 dot 可执行文件的目录在您的系统 PATH 上(有时由安装程序完成;在 LinuxMac,和 Windows 上设置 PATH)。

Anaconda:请参阅 conda-forge 软件包 conda-forge/python-graphvizfeedstock),它应该自动 conda install conda-forge/graphvizfeedstock)作为依赖项。

快速入门

创建一个图对象

>>> import graphviz  # doctest: +NO_EXE
>>> dot = graphviz.Digraph(comment='The Round Table')
>>> dot  #doctest: +ELLIPSIS
<graphviz.graphs.Digraph object at 0x...>

添加节点和边

>>> dot.node('A', 'King Arthur')  # doctest: +NO_EXE
>>> dot.node('B', 'Sir Bedevere the Wise')
>>> dot.node('L', 'Sir Lancelot the Brave')

>>> dot.edges(['AB', 'AL'])
>>> dot.edge('B', 'L', constraint='false')

检查生成的源代码

>>> print(dot.source)  # doctest: +NORMALIZE_WHITESPACE +NO_EXE
// The Round Table
digraph {
    A [label="King Arthur"]
    B [label="Sir Bedevere the Wise"]
    L [label="Sir Lancelot the Brave"]
    A -> B
    A -> L
    B -> L [constraint=false]
}

保存并渲染源代码(跳过/忽略任何 doctest_mark_exe() 行)

>>> doctest_mark_exe()  # skip this line

>>> dot.render('doctest-output/round-table.gv').replace('\\', '/')
'doctest-output/round-table.gv.pdf'

保存并渲染并查看结果

>>> doctest_mark_exe()  # skip this line

>>> dot.render('doctest-output/round-table.gv', view=True)  # doctest: +SKIP
'doctest-output/round-table.gv.pdf'
round-table.svg

注意:反斜杠转义符和 <...> 形式的字符串在DOT语言中有特殊含义。如果您需要渲染任意字符串(例如,来自用户输入),请参阅 用户指南 中的详细信息。

另请参阅

许可证

此软件包遵循 MIT许可

开发

项目详情


下载文件

下载适用于您平台的文件。如果您不确定该选择哪个,请了解更多关于安装包的信息。

源分布

graphviz-0.20.3.zip (256.5 kB 查看哈希值)

上传时间

构建分布

graphviz-0.20.3-py3-none-any.whl (47.1 kB 查看哈希值)

上传时间 Python 3

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面