Skip to content

Can't sync custom traitlets when extending Tree/Node #75

Open
@SBucur

Description

@SBucur

I'm trying to use the Tree to represent the tree-like structure of my CustomClass. I extended the Node class to contain a reference to a CustomClass instance. The goal is that when a user clicks on the Node, the Tree it's under can save the reference for another widget to link to and load the information.

import ipywidgets as widgets
import ipytree
from traitlets import Instance

class CustomClass():
    """ node-like: contains a List of other CustomClasses """
    pass

class CustomTree(ipytree.Tree):
    selected_custom_class = Instance(CustomClass, allow_none=True).tag(sync=True)

    def __init__(self, **kwargs):
        super().__init__(multiple_selection=False, **kwargs)
        self.observe(self._get_custom_ref, 'selected')

    def _get_custom_ref(self, event):
        # Unsure how to write/get working <-------------------------------------------
        event['owner'].set_trait('selected_custom_class', event['new'].node_ref)

class CustomNode(ipytree.Node):
    """ Node holds a reference to CustomClass """
    def __init__(self, my_data: CustomClass, **kwargs):
        self.my_data_ref = my_data
        super().__init__(**kwargs)

class OtherWidget(ipywidget.Tab):
    """ Loads the selected Node to view. """
    selected_custom_class = Instance(CustomClass, allow_none=True).tag(sync=True)
    # __init__, etc.

# ... elsewhere in the code base ...
box = widgets.HBox([custom_tree, other_widget])
widgets.link(custom_tree, 'selected_custom_class', other_widget, 'selected_custom_class')

I've tried a lot of things for _get_custom_ref, but nothing seems to run; another version of the code above was to move _get_custom_ref to CustomNode and have it trigger there, but again, I don't see any change. I can run the example jupyter notebook and modify the text just fine, but it doesn't seem to modify the custom traitlet.

Any ideas and advice would be greatly appreciated.

ipytree version: 0.2.2
ipywidgets version: 8.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions