ClassesListSelector

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

classes_list_selector = ClassesListSelector(
    classes=obj_classes,
    multiple=True,
    empty_notification=None,
    widget_id=None
)

Parameters

ParametersTypeDescription

classes

Union[List[ObjClass], ObjClassCollection]

Supervisely object class collection or list of object classes

multiple

bool

Enable multiple classes selection

empty_notification

NotificationBox

Notification that will be displayed when there are no classes in widget

widget_id

str

ID of the widget

classes

List of ObjClass objects or Supervisely object class collection (ObjClassCollection).

type: Union[List[ObjClass], ObjClassCollection]

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

classes_list_selector = ClassesListSelector(
    classes=obj_classes,
    multiple=True
)

empty_notification

Notification that will be displayed when there are no classes in widget

type: NotificationBox

default None

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 MethodsDescription

set()

Set classes to widget.

get_selected_classes()

Return list of selected classes.

select_all()

Select all classes.

deselect_all()

Deselect all classes.

select()

Select classes by names.

deselect()

Deselect classes by names.

set_multiple()

Set multiple flag.

get_all_classes()

Return list of all classes.

@selection_changed

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

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:

load_dotenv("local.env")
load_dotenv(os.path.expanduser("~/supervisely.env"))

api = sly.Api()

Create list of object classes

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

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.

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.

app = sly.Application(layout=layout)

Add functions to control widgets from python code

@classes_list_selector.selection_changed
def selection_changed(classes):
    selected_classes_cnt.set(f"Selected classes: {len(classes)} / {len(obj_classes)}", "text")

Last updated