[Python] Treelib does not show hierarchical tree node data

Discussão em 'Python' iniciado por Stack, Agosto 25, 2025.

  1. Stack

    Stack Membro Participativo

    Using tree package treelib in Python I define a small tree that is slightly modified from the description for command show:

    from treelib import Tree
    tree = Tree()
    tree.create_node("Root", "root")
    tree.create_node("Child A", "a", parent="root",data={"name":"Mark"})
    tree.create_node("Child B", "b", parent="root",data={"name":"Bob"})


    I can graphically display the tree:

    tree.show()

    Root
    ├── Child A
    └── Child B


    Instead of the tags I want to graphically display the data. Let's first return the data of a single node:

    print(tree.get_node('a').data)
    output: {'name': 'Mark'}


    Now display the whole tree:

    tree.show(data_property="name")


    I get AttributeError: 'dict' object has no attribute 'name'. How to graphically display the data of the tree nodes with treelib?

    Python 3.12.4, treelib 1.8.0, Spyder 6.0.7

    Continue reading...

Compartilhe esta Página