Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SBucur opened this issue Dec 8, 2022 · 0 comments
Open

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

SBucur opened this issue Dec 8, 2022 · 0 comments

Comments

@SBucur
Copy link

SBucur commented Dec 8, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant