TreeSelect
Introduction
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.
Function signature
Parameters
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.
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:
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
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.
Mini app example
You can find this example in our GitHub repository:
supervisely-ecosystem/ui-widgets-demos/selection/021_tree_select/src/main.py
Import libraries
Init API client
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
TreeSelect
widgetLet'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
TreeSelect
widgetNow, 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.
Create an app using the layout
Create an app object with the layout parameter.
Last updated