TreeSelect
Last updated
Last updated
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.
items
Optional[List[TreeSelect.Item]]
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.
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:
If True
, multiple items can be selected. Default is False
.
type: bool
default value: False
Initialize the widget with multiple selection:
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:
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:
An optional ID for the widget.
type: str
default value: None
Initialize the widget with an ID:
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.
clear_items()
Clear all items from the widget.
get_selected()
Get the selected items in the widget.
set_selected(value: Union[List[TreeSelect.Item], TreeSelect.Item])
Set the selected items in the widget.
You can find this example in our GitHub repository:
supervisely-ecosystem/ui-widgets-demos/selection/021_tree_select/src/main.py
First, we load environment variables with credentials and init API for communicating with Supervisely Instance:
Prepare the items to be displayed in the TreeSelect
widget:
TreeSelect
widgetLet's initialize the TreeSelect
widget without the items:
And then set the 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.
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.
TreeSelect
widgetNow, let's create a event handler for the TreeSelect widget, when the value of the widget is changed.
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.
Create an app object with the layout parameter.