python画流程_python用graphviz画流程图

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 19:20   2009   0

问题描述

项目中需要用到流程图,如果用js的echarts处理,不同层级建动态计算位置比较复杂,考虑用python来实现

测试demo实现效果如下

完整代码

import yaml

import os

import ibm_db

from graphviz import Digraph

from datetime import datetime

# db连接

def db2_query(sql):

conn = ibm_db.connect(

"DATABASE=%s;HOSTNAME=%s;PORT=%s;PROTOCOL=TCPIP;UID=%s;PWD=%s" % (

config['db2_databse'],

config['db2_host'],

config['db2_port'],

config['db2_user'],

config['db2_password']

), "", ""

)

if conn:

# 执行

stmt = ibm_db.exec_immediate(conn, sql)

result = ibm_db.fetch_both(stmt)

return result, stmt, conn

# 添加所有的点

def add_nodes():

sql = "SELECT * FROM AML.DEP_OFTEN_TRAD WHERE ACCOUNT_NO=" + ACCOUNT_NO + " ORDER BY NUM DESC fetch first 10 rows ONLY;"

result, stmt, conn = db2_query(sql)

nodes = []

nodes_dict = {}

nodes_dict[ACCOUNT_NO] = 'A'

count = 0

while (result):

count = count + 1

print(chr(ord('A') + count) + "," + result[3])

dot.node(chr(ord('A') + count), result[3], shape="rectangle", color="orange")

nodes.append(result[2])

nodes_dict[result[2]] = chr(ord('A') + count)

print('-----------------')

result = ibm_db.fetch_both(stmt)

# 关闭数据库连接

ibm_db.close(conn)

nodes.append(ACCOUNT_NO)

return dot, nodes, nodes_dict

# 添加所有的边

def add_edges(nodes):

nodes_back = nodes

print(nodes_back)

for p in range(len(nodes_back)):

nodes_temp = nodes.copy()

from_nodes = nodes_temp.pop(p)

print("len of nodes_temp:" + str(len(nodes_temp)) + ",from_nodes:" + from_nodes)

nodes_temp_in = ",".join(nodes_temp)

nodes_temp_in = '(' + nodes_temp_in + ')'

sql = "SELECT * FROM AML.DEP_OFTEN_TRAD WHERE ACCOUNT_NO=" + from_nodes + " and CNTPRT_ACCOUNT_NO IN" + \

nodes_temp_in

result, stmt, conn = db2_query(sql)

if (result):

while (result):

print('-----------------')

print(result)

print(result['CNTPRT_ACCOUNT_NO'] + nodes_dict[result['CNTPRT_ACCOUNT_NO']])

print("add edges," + result['TRAD_VAL'] + " from:" + from_nodes + ",to:" + result['CNTPRT_ACCOUNT_NO'])

dot.edge(nodes_dict[from_nodes], nodes_dict[result['CNTPRT_ACCOUNT_NO']], result['TRAD_VAL'])

result = ibm_db.fetch_both(stmt)

# 关闭数据库连接

ibm_db.close(conn)

return dot

if __name__ == "__main__":

yaml_path = os.path.join('../', 'config.yaml')

with open(yaml_path, 'r') as f:

config = yaml.load(f)

dot = Digraph(

engine="circo",

comment='The Test Table',

format="png"

)

ACCOUNT_NO = '10100002181'

# 添加圆点A,A的标签是Dot A

dot.node('A', '中心客户', shape="rectangle", color="blue")

nodes_begin = datetime.now()

print(str(nodes_begin) + " nodes_begin")

dot, nodes, nodes_dict = add_nodes()

print(nodes_dict)

edges_begin = datetime.now()

print(str(edges_begin) + " edges_begin")

dot = add_edges(nodes)

print(dot.source)

render_begin = datetime.now()

print(str(render_begin) + " render_begin")

dot.render('./file/db2-table.gv', view=True)

测试流程图

用不同的参数,生成的2个图如下

linux环境搭建

线上是不连外网的私有服务器,开始在离线环境安装,需要依赖的包太多,后来就在虚拟机里redhat6.6在线装好了。

redhat安装rpm依赖

#清除原有RHEL的YUM及相关软件包。

rpm -qa | grep yum | xargs rpm -e --nodeps

rpm -qa |grep python-urlgrabber|xargs rpm -e --nodeps

#安装rpm依赖包

rpm -ivh python-iniparse-0.3.1-2.1.el6.noarch.rpm

rpm -ivh python-urlgrabber-3.9.1-11.el6.noarch.rpm

rpm -ivh yum-metadata-parser-1.1.2-16.el6.x86_64.rpm

rpm -ivh yum-plugin-fastestmirror-1.1.30-41.el6.noarch.rpm yum-3.2.29-81.el6.centos.noarch.rpm

mv CentOS6-Base-163.repo /etc/yum.repos.d/

替换yum163源

cd /etc/yum.repos.d/

mv rhel-source.repo rhel-source.repo.bak

清理yum缓存

yum clean all

yum makecache

#测试

yum repolist

安装graphviz

yum install 'graphviz*'

#测试

which dot

#/usr/bin/dot

dot -V

#dot - graphviz version 2.26.0 (20091210.2329)

查看yum的安装日志,发现需要依赖2,30个包

解决依赖关系

--> 执行事务检查

---> Package graphviz.x86_64 0:2.26.0-10.el6 will be 安装

--> 处理依赖关系 libgd.so.2()(64bit),它被软件包 graphviz-2.26.0-10.el6.x86_64 需要

--> 处理依赖关系 libXpm.so.4()(64bit),它被软件包 graphviz-2.26.0-10.el6.x86_64 需要

--> 处理依赖关系 libXaw.so.7()(64bit),它被软件包 graphviz-2.26.0-10.el6.x86_64 需要

---> Package graphviz-devel.x86_64 0:2.26.0-10.el6 will be 安装

---> Package graphviz-doc.x86_64 0:2.26.0-10.el6 will be 安装

---> Package graphviz-gd.x86_64 0:2.26.0-10.el6 will be 安装

---> Package graphviz-graphs.x86_64 0:2.26.0-10.el6 will be 安装

---> Package graphviz-guile.x86_64 0:2.26.0-10.el6 will be 安装

--> 处理依赖关系 guile,它被软件包 graphviz-guile-2.26.0-10.el6.x86_64 需要

---> Package graphviz-java.x86_64 0:2.26.0-10.el6 will be 安装

---> Package graphviz-lua.x86_64 0:2.26.0-10.el6 will be 安装

---> Package graphviz-perl.x86_64 0:2.26.0-10.el6 will be 安装

---> Package graphviz-php.x86_64 0:2.26.0-10.el6 will be 安装

--> 处理依赖关系 php(zend-abi) = 20090626,它被软件包 graphviz-php-2.26.0-10.el6.x86_64 需要

--> 处理依赖关系 php(api) = 20090626,它被软件包 graphviz-php-2.26.0-10.el6.x86_64 需要

---> Package graphviz-python.x86_64 0:2.26.0-10.el6 will be 安装

---> Package graphviz-ruby.x86_64 0:2.26.0-10.el6 will be 安装

--> 处理依赖关系 ruby,它被软件包 graphviz-ruby-2.26.0-10.el6.x86_64 需要

--> 处理依赖关系 libruby.so.1.8()(64bit),它被软件包 graphviz-ruby-2.26.0-10.el6.x86_64 需要

---> Package graphviz-tcl.x86_64 0:2.26.0-10.el6 will be 安装

--> 处理依赖关系 tcl >= 8.3,它被软件包 graphviz-tcl-2.26.0-10.el6.x86_64 需要

--> 处理依赖关系 tk,它被软件包 graphviz-tcl-2.26.0-10.el6.x86_64 需要

--> 处理依赖关系 libtk8.5.so()(64bit),它被软件包 graphviz-tcl-2.26.0-10.el6.x86_64 需要

--> 执行事务检查

---> Package gd.x86_64 0:2.0.35-11.el6 will be 安装

---> Package guile.x86_64 5:1.8.7-5.el6 will be 安装

---> Package libXaw.x86_64 0:1.0.11-2.el6 will be 安装

---> Package libXpm.x86_64 0:3.5.10-2.el6 will be 安装

---> Package php-common.x86_64 0:5.3.3-49.el6 will be 安装

---> Package ruby.x86_64 0:1.8.7.374-5.el6 will be 安装

---> Package ruby-libs.x86_64 0:1.8.7.374-5.el6 will be 安装

--> 处理依赖关系 libreadline.so.5()(64bit),它被软件包 ruby-libs-1.8.7.374-5.el6.x86_64 需要

---> Package tcl.x86_64 1:8.5.7-6.el6 will be 安装

---> Package tk.x86_64 1:8.5.7-5.el6 will be 安装

--> 执行事务检查

---> Package compat-readline5.x86_64 0:5.2-17.1.el6 will be 安装

--> 完成依赖关系计算

依赖关系解决

yum安装openssl-devel

#查看openssl安装包

rpm -aq|grep openssl

yum install openssl-devel -y

查看yum依赖

正在升级 : libcom_err-1.41.12-24.el6.x86_64 1/26

正在升级 : libselinux-2.0.94-7.el6.x86_64 2/26

正在升级 : krb5-libs-1.10.3-65.el6.x86_64 3/26

****************

Verifying : e2fsprogs-libs-1.41.12-21.el6.x86_64 23/26

Verifying : libselinux-2.0.94-5.8.el6.x86_64 24/26

Verifying : krb5-libs-1.10.3-33.el6.x86_64 25/26

Verifying : libss-1.41.12-21.el6.x86_64 26/26

python安装pip依赖

pip install -r requirement.txt

#Installing collected packages: PyYAML, ibm-db, tornado, graphviz

# Running setup.py install for PyYAML ... done

# Running setup.py install for ibm-db ... done

# Running setup.py install for tornado ... done

#Successfully installed PyYAML-5.1.1 graphviz-0.11.1 ibm-db-3.0.1 tornado-6.0.3

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP