Introduction
OneOf
is a container widget that can hold one of several child widgets, such as Radio
, Select
, or Switch
. Depending on the value selected by the user within these child widgets, the corresponding item will be added to the page markup. It is very convenient to quickly switch between different items without losing sight of the overall picture.
Function signature
Copy one_of = OneOf (
conditional_widget = Select (items = animals),
widget_id = None
)
Parameters
Parameters
Type
Description
conditional widget with preset items (Select
, RadioButton
, Switch
)
conditional_widget
Conditional widget with preset items.
type: ConditionalWidget
Copy # prepare "animals" using RadioGroup.Item
one_of = OneOf (
conditional_widget = RadioGroup (items = animals)
)
widget_id
ID of the widget.
type: str
default value: None
Mini App Example
You can find this example in our Github repository:
ui-widgets-demos/layoutsnand containers/008_one_of/src/main.py
Import libraries
Copy import os
import supervisely as sly
from dotenv import load_dotenv
from supervisely . app . widgets import Card , Container , OneOf , Select , Image
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 ()
Prepare conditional widget content
You can use RadioGroup
, Select
or Switch
widgets to set in conditional_widget
parameter. In this tutorial we will use Select
widget.
Prepare sample images
Copy image_1 = Image (
url = "https://user-images.githubusercontent.com/79905215/218269544-2e126d4a-20eb-4ace-8933-d36732bb0634.jpeg"
)
image_2 = Image (
url = "https://user-images.githubusercontent.com/79905215/218269547-5b5316f9-9ae2-4b0c-aedb-b2238e44f95d.jpeg"
)
image_3 = Image (
url = "https://user-images.githubusercontent.com/79905215/218269550-a5caba65-1f0f-4986-8711-7d36c7911e51.jpeg"
)
Prepare items for Select
widget using Select.Item
Copy items = [
Select . Item (value = "image 1" , label = "image 1" , content = image_1),
Select . Item (value = "image 2" , label = "image 2" , content = image_2),
Select . Item (value = "image 3" , label = "image 3" , content = image_3),
]
Create Select
widget.
Copy select_items = Select (items = items)
Initialize OneOf
widget
Copy one_of = OneOf (conditional_widget = select_items)
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 card = Card (
title = "One of" ,
content = Container (widgets = [select_items, one_of]),
)
layout = Container (widgets = [card])
Create app using layout
Create an app object with layout parameter.
Copy app = sly . Application (layout = layout)