Introduction
Classes List Selector
widget allows users to view a list of object classes and change it dynamically using widget methods. This widget can be used for visualizing and selecting classes in Supervisely.
Function signature
Copy classes_list_selector = ClassesListSelector (
classes = obj_classes,
multiple = True ,
empty_notification = None ,
widget_id = None
)
Default with classes Default without classes
Parameters
Parameters
Type
Description
Union[List[ObjClass], ObjClassCollection]
Supervisely object class collection or list of object classes
Enable multiple classes selection
Notification that will be displayed when there are no classes in widget
classes
List of ObjClass
objects or Supervisely object class collection (ObjClassCollection
).
type: Union[List[ObjClass], ObjClassCollection]
Copy classes_list_selector = ClassesListSelector (
classes = obj_classes
)
multiple
If True
then multiple classes can be selected. Otherwise, only one class can be selected.
type: bool
default False
Copy classes_list_selector = ClassesListSelector (
classes = obj_classes,
multiple = True
)
Multiple Enabled Multiple disabled
empty_notification
Notification that will be displayed when there are no classes in widget
type: NotificationBox
default None
Copy notification_box = NotificationBox (title = "No classes" , description = "Provide classes to the widget." )
classes_list_selector = ClassesListSelector (
multiple = True ,
empty_notification = notification_box
)
widget_id
ID of the widget.
type: str
default value: None
Methods and attributes
Attributes and Methods
Description
Return list of selected classes.
Deselect classes by names.
Return list of all classes.
Callback triggers when selection is changed.
Mini App Example
You can find this example in our Github repository:
supervisely-ecosystem/ui-widgets-demos/selection/016_classes_list_selector/src/main.py
Import libraries
Copy import os
import supervisely as sly
from supervisely . app . widgets import Card , Container , ClassesListSelector , NotificationBox , Text
from dotenv import load_dotenv
Init API client
First, we load environment variables with credentials and init API for communicating with Supervisely Instance:
Copy load_dotenv ( "local.env" )
load_dotenv (os.path. expanduser ( "~/supervisely.env" ))
api = sly . Api ()
Create list of object classes
Copy cat_obj_class = sly . ObjClass ( "cat" , sly.Rectangle)
dog_obj_class = sly . ObjClass ( "dog" , sly.Rectangle)
horse_obj_class = sly . ObjClass ( "horse" , sly.Rectangle)
sheep_obj_class = sly . ObjClass ( "sheep" , sly.Rectangle)
cow_obj_class = sly . ObjClass ( "cow" , sly.Rectangle)
elephant_obj_class = sly . ObjClass ( "elephant" , sly.Rectangle)
bear_obj_class = sly . ObjClass ( "bear" , sly.Rectangle)
zebra_obj_class = sly . ObjClass ( "zebra" , sly.Rectangle)
obj_classes = [
cat_obj_class ,
dog_obj_class ,
horse_obj_class ,
sheep_obj_class ,
cow_obj_class ,
elephant_obj_class ,
bear_obj_class ,
zebra_obj_class ,
]
Initialize ClassesListSelector
widget, NotificationBox
widget for custom notification and Text
widget for displaying selected classes count
Copy notification_box = NotificationBox (title = "No classes" , description = "Provide classes to the widget." )
classes_list_selector = ClassesListSelector (
obj_classes, multiple = True , empty_notification = notification_box
)
selected_classes_cnt = Text ( f "Selected classes: 0 / { len (obj_classes) } " )
Create app layout
Prepare a layout for app using Card
widget with the content
parameter and place widget that we've just created into the Container
widget.
Copy container = Container (widgets = [selected_classes_cnt, classes_list_selector])
card = Card (
title = "Classes List Selector" ,
content = container,
)
layout = card
Create app using layout
Create an app object with layout parameter.
Copy app = sly . Application (layout = layout)
Add functions to control widgets from python code
Copy @classes_list_selector . selection_changed
def selection_changed ( classes ):
selected_classes_cnt . set ( f "Selected classes: { len (classes) } / { len (obj_classes) } " , "text" )