Switch

Introduction

Switch widget in Supervisely is a user interface element that allows users to toggle between two states, such as on/off or true/false. With the Switch widget, users can easily turn on or off a specific feature or setting. The Switch widget is useful for creating project dashboards that require users to toggle specific options quickly and easily. Users can customize the appearance and behavior of the Switch widget to match their project requirements, making it a flexible tool for creating user interfaces in Supervisely apps. Overall, the Switch widget is a simple yet effective tool for improving the usability and functionality of Supervisely apps.

Function signature

Switch(
    switched=False,
    width=58,
    on_text="ON",
    off_text="OFF",
    on_color=None,
    off_color=None,
    on_content=None,
    off_content=None,
    widget_id=None,
)

Parameters

ParametersTypeDescription

switched

bool

Determine Switch is ON or OFF

width

int

Width of Switch

on_text

str

Text displayed when Switch in ON state

off_text

str

Text displayed when Switch in OFF state

on_color

str

Background color when Switch in ON state

off_color

str

Background color when Switch in OFF state

on_content

Widget

Determine active Widget when Switch is in ON state

off_content

Widget

Determine active Widget when Switch is in OFF state

widget_id

str

ID of the widget

switched

Determine Switch is ON or OFF.

type: bool

default value: false

switch = Switch(switched=True)

width

Determine Switch width.

type: int

default value: 58

switch = Switch(width=150)

on_text

Determine text displayed when Switch in ON state.

type: str

default value: ON

off_text

Determine text displayed when Switch in OFF state.

type: str

default value: OFF

switch = Switch(
    on_text="on example",
    off_text="off example",
    width=140,
)

on_color

Determine background color when Switch in ON state.

type: str

default value: None

switch = Switch(on_color="#FF0000")

off_color

Determine background color when Switch in OFF state.

type: str

default value: None

switch = Switch(off_color="#7F00FF")

on_content

Determine active Widget when Switch in ON state.

type: Widget

default value: None

off_content

Determine active Widget when Switch in OFF state.

type: Widget

default value: None

switch = Switch(
    on_content=Text("ON Content"),
    off_content=Text("OFF Content"),
)
switch_one_of = OneOf(switch)

widget_id

ID of the widget.

type: str

default value: None

Methods and attributes

Attributes and MethodsDescription

is_switched()

Check Switch state.

on()

Set Switch in ON state.

off()

Set Switch in OFF state.

get_width()

Return Switch width.

get_on_text()

Return Switch ON text.

set_on_text()

Set Switch ON text.

get_off_text()

Return Switch OFF text.

set_off_text(value: str)

Set Switch OFF text.

get_on_color()

Return Switch ON color.

set_on_color(value: str)

Set Switch ON color.

get_off_color()

Return Switch OFF color.

set_off_color(value: str)

Set Switch OFF color.

@value_changed

Decorator function is handled when Switch value is change.

Mini App Example

You can find this example in our Github repository:

ui-widgets-demos/controls/004_switch/src/main.py

Import libraries

import supervisely as sly
from supervisely.app.widgets import Container, Switch, OneOf, Text, Card

Initialize Switch widget

switch = Switch(
    on_content=Text("ON Content"),
    off_content=Text("OFF Сontent"),
)

Initialize OneOf widget to switch between Cards

switch_one_of = OneOf(switch)

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.

layout = Container(widgets=[
    Card(title="Switch", content=switch),
    Card(title="OneOf", content=switch_one_of),
])

Create app using layout

Create an app object with layout parameter.

app = sly.Application(layout=layout)

Last updated