This article is for people who want to edit a network diagram made with python with draw.io. (1) Create dot format data that is the basis of the graph with networkx, ② Output it in the format (xlm) that can be used with Draw.io using graphviz2drawio.
So far, I just said that I was able to do it for the time being, but since I had a hard time creating an environment, I hope that no one will get stuck in a similar swamp.
At the end, something like this is completed. (Displayed in Draw.io Integration, an extension of VS Code)
The Graphviz package used to draw with Graphviz and the Graphviz package used to use graphviz2drawio are different. If you do not separate the environment, an error will occur. There may be packages that are supposed to be installed and used by Graphviz alone, and those that are completed within the package. Or compatibility between packages? This time, apart from the environment for drawing with Graphviz, we prepared an environment for outputting with graphviz2drawio.
OS Windows10 64bit Python 3.7.7 (created with Anaconda3-2019.03)
Create a new Python 3.7 environment with Anaconda (named Dataviz) As of May 30, 2020, it was python 3.7.7.
Install the following packages. It is in the standard package. ・ Networkx ・ Pydot (not pydotplus) -Python-graphviz (Don't forget that import graphviz will not work without it)
From here, start the Anaconda terminal and in order ・ Graphviz (https://anaconda.org/alubbock/graphviz) ver 2.41 ・ Pygraphviz (https://anaconda.org/alubbock/pygraphviz) ver 1.5 ・ Graphviz2drawio (https://pypi.org/project/graphviz2drawio/0.2.0/) I will put in.
(DataViz) C:\Users\(username)>conda install graphviz pygraphviz -c alubbock
(DataViz) C:\Users\(username)>pip install graphviz2drawio==0.2.0
will do. With standard graphviz
pygraphviz/graphviz_wrap.c(2987): fatal error C1083:Unable to open include file.'graphviz/cgraph.h':No such file or directory
Error appears and pygraphviz cannot be installed.
Now type dot -v
to see if you can handle dot files.
If something goes wrong, you will be asked to include dot -c
as follows:
(DataViz) C:\Users\(username)>dot -v
dot - graphviz version 2.41.20170921.1950 (20170921.1950)
There is no layout engine support for "dot"
Perhaps "dot -c" needs to be run (with installer's privileges) to register the plugins?
Obediently dot -c
(nothing comes out)
(DataViz) C:\Users\(username)>dot -c
(DataViz) C:\Users\(username)>
If you hit dot -c
again and get the following result, it will be a little more.
If this is not done, an error such as "svg" is not recognized… will appear when using graphviz2drawio.
dot - graphviz version 2.41.20170921.1950 (20170921.1950)
libdir = "C:\Users\(username)\.conda\envs\DataViz\Scripts"
Activated plugin library: gvplugin_dot_layout.dll
Using layout: dot:dot_layout
Activated plugin library: gvplugin_core.dll
Using render: dot:core
Using device: dot:dot:core
The plugin configuration file:
C:\Users\(username)\.conda\envs\DataViz\Scripts\config6
was successfully loaded.
render : cairo dot dot_json fig gdiplus json json0 map mp pic ps svg tk vml xdot xdot_json
layout : circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi
textlayout : textlayout
device : bmp canon cmap cmapx cmapx_np dot dot_json emf emfplus eps fig gif gv imap imap_np ismap jpe jpeg jpg json json0 metafile mp pdf pic plain plain-ext png ps ps2 svg tif tiff tk vml xdot xdot1.2 xdot1.4 xdot_json
loadimage : (lib) bmp eps gif jpe jpeg jpg png ps svg
"C: \ Users \ (user name) \ .conda \ envs \ DataViz \ Lib \ site-packages \ graphviz2drawio \ mx \ MxGraph.py" I get an error in the file, but I'm not sure what I was trying to do, so comment it out and save it. If you know, please help me with the correction. Https://github.com/hbmartin/graphviz2drawio/issues
~\.conda\envs\DataViz\Lib\site-packages\graphviz2drawio\mx\MxGraph.py in add_mx_geo_with_points(element, curve)
115 for cb in curve.cbset:
116 ET.SubElement(array, MxConst.POINT, x=str(cb[0][0]), y=str(cb[0][1]))
--> 117 if cb:
118 ET.SubElement(array, MxConst.POINT, x=str(cb[1][0]), y=str(cb[1][1]))
119
UnboundLocalError: local variable 'cb' referenced before assignment
Revised
MxGraph.py
for cb in curve.cbset:
ET.SubElement(array, MxConst.POINT, x=str(cb[0][0]), y=str(cb[0][1]))
#Comment out because it causes an error
#if cb:
# ET.SubElement(array, MxConst.POINT, x=str(cb[1][0]), y=str(cb[1][1]))
As of May 30, 2020, pygraphviz that supports python3 is only alubbock's, and it seems that it is necessary to put albubbock's graphviz accordingly. However, even if I put this graphviz, I could not draw the graph.
graphviz_test.py
from graphviz import Source
path = "edges.dot"
s = Source.from_file(path)
s.view()
result
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
//
FileNotFoundError: [WinError 2]The specified file could not be found.
During handling of the above exception, another exception occurred:
ExecutableNotFound Traceback (most recent call last)
//
ExecutableNotFound: failed to execute ['dot.bat', '-Tpdf', '-O', 'pdt.dot'], make sure the Graphviz executables are on your systems' PATH
On the contrary, if standard graphviz is inserted, graphviz2drawio cannot be used (pygraphviz called internally cannot be used).
from graphviz2drawio import graphviz2drawio
When you run
ImportError: DLL load failed:The specified module cannot be found.
It will become.
If it is not Windows, if you build the environment with pip, install Graphviz software and set an appropriate PATH, you may not have such trouble.
This time I will use this sample. It is assumed that it is saved in the same folder as edgelist2drawio.py.
edges.txt
A B
A C
A D
B C
C D
D E
python program
edgelist2drawio.py
import networkx as nx
from graphviz2drawio import graphviz2drawio
#edges.Read text as a directed graph
G = nx.read_edgelist('edges.txt', create_using=nx.DiGraph(), nodetype=str)
#Convert the read information to dot format
edges_dot = nx.drawing.nx_pydot.to_pydot(G)
#Export as a dot file if needed
# nx.drawing.nx_pydot.write_dot(G,"edges.dot")
#Draw like graphviz based on dot format data and draw.Export as an io file
xml = graphviz2drawio.convert(str(edges_dot))
#When reading from a dot file and converting
# xml = graphviz2drawio.convert("edges.dot")
#Save the converted file
with open('edges.drawio', 'w') as f:
print(xml, file=f)
When you open edges.drawio created by executing the program with VS Code (Draw.io Integration https://github.com/hediet/vscode-drawio.git is included in the extension), it looks like it was written in Graphviz. You can open a picture. You can edit it as you like!
・ Graphviz2drawio https://pypi.org/project/graphviz2drawio/0.2.0/
・ GraphViz https://www.graphviz.org/
・ I learned about the joy of using draw.io with VS Code in this article. "It seems that Draw.io can now be used with VS Code!" https://qiita.com/riku-shiru/items/5ab7c5aecdfea323ec4e
・
Recommended Posts