Graphviz.Graph/Graphviz.DiGraph is unusable after pydot.graph_from_dot_file()
The ability to use DOT files is no longer supported by Graphviz 2.40.1 (Debian Buster), if using the latest PyDot (v1.4.1) approach to load its DOT file.
I suspect the problem to be at graphviz.Graph
/graphviz.DiGraph
.
What's really scary is that when using the Python debugger in PyCharm IDE (by JetBrain), Python exception error messages got written (OVER?) into the Graphviz.source
string.
Steps to reproduce
Run
#!/usr/bin/python3
import graphviz
import pydot
print('PyDot version:', pydot.__version__)
print('Graphviz version:', graphviz.version())
DotG = pydot.graph_from_dot_file('Digraph.gv')
print('DotG:', DotG[0]) # prints out original file content
# Now, convert PyDot Graph into a Graphviz Graph type
GvDotG = graphviz.Digraph(DotG)
print('GvDotG.engine:', GvDotG.engine)
print('GvDotG.render:', GvDotG.render)
print('GvDotG.strict:', GvDotG.strict)
print('GvDotG.directed:', GvDotG.directed)
print('GvDotG.comment:', GvDotG.comment)
print('GvDotG.directory:', GvDotG.directory)
print('GvDotG.encoding:', GvDotG.encoding)
print('GvDotG.filename:', GvDotG.filename)
print('GvDotG.filepath:', GvDotG.filepath)
print('GvDotG.graph_attr:', GvDotG.graph_attr)
print('GvDotG.name:', GvDotG.name)
print('GvDotG.format:', GvDotG.format)
# so far, so good.
# But fails on next three lines
print('GvDotG.source:', GvDotG.source) # BOOM!
print('GvDotG:', GvDotG) # doesn't work here also (alias of GvDotG.source?)
GvDotG.edge('Hello', 'World') # also fails here, graph is rendered unusable?
Expected Behaviour
I expect the graphviz.Digraph(DotG)
to provide a Graphviz-compliant Graph
type variable so that this Graph
can be used by other functions provided by Graphviz.
Following 3 lines should be working.
print('GvDotG.source:', GvDotG.source) # BOOM!
print('GvDotG:', GvDotG) # doesn't work here also (alias of GvDotG.source?)
GvDotG.edge('Hello', 'World') # also fails here, graph is rendered unusable?
Actual Behaviour
Instead, it errors all similarly for 3 test lines here:
print('GvDotG.source:', GvDotG.source) # BOOM!
print('GvDotG:', GvDotG) # doesn't work here also (alias of GvDotG.source?)
GvDotG.edge('Hello', 'World') # also fails here, graph is rendered unusable?
Python3.7.3 reports:
$ python3 bug-gv.py
PyDot version: 1.4.1
Graphviz version: (2, 40, 1)
DotG: digraph G {
Hello -> World;
}
GvDotG.engine: dot
GvDotG.render: <bound method File.render of <graphviz.dot.Digraph object at 0x7f3a7f612208>>
GvDotG.strict: False
GvDotG.directed: True
GvDotG.comment: None
GvDotG.directory:
GvDotG.encoding: utf-8
GvDotG.filename: [<pydot.Dot object at 0x7f3a7f629908>].gv
GvDotG.filepath: [<pydot.Dot object at 0x7f3a7f629908>].gv
GvDotG.graph_attr: {}
GvDotG.name: [<pydot.Dot object at 0x7f3a7f629908>]
GvDotG.format: pdf
Traceback (most recent call last):
File "bug-gv.py", line 28, in <module>
print('GvDotG.source:', GvDotG.source) # BOOM!
File "/usr/local/lib/python3.7/dist-packages/graphviz/dot.py", line 118, in __str__
return '\n'.join(self)
File "/usr/local/lib/python3.7/dist-packages/graphviz/dot.py", line 104, in __iter__
yield head % (self._quote(self.name) + ' ' if self.name else '')
File "/usr/local/lib/python3.7/dist-packages/graphviz/lang.py", line 72, in quote
if is_html_string(identifier) and not isinstance(identifier, NoHtml):
TypeError: expected string or bytes-like object
ipython3 v5.8.0 reports similar error:
$ ipython3 bug-gv.py
PyDot version: 1.4.1
Graphviz version: (2, 40, 1)
DotG: digraph G {
Hello -> World;
}
GvDotG.engine: dot
GvDotG.render: <bound method File.render of <graphviz.dot.Digraph object at 0x7fd0e9f19898>>
GvDotG.strict: False
GvDotG.directed: True
GvDotG.comment: None
GvDotG.directory:
GvDotG.encoding: utf-8
GvDotG.filename: [<pydot.Dot object at 0x7fd0eb10dbe0>].gv
GvDotG.filepath: [<pydot.Dot object at 0x7fd0eb10dbe0>].gv
GvDotG.graph_attr: {}
GvDotG.name: [<pydot.Dot object at 0x7fd0eb10dbe0>]
GvDotG.format: pdf
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/steve/work/datascience/WSGraphDemo/bug-gv.py in <module>()
26
27 # But fails on next three lines
---> 28 print('GvDotG.source:', GvDotG.source) # BOOM!
29 print('GvDotG:', GvDotG) # doesn't work here also (alias of GvDotG.source?)
30 GvDotG.edge('Hello', 'World') # also fails here, graph is rendered unusable?
/usr/local/lib/python3.7/dist-packages/graphviz/dot.py in __str__(self)
116 def __str__(self):
117 """The DOT source code as string."""
--> 118 return '\n'.join(self)
119
120 source = property(__str__, doc=__str__.__doc__)
/usr/local/lib/python3.7/dist-packages/graphviz/dot.py in __iter__(self, subgraph)
102 else:
103 head = self._head_strict if self.strict else self._head
--> 104 yield head % (self._quote(self.name) + ' ' if self.name else '')
105
106 for kw in ('graph', 'node', 'edge'):
/usr/local/lib/python3.7/dist-packages/graphviz/lang.py in quote(identifier, is_html_string, is_valid_id, dot_keywords, escape_unescaped_quotes)
70 "\\\""
71 """
---> 72 if is_html_string(identifier) and not isinstance(identifier, NoHtml):
73 pass
74 elif not is_valid_id(identifier) or identifier.lower() in dot_keywords:
TypeError: expected string or bytes-like object
OS Version
Linux arca 4.19.0-10-amd64 #1 (closed) SMP Debian 4.19.132-1 (2020-07-24) x86_64 GNU/Linux
Graphviz Version
$ dot -v
dot - graphviz version 2.40.1 (20161225.0304)
libdir = "/usr/lib/x86_64-linux-gnu/graphviz"
Activated plugin library: libgvplugin_dot_layout.so.6
Using layout: dot:dot_layout
Activated plugin library: libgvplugin_core.so.6
Using render: dot:core
Using device: dot:dot:core
The plugin configuration file:
/usr/lib/x86_64-linux-gnu/graphviz/config6a
was successfully loaded.
render : cairo dot dot_json fig gd json json0 map mp pic pov ps svg tk vml vrml 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 eps fig gd gd2 gif gtk gv ico imap imap_np ismap jpe jpeg jpg json json0 mp pdf pic plain plain-ext png pov ps ps2 svg svgz tif tiff tk vml vmlz vrml wbmp x11 xdot xdot1.2 xdot1.4 xdot_json xlib
loadimage : (lib) bmp eps gd gd2 gif ico jpe jpeg jpg png ps svg xbm
Additional info
Apparently, this error occurs with every DOT-formatted files, including this one:
digraph {
Hello -> World
}