I am trying to use anytree to read a JSON file and export it as a png image using render tree graph. For a basic example where I create the node it works.
from anytree import RenderTree
from anytree import Node
from anytree.dotexport import RenderTreeGraph
root = Node("root")
s0 = Node("s0", parent=root)
s1 = Node("s1", parent=root)
t1 = Node("t1", parent=s0)
print(root)
RenderTreeGraph(root).to_picture("test.png")
When I try importing a json file as per the docs
from anytree.importer import JsonImporter
from anytree import RenderTree
from anytree import Node
from anytree.dotexport import RenderTreeGraph
importer = JsonImporter()
path = open("config.json")
root = importer.read(path)
tree = RenderTree(root)
print(tree)
RenderTreeGraph(tree).to_picture("test.png")
I get the following error:
Traceback (most recent call last):
File "pyth.py", line 20, in <module>
DotExporter(tree).to_dotfile("tree.dot")
File "/home/user/.local/lib/python3.5/site-packages/anytree/exporter/dotexporter.py", line 214, in to_dotfile
for line in self:
File "/home/user/.local/lib/python3.5/site-packages/anytree/exporter/dotexporter.py", line 160, in __iter
for node in self.__iter_nodes(indent, nodenamefunc, nodeattrfunc):
File "/home/user/.local/lib/python3.5/site-packages/anytree/exporter/dotexporter.py", line 174, in __iter_nodes
nodename = nodenamefunc(node)
File "/home/user/.local/lib/python3.5/site-packages/anytree/exporter/dotexporter.py", line 142, in __default_nodenamefunc
return node.name
AttributeError: 'RenderTree' object has no attribute 'name'
Piping the print(tree)
to a text file give the JSON as a text block without white space formatting, where as in the docs this needs to be done line by line to capture the tree structure. So it appears as though RenderTree(root)
isn't formatting the JSON in the Node style of the first example.
Does anyone know what is going wrong? Is there another step to correctly parse the JSON?