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.
type:Optional[List[TreeSelect.Item]]
default value:None
Prepare the items:
Initialize the widget with the items:
multiple_select
If True, multiple items can be selected. Default is False.
type:bool
default value:False
Initialize the widget with multiple selection:
multiple_select
flat
If True, selecting a parent item will not select its children. Default is False.
type:bool
default value:False
Initialize the widget with the flat option:
always_open
If True, the tree will be opened when the widget is initialized.
type:bool
default value:False
Initialize the widget with the always open option:
widget_id
An optional ID for the widget.
type:str
default value:None
Initialize the widget with an ID:
Methods and attributes
Methods and attributes
Description
set_items(items: List[TreeSelect.Item])
Set the items to be displayed in the widget (overrides current).
add_items(items: List[TreeSelect.Item])
Add items to the current list of items in the widget.
First, we load environment variables with credentials and init API for communicating with Supervisely Instance:
Prepare the items
Prepare the items to be displayed in the TreeSelect widget:
Initialize the TreeSelect widget
Let's initialize the TreeSelect widget without the items:
And then set the items:
Dynamically add items
Let's prepare a new list of items, which will be added dynamically to the widget.
And prepare a button, which will add the new items to the widget.
We also need to set the handler for the button click event.
Show selected items in UI
Now, let's create a button, which will show selected items in UI. And create a simple text widget to show the selected items.
Same as for the previous button, we need to set the handler for the click event.
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.
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.
import os
import supervisely as sly
from dotenv import load_dotenv
from typing import List
from supervisely.app.widgets import Card, TreeSelect, Container, Text, Button, Flexbox
load_dotenv("local.env")
load_dotenv(os.path.expanduser("~/supervisely.env"))
api = sly.Api()
@show_selected_button.click
def show_selected():
print("Showing selected items")
selected_items = tree_select.get_selected()
if selected_items and isinstance(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()
value_changed_info = Text(status="info")
value_changed_info.hide()
@tree_select.value_changed
def new_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()