Programmatically expand nodes in tag browse tree

Hello, I'm working with a tag browse tree in Perspective and I was wondering if there was a way to programmatically expand the nodes without needing a user to click on a folder. I.e. is there a way to identify a node by name or path and then write node.expand() in an event script such that the node's children are visible? Thank you!

1 Like

Yes; identifying and programatically expanding nodes is possible.

Here is an example script that programatically opens all Item nodes and subNodes named Child 1 when the parents are named Item:

def runAction(self, event):
	nodes = self.getSibling("Tree").props.items
	def expandNodes(node):
		if 'Item' in node.label and len(node.label) == 6:
			node.expanded = True
		elif node.label == 'Child 1':
			node.expanded = True
		for subNode in node.items:
			expandNodes(subNode)
	for node in nodes:
		expandNodes(node)

Before button press:
image

After button press:
image

Every sub prop has a label and string data that can identify it, and the setting the expanded bool to True will expand the node.

Hi @justinedwards.jle , thanks so much for your response!

I'm trying to implement your solution in my project and have run into a few things. First, it appears that self.props.items is an instance method on the tag browse tree and writing nodes = self.props.items() will return the object

[(u'root', {u'path': u'[default]import'}), (u'selection', {u'mode': u'multiple', u'values': [u'[default]import/flow indicators']}), (u'display', {u'refreshIcon': {u'path': u'material/search', u'visible': False, u'style': {u'classes': u''}}}), (u'style', {u'marginRight': -2L, u'classes': u''})]

I'm not sure the exact class of object this is but is not a list of nodes but rather the properties of the tag browse tree. Do you know of an alternate method of retrieving the list of nodes? Also, how do you know that node objects have the property expanded? I'm trying to find documentation on it but I can't, so if you have any links or information that would be super helpful!

Thank you again!

I believe Justin mixed things up and thought you were using a simple tree, not a tag browse tree.

The items method has nothing to do with the actual items in the tree, it's an equivalent of the dict's items method that returns its keys and values.

I'm not sure you can actually access an 'expanded' property of the tag browse tree's nodes...

Confirmed :+1:

No worries, I will investigate if using a simple tree works in my implementation! Thanks for your help!

1 Like