The TreeSelect widget in Supervisely is a user interface element that allows users to select items from a tree-like structure that can be useful when working with hierarchical data. The TreeSelect widget has event handlers that are triggered when items in the right lists are changed. This can be useful for applications that require dynamic change handling, such as showing data from the selected items.
A list of TreeSelect.Item to be displayed in the widget.
multiple_select
bool
If True, multiple items can be selected. Default is False.
flat
bool
If True, selecting a parent item will not select its children. Default is False.
always_open
bool
If True, the tree will be opened when the widget is initialized.
widget_id
str
An optional ID for the widget.
items
Determine the list of TreeSelect.Item items, each of which must have a unique id and an optional label to be displayed in the widget. The children field is a list of TreeSelect.Item items that are children of the current item.
Same as for the previous button, we need to set the handler for the click event.
@show_selected_button.clickdefshow_selected():print("Showing selected items") selected_items = tree_select.get_selected()if selected_items andisinstance(selected_items, list): labels =", ".join([item.label for item in selected_items]) text ="Selected items: {}".format(labels) selected_info.text = text selected_info.show()else: selected_info.hide()
Create an event handler for the TreeSelect widget
Now, let's create a event handler for the TreeSelect widget, when the value of the widget is changed.
value_changed_info =Text(status="info")value_changed_info.hide()@tree_select.value_changeddefnew_items_selected(items: List[TreeSelect.Item]):print("Value changed")if items:# We're processing the event the same way as for the button click event. labels =", ".join([item.label for item in items]) text ="Value changed: {}".format(labels)# Setting the text to the widget and showing it. value_changed_info.text = text value_changed_info.show()else: value_changed_info.hide()
Create app layout
Prepare a layout for the app using Card widget with the content parameter and place widget that we've just created in the Container widget. Let's group our buttons in a Flexbox widget to make them look better.