{"id":4135,"library":"nutree","title":"Nutree (Tree Data Structures)","description":"Nutree is a Python library for tree data structures with an intuitive, yet powerful, API. It allows handling of arbitrary objects in a hierarchical structure, offering features like navigation, searching, filtering, serialization to JSON and DOT, diffing, and typed child nodes. The library is actively maintained with regular releases, currently at version 1.1.0.","status":"active","version":"1.1.0","language":"en","source_language":"en","source_url":"https://github.com/mar10/nutree/","tags":["tree","data-structure","hierarchy","graph","serialization"],"install":[{"cmd":"pip install nutree","lang":"bash","label":"Base installation"},{"cmd":"pip install \"nutree[graph]\"","lang":"bash","label":"With graph support (e.g., Graphviz)"},{"cmd":"pip install \"nutree[all]\"","lang":"bash","label":"All optional features"}],"dependencies":[{"reason":"Required for generating DOT files and Graphviz diagrams when using the 'graph' extra.","package":"graphviz","optional":true}],"imports":[{"symbol":"Tree","correct":"from nutree import Tree"},{"symbol":"Node","correct":"from nutree import Node"}],"quickstart":{"code":"from nutree import Tree\n\n# Create a new tree\ntree = Tree(\"Root\")\n\n# Add nodes in a chained fashion\nchapter1 = tree.add(\"Chapter 1\")\nchapter1.add(\"Section 1.1\")\nchapter1.add(\"Section 1.2\")\n\n# Go up to the root and add another branch\nchapter2 = tree.add(\"Chapter 2\")\nchapter2.add(\"Section 2.1\").add(\"Subsection 2.1.1\")\n\n# Pretty print the tree\ntree.print()\n\n# Access a node by name\nsection_1_1 = tree[\"Section 1.1\"]\nassert section_1_1.name == \"Section 1.1\"\n\n# Add an arbitrary object as data to a node\nclass Task:\n    def __init__(self, name, priority):\n        self.name = name\n        self.priority = priority\n    def __str__(self):\n        return f\"Task({self.name}, P:{self.priority})\"\n\ntask_node = chapter1.add(Task(\"Review Draft\", 1))\nassert isinstance(task_node.data, Task)","lang":"python","description":"This example demonstrates how to create a tree, add nodes using string data and arbitrary objects, chain `add` calls for easy construction, and pretty print the tree structure. It also shows how to access nodes by their name."},"warnings":[{"fix":"Verify that documentation and examples explicitly reference 'nutree' (Python) or the PyPI/GitHub project for this library.","message":"The Python `nutree` library is distinct from the JavaScript `nut-tree/nut.js` project. Ensure you are consulting the correct documentation and examples for the Python package.","severity":"gotcha","affected_versions":"All"},{"fix":"If you need graph features, install with the appropriate extra, e.g., `pip install \"nutree[graph]\"`.","message":"Advanced features like graph visualization (DOT, Mermaid) require installing optional dependencies using `pip install \"nutree[graph]\"` or `pip install \"nutree[all]\"`.","severity":"gotcha","affected_versions":"All"},{"fix":"Be aware of how `nutree` handles object references and identity within the tree structure. Use the library's methods for searching and comparing nodes to ensure expected behavior, especially when an object is part of the tree multiple times.","message":"Nutree nodes can wrap arbitrary objects, and the library handles multiple references to the same object ('clones') distinctly within the tree. Node lookup can be done by name, object reference, or a generated `data_id`, which might differ from direct Python object identity comparisons if not handled carefully.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}