Introduction
This widget is a simple and intuitive interface element that allows users to select given option. The Checkbox
widget can be customized with different label text and default state. By providing an easy and efficient way to make selections, the Checkbox
widget is an essential tool for any image or video annotation project.
Function signature
Copy checkbox = Checkbox (
content = "Enable" ,
checked = False ,
widget_id = None ,
)
Parameters
Parameters Type Description Return True
if checkbox is checked
content
Checkbox content.
type: Union[Widget, str]
Copy checkbox = Checkbox (content = "Enable" )
checked
Whether Checkbox is checked. Return True
if checked.
type: bool
default value: False
Copy checkbox = Checkbox (content = "Enable" , checked = True )
widget_id
ID of the widget.
type: str
default value: None
Methods and attributes
Attributes and Methods Description Return True
if checked, else False
.
Disable checked
property.
Decorator function is handled when checkbox value is changed.
Mini App Example
You can find this example in our Github repository:
ui-widgets-demos/controls/002_checkbox/src/main.py
Import libraries
Copy import os
import supervisely as sly
from dotenv import load_dotenv
from supervisely . app . widgets import (
Button ,
Card ,
Checkbox ,
Container ,
NotificationBox ,
SelectDataset ,
)
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
we will use
Copy project_id = sly . env . project_id ()
Initialize Checkbox
widget
Copy checkbox = Checkbox (
content = "Enable" ,
checked = False ,
)
Create more widgets
In this tutorial we will use SelectDataset
, Button
, NotificationBox
widgets to show how to control Checkbox
widget from python code.
Copy select_dataset = SelectDataset (
project_id = project_id,
compact = True ,
)
show_btn = Button (text = "Show info" )
note = NotificationBox ()
note . hide ()
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 # create new cards
card = Card (
title = "Checkbox demo" ,
content = Container (widgets = [select_dataset, checkbox, show_btn, note]),
)
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 widget from python code
Copy @checkbox . value_changed
def hide_notification ( value ):
if value is True :
select_dataset . disable ()
else :
select_dataset . enable ()
note . hide ()
@show_btn . click
def show_info ():
images_count = 0
if checkbox . is_checked ():
datasets_list = api . dataset . get_list (project_id = project_id)
select_dataset . disable ()
for dataset in datasets_list :
images_count += dataset . images_count if dataset . images_count is not None else 0
else :
ds_id = select_dataset . get_selected_id ()
dataset = api . dataset . get_info_by_id (ds_id)
images_count = dataset . images_count
note . title = f "Total count of images in selected datasets: { images_count } ."
note . show ()