Introduction
Classes List Preview
widget displays classes mapping. It can be used to display new names of classes that were edited by the user in the Classes Mapping widget for example.
Function signature
Copy classes_mapping_preview = ClassesMappingPreview (
classes = obj_classes,
mapping = mapping,
max_height = "128px" ,
widget_id = None ,
)
Parameters
Parameters Type Description Union[List[ObjClass], ObjClassCollection]
Supervisely object class collection or list of object classes
Dictionary where keys are class names and values are new names.
Text 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_mapping_preview = ClassesMappingPreview (
classes = obj_classes
)
mapping
Dictionary where keys are original class names and values are new names.
type: Dict[str,str]
default None
Copy mapping = { obj_class . name : f " { obj_class . name } -detection" for obj_class in obj_classes }
classes_mapping_preview = ClassesMappingPreview (
classes = obj_classes,
mapping = mapping
)
max_height
Set the maximum height of the widget in pixels. If the content exceeds the maximum height, the scroll bar will appear.
type: str
default "128px"
Copy classes_mapping_preview = ClassesMappingPreview (
classes = obj_classes,
max_height = "100px"
)
widget_id
ID of the widget.
type: str
default value: None
Methods and attributes
Attributes and Methods Description Set classes and mapping to widget.
Return current class mapping.
Mini App Example
In this example we will create a mini app with ClassesMappingPreview
widget. We will create a ClassesMapping
widget and display changed classes with ClassesMappingPreview
widget.
You can find this example in our Github repository:
supervisely-ecosystem/ui-widgets-demos/media/016_classes_mapping_preview/src/main.py
Import libraries
Copy import os
import supervisely as sly
from supervisely . app . widgets import (
Card ,
Container ,
ClassesMapping ,
ClassesMappingPreview ,
Button ,
NotificationBox ,
)
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 and init ClassesMapping
widget
Copy cat_obj_class = sly . ObjClass ( "cat" , sly.Rectangle)
dog_obj_class = sly . ObjClass ( "dog" , sly.Polygon)
horse_obj_class = sly . ObjClass ( "horse" , sly.Bitmap)
cow_obj_class = sly . ObjClass ( "cow" , sly.Polyline)
zebra_obj_class = sly . ObjClass ( "zebra" , sly.Point)
obj_classes = [
cat_obj_class ,
dog_obj_class ,
horse_obj_class ,
cow_obj_class ,
zebra_obj_class ,
]
empty_notification = NotificationBox (
title = "No classes" , description = "Provide classes to widget in order to map new names."
)
classes_mapping = ClassesMapping (obj_classes, empty_notification = empty_notification)
Initialize ClassesMappingPreview
widget and Button
widget for saving and displaying mapping
Copy classes_mapping_preview = ClassesMappingPreview ()
save_button = Button ( "Save" , button_size = "mini" )
Create app layout
Prepare a layout for app using Card
widget with the content
parameter and place widgets that we've previously created into the Container
widget.
Copy container = Container ([classes_mapping, save_button, classes_mapping_preview])
card = Card (
title = "Classes Mapping Preview" ,
content = 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 @save_button . click
def save_mapping ():
mapping = classes_mapping . get_mapping ()
classes_mapping_preview . set (obj_classes, mapping)