您好,登錄后才能下訂單哦!
小編給大家分享一下Python如何使用graphviz畫流程圖,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
問題描述
項(xiàng)目中需要用到流程圖,如果用js的echarts處理,不同層級建動(dòng)態(tài)計(jì)算位置比較復(fù)雜,考慮用python來實(shí)現(xiàn)
測試demo
實(shí)現(xiàn)效果如下
完整代碼
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: # 執(zhí)行 stmt = ibm_db.exec_immediate(conn, sql) result = ibm_db.fetch_both(stmt) return result, stmt, conn # 添加所有的點(diǎn) 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) # 關(guān)閉數(shù)據(jù)庫連接 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) # 關(guān)閉數(shù)據(jù)庫連接 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' # 添加圓點(diǎn)A,A的標(biāo)簽是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)
測試流程圖
用不同的參數(shù),生成的2個(gè)圖如下
linux環(huán)境搭建
線上是不連外網(wǎng)的私有服務(wù)器,開始在離線環(huán)境安裝,需要依賴的包太多,后來就在虛擬機(jī)里redhat6.6在線裝好了。####redhat安裝rpm依賴
#清除原有RHEL的YUM及相關(guān)軟件包。
rpm -qa | grep yum | xargs rpm -e --nodeps
rpm -qa |grep python-urlgrabber|xargs rpm -e --nodepsmv CentOS6-Base-163.repo /etc/yum.repos.d/
#安裝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
替換yum163源
cd /etc/yum.repos.d/mv rhel-source.repo rhel-source.repo.bak
清理yum緩存
yum clean allyum makecache#測試yum repolist
安裝graphviz
yum install 'graphviz*'#測試which dot #/usr/bin/dotdot -V#dot - graphviz version 2.26.0 (20091210.2329)
查看yum的安裝日志,發(fā)現(xiàn)需要依賴2,30個(gè)包
解決依賴關(guān)系
--> 執(zhí)行事務(wù)檢查
---> Package graphviz.x86_64 0:2.26.0-10.el6 will be 安裝
--> 處理依賴關(guān)系 libgd.so.2()(64bit),它被軟件包 graphviz-2.26.0-10.el6.x86_64 需要
--> 處理依賴關(guān)系 libXpm.so.4()(64bit),它被軟件包 graphviz-2.26.0-10.el6.x86_64 需要
--> 處理依賴關(guān)系 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 安裝
--> 處理依賴關(guān)系 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 安裝
--> 處理依賴關(guān)系 php(zend-abi) = 20090626,它被軟件包 graphviz-php-2.26.0-10.el6.x86_64 需要
--> 處理依賴關(guān)系 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 安裝
--> 處理依賴關(guān)系 ruby,它被軟件包 graphviz-ruby-2.26.0-10.el6.x86_64 需要
--> 處理依賴關(guān)系 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 安裝
--> 處理依賴關(guān)系 tcl >= 8.3,它被軟件包 graphviz-tcl-2.26.0-10.el6.x86_64 需要
--> 處理依賴關(guān)系 tk,它被軟件包 graphviz-tcl-2.26.0-10.el6.x86_64 需要
--> 處理依賴關(guān)系 libtk8.5.so()(64bit),它被軟件包 graphviz-tcl-2.26.0-10.el6.x86_64 需要
--> 執(zhí)行事務(wù)檢查
---> 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 安裝
--> 處理依賴關(guān)系 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 安裝
--> 執(zhí)行事務(wù)檢查
---> Package compat-readline5.x86_64 0:5.2-17.1.el6 will be 安裝
--> 完成依賴關(guān)系計(jì)算
依賴關(guān)系解決
yum安裝openssl-devel
#查看openssl安裝包rpm -aq|grep opensslyum 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
以上是“Python如何使用graphviz畫流程圖”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。