anytree icon indicating copy to clipboard operation
anytree copied to clipboard

Weighted Edges Image Export Not Working

Open rsdelhi91 opened this issue 4 years ago • 3 comments

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?

rsdelhi91 avatar Jan 05 '22 12:01 rsdelhi91

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.

GeorgeNabilPro avatar Jan 19 '22 10:01 GeorgeNabilPro

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?

eckp avatar Feb 15 '22 21:02 eckp

Run this on your terminal: sudo apt install graphviz

it will solve the issue

ondiekisteven avatar Mar 14 '22 06:03 ondiekisteven