Introduction
ObjectClassesList
is a widget that allows users to view a list of all given object classes. The widget provides a flexible display option with a choice of single-column or multiple-column layouts. It also allows users to select or deselect one or more classes, making it easy to manage and organize object classes. This widget is a useful tool for visualizing and selecting classes in Supervisely.
Function signature
Copy obj_classes_list = ObjectClassesList (
object_classes = project_meta.obj_classes,
selectable = False ,
columns = 1 ,
widget_id = None
)
Parameters
Parameters
Type
Description
Union[ObjClassCollection, List[ObjClass]]
Supervisely object class collection or list of object classes
object_classes
Supervisely object class collection (ObjClassCollection
) or list of ObjClass
.
type: Union[ObjClassCollection, List[ObjClass]]
Copy obj_classes_list = ObjectClassesList (
object_classes = project_meta.obj_classes
)
selectable
Enable classes selection.
type: bool
default False
Copy obj_classes_list = ObjectClassesList (
object_classes = project_meta.obj_classes,
selectable = True
)
columns
Number of columns.
type: int
default 1
Copy obj_classes_list = ObjectClassesList (
object_classes = project_meta.obj_classes,
columns = 2
)
widget_id
ID of the widget.
type: str
default value: None
Methods and attributes
Attributes and Methods
Description
Return list of selected classes.
Mini App Example
You can find this example in our Github repository:
supervisely-ecosystem/ui-widgets-demos/media/010_object_classes_list/src/main.py
Import libraries
Copy import os
from random import randint
import supervisely as sly
from dotenv import load_dotenv
from supervisely . app . widgets import Button , Card , Container
from supervisely . app . widgets import Image , ObjectClassesList , ProjectThumbnail
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 ()
Initialize Project ID
and Dataset ID
we will use
Copy project_id = sly . env . project_id ()
dataset_id = sly . env . dataset_id ()
Get Project info and meta
Copy project_info = api . project . get_info_by_id (id = project_id)
project_meta = sly . ProjectMeta . from_json (api.project. get_meta (project_id))
Initialize ObjectClassesList
widget
Copy obj_classes_list = ObjectClassesList (
object_classes = project_meta.obj_classes,
selectable = True ,
columns = 3 ,
)
Create app layout
Prepare a layout for app using Card
widget with the content
parameter and place widget that we've just created in the Container
widget.
Copy project_thumbnail = ProjectThumbnail (project_info)
cls_select_btn = Button (text = "PREVIEW" )
select_class_container = Container (widgets = [obj_classes_list, cls_select_btn])
image = Image ()
preview_container = Container (
widgets = [select_class_container, image],
direction = "horizontal" ,
fractions = [ 2 , 1 ],
)
card = Card (
title = "Object Classes List" ,
content = Container (widgets = [project_thumbnail, preview_container]),
)
layout = Container (widgets = [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 @cls_select_btn . click
def show_info ():
classes = obj_classes_list . get_selected_classes ()
obj_classes = api . object_class . get_list (
project_id = project_id, filters = [{ "field" : "name" , "operator" : "in" , "value" : classes}]
)
imgs = api . image . get_filtered_list (
dataset_id = dataset_id,
filters = [{ "type" : "objects_class" , "data" : { "classId" : obj.id}} for obj in obj_classes],
)
if len (imgs) == 0 :
image . clean_up ()
return
index = randint ( 0 , len (imgs) - 1 )
image . set (imgs[index].full_storage_url)