Search code examples
pythontreepython-3.6anytree

Exporting / Importing trees created with python anytree 2.4.3 library


I create a tree with anytree library. I want to be able to modify it, then export - save it to disk, and import it back with the modifications. For instance, the example tree:

udo = Node("Udo")
marc = Node("Marc", parent=udo)
lian = Node("Lian", parent=marc)
dan = Node("Dan", parent=udo)
jet = Node("Jet", parent=dan)
jan = Node("Jan", parent=dan)
joe = Node("Joe", parent=dan)

Udo
├── Marc
│   └── Lian
└── Dan
    ├── Jet
    ├── Jan
    └── Joe

I can modify it, for instance cutting Dan off and adding a children to Marc

dan.parent = None 
bonny = Node ("Bonny", parent = marc)

Udo
└── Marc
    ├── Lian
    └── Bonny

But when I export the tree to json and then import it back, the only node I seem to be able to reference is the root. So I can't do this kind of modification anymore because the variable names like dan or marc are not there, ie, I don't find a way to reference a node. Am I missing anything?

with open ('cajon/anytreeexample.json', 'r+', encoding = 'utf-8') as f:
    datos = importer.read(f)

print (datos)

This means that after importing the tree what you have is just a root node

AnyNode(name='Udo')  # Udo is the root

From here you can get Udo's children, and the children's children like

marc, dan = udo.children
lian = marc.children
jet, jan, joe = dan.children

But they are not working as a node

print (lian.parent)
AttributeError: 'tuple' object has no attribute 'parent'

And seems you cannot attach a children to them, which is my main purpose with this structure:

sonny = AnyNode("Sonny", parent = lian)
TypeError: __init__() got multiple values for argument 'parent'

So my question is, is there a way to load the json saved tree into a proper anytree structure, where you can append new nodes?


Solution

  • You actually did it the right way: you just forgot a ,

    from anytree import Node
    
    udo = Node("Udo")
    marc = Node("Marc", parent=udo)
    Node("Lian", parent=marc)
    
    lian, = marc.children # this is a tupel, even if its only one entry -> add ,
    sonny = Node("Sonny", parent = lian)
    
    print (lian.parent)
    > Node('/Udo/Marc')
    
    print (sonny)
    > Node('/Udo/Marc/Lian/Sonny')
    

    @How to find your nodes: You are looking for anytrees find_by_attr:

    Search for a single node with attribute name having value [...]

    So after loading your tree with

    with open ('cajon/anytreeexample.json', 'r+', encoding = 'utf-8') as f:
        datos = importer.read(f)
    

    You can search for nodes by name:

    udo = datos.find_by_attr("Udo") # should be the same as datos if udo was the root
    

    And then add more like so:

    Node("Sonny", parent = datos.find_by_attr("lian"))