ClassesListPreview

Introduction

Classes List Preview widget just displays a list of classes. It can be used to display classes that were selected by the user in the Classes List Selector widget for example.

Function signature

classes_list_preview = ClassesListPreview(
    classes=obj_classes,
    max_height="128px",
    empty_text = None,
    show_shape_title=True,
    show_shape_icon=True,
    widget_id = None,
    )
default

Parameters

ParametersTypeDescription

classes

Union[List[ObjClass], ObjClassCollection]

Supervisely object class collection or list of object classes

max_height

bool

Max height of the widget

empty_text

str

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

show_shape_title

bool

If True show name of the shape next to class name

show_shape_icon

bool

If True show icon of the shape near class name

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_preview = ClassesListPreview(
    classes=obj_classes
)

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"

classes_list_preview = ClassesListPreview(
    classes=obj_classes,
    max_height="100px"
)

empty_text

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

type: str

default None

empty_text = "No classes to preview"
classes_list_preview = ClassesListPreview(
    classes=[],
    empty_text=empty_text,
)
empty-text

show_shape_title

If True show name of the shape next to class name.

type: bool

default True

empty_text = "No classes to preview"
classes_list_preview = ClassesListPreview(
    classes=[],
    empty_text=empty_text,
)
disable-shape-text

show_shape_icon

If True show icon of the shape near class name.

type: bool

default True

empty_text = "No classes to preview"
classes_list_preview = ClassesListPreview(
    classes=[],
    empty_text=empty_text,
)

widget_id

ID of the widget.

type: str

default value: None

Methods and attributes

Attributes and MethodsDescription

set()

Set classes to widget and determine wheter to display shape icon and title.

get()

Return list of classes.

Mini App Example

In this example we will create a mini app with ClassesListPreview widget. We will create a ClassesListSelector widget and display selected classes with ClassesListPreview widget.

You can find this example in our Github repository:

supervisely-ecosystem/ui-widgets-demos/media/017_classes_list_preview/src/main.py

Import libraries

import os
import supervisely as sly
from supervisely.app.widgets import (
    Card,
    Container,
    ClassesListPreview,
    ClassesListSelector,
    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 and init ClassesListSelector widget

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,
]

classes_list_selector = ClassesListSelector(obj_classes, multiple=True)

Initialize ClassesListPreview widget and Text widget for displaying number of selected classes

classes_list_preview = ClassesListPreview()
preview_text = Text(f"Selected Classes: 0 / {len(obj_classes)}", "text")
preview_container = Container([preview_text, classes_list_preview])

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.

container = Container(widgets=[classes_list_selector, preview_container])

card = Card(
    title="Classes List Preview",
    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 on_selection_changed(selected_classes):
    preview_text.set(f"Selected Classes: {len(selected_classes)} / {len(obj_classes)}", "text")
    classes_list_preview.set(selected_classes)

Last updated