Weighted Edges Image Export Not Working
Hey,
I'm trying the example snippet for creating an image of the weighted edges tree using DotExporter and am getting errors.
Python version: 3.6.9
The code snippet I tried is as follows:
from anytree import Node, RenderTree, AsciiStyle, PreOrderIter, Resolver, NodeMixin
from anytree.exporter import DotExporter
class WNode(NodeMixin):
def __init__(self, foo, parent=None, weight=None):
super(WNode, self).__init__()
self.foo = foo
self.parent = parent
self.weight = weight if parent is not None else None
def _post_detach(self, parent):
self.weight = None
a = WNode("a")
a0 = WNode("a0", parent=a, weight=2)
a1 = WNode("a1", parent=a, weight=3)
a1a = WNode("a1a", parent=a1)
a2 = WNode("a2", parent=a)
for pre, _, node in RenderTree(a):
print("%s%s (%s)" % (pre, node.foo, node.weight or 0))
DotExporter(a, nodenamefunc=lambda node: node.foo, edgeattrfunc=lambda parent, child: "style=bold,label=%d" % (child.weight or 0)).to_picture("weight.png")
The documentation page I followed is this: https://anytree.readthedocs.io/en/latest/tricks/weightededges.html
The error I'm getting is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ubuntu/.local/lib/python3.6/site-packages/anytree/exporter/dotexporter.py", line 272, in to_picture
check_call(cmd)
File "/usr/lib/python3.6/subprocess.py", line 306, in check_call
retcode = call(*popenargs, **kwargs)
File "/usr/lib/python3.6/subprocess.py", line 287, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'dot': 'dot'
Am I missing something here?
I am using Python version 3.10 and it doesn't work too I tried to see if there is anything that I can edit in the library but unfortunately I couldn't fix the problem.
The to_picture method tries running the dot tool on the command line to render the .dot file it generated into a .PNG. Apparently your shell could not find this tool, have you installed the graphviz library?
Run this on your terminal:
sudo apt install graphviz
it will solve the issue