Easy with just Python! Output Graphviz figures in draw.io format!

Introduction

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)

I was addicted to

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.

environment

OS Windows10 64bit Python 3.7.7 (created with Anaconda3-2019.03)

Environment

Create a new Python 3.7 environment with Anaconda (named Dataviz) As of May 30, 2020, it was python 3.7.7.

Installation of standard packages

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)

Installation of non-standard packages

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.

dot.exe plug-in registration (?)

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

Fix MxGraph.py

"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]))

Supplement for environment construction

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.

Try using

program

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)

To edit

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!

image.png

in conclusion

・ 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

Easy with just Python! Output Graphviz figures in draw.io format!
Output log in JSON format with Python standard logging
Easy image processing in Python with Pillow
format in python
Image format in Python
Japanese output in Python
Get standard output in real time with Python subprocess
Get the result in dict format with Python psycopg2
Read json file with Python, format it, and output json
Find this week's date in any format with python
Japanese output when dealing with python in visual studio
It is easy to execute SQL with Python and output the result in Excel
Scraping with selenium in Python
[Co-occurrence analysis] Easy co-occurrence analysis with Python! [Python]
Working with LibreOffice in Python
Scraping with chromedriver in python
Debugging with pdb in Python
Easy folder synchronization with Python
Working with sounds in Python
Scraping with Selenium in Python
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python
Easy Python compilation with NUITKA-Utilities
Easy HTTP server with Python
Easy password box in Python
Format json with Vim (with python)
String format with Python% operator
Read Fortran output in python
Output the contents of ~ .xlsx in the folder to HTML with Python
Number recognition in images with Python
Testing with random numbers in Python
[Python] Easy parallel processing with Joblib
Output 2017 Premium Friday list in Python
Working with LibreOffice in Python: import
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
Automatically format Python code in Vim
Input / output with Python (Python learning memo ⑤)
Handle NetCDF format data in Python
Numer0n with items made in Python
Handle GDS II format in Python
Open UTF-8 with BOM in Python
Make standard output non-blocking in Python
Use rospy with virtualenv in Python3
Easy Python + OpenCV programming with Canopy
[Note] Hello world output with python
Visualize python package dependencies with graphviz
Unit test log output with python
Easy email sending with haste python3
Bayesian optimization very easy with Python
Use Python in pyenv with NeoVim
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
Export and output files in Python
Password generation in texto with python
Easy data visualization with Python seaborn.
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
Easy parallel execution with python subprocess
Output networkX graph with graphviz (PyGraphviz)