ImageSlider

Introduction

ImageSlider widget in Supervisely is a simple widget that displays images using Slider and is convenient to use when there is no need to add extra functions for displaying annotations or adjusting their settings, but only to display the images passed to it by a list of URLs or local paths.

Function signature

ImageSlider(
    previews=None,
    examples=None,
    combined_data=None,
    height=200,
    selectable=False,
    preview_idx=None,
    preview_url=None,
    widget_id=None,
)
default

Parameters

Parameters
Type
Description

previews

List[str]

List of previews URLs for the Slider widget

examples

List[List[str]]

List of image examples for the Slider widget

combined_data

List[dict]

List of image previews and examples URLs for the Slider widget

height

int

Height of the Slider widget

selectable

bool

Determines whether image selection is enabled or disabled

preview_idx

int

Index of the initially selected image in Slider

preview_url

str

URL of the initially selected image in Slider

widget_id

str

ID of the widget

previews

List of previews URLs for the Slider widget.

type: List[str]

default value: None

image_urls = [
    "https://www.w3schools.com/howto/img_nature.jpg",
    "https://www.quackit.com/pix/samples/18m.jpg",
    "https://i.imgur.com/35pUPD2.jpg",
    "https://i.imgur.com/fB2DBcM.jpeg",
    "https://i.imgur.com/OpSj3JE.jpg",
]

image_slider = ImageSlider(previews=image_urls)
default

examples

List of image examples for the Slider widget.

type: List[List[str]]

default value: None

image_urls = [
    "https://www.w3schools.com/howto/img_nature.jpg",
    "https://www.quackit.com/pix/samples/18m.jpg",
    "https://i.imgur.com/35pUPD2.jpg",
    "https://i.imgur.com/fB2DBcM.jpeg",
    "https://i.imgur.com/OpSj3JE.jpg",
]

example_urls = [
    [
        "https://www.w3schools.com/howto/img_nature.jpg",
        "https://www.quackit.com/pix/samples/18m.jpg",
        "https://i.imgur.com/35pUPD2.jpg",
        "https://i.imgur.com/fB2DBcM.jpeg",
        "https://i.imgur.com/OpSj3JE.jpg",
    ],
    ["https://www.quackit.com/pix/samples/18m.jpg"],
    ["https://i.imgur.com/35pUPD2.jpg"],
    ["https://i.imgur.com/fB2DBcM.jpeg"],
    ["https://i.imgur.com/OpSj3JE.jpg"],
]

image_slider = ImageSlider(previews=image_urls, examples=example_urls)
examples

combined_data

List of image previews and examples URLs for the Slider widget.

type: List[dict]

default value: None

combined_data = [
    {
        "preview": "https://i.imgur.com/0XbKOJ3.jpeg",
        "moreExamples": ["https://i.imgur.com/0XbKOJ3.jpeg", "https://i.imgur.com/mIcObyL.jpeg"],
    },
    {
        "preview": "https://i.imgur.com/3rYHhEu.jpeg",
        "moreExamples": [
            "https://i.imgur.com/3rYHhEu.jpeg",
            "https://i.imgur.com/pSafUhg.jpeg",
            "https://i.imgur.com/yKc0xTb.jpeg",
        ],
    },
]

image_slider = ImageSlider(combined_data=combined_data)

height

Height of the Slider widget.

type: int

default value: 200

image_slider = ImageSlider(data=image_urls, height=100)

selectable

Determines whether image selection is enabled or disabled.

type: bool

default value: False

image_slider = ImageSlider(data=image_urls, selectable=True)

preview_idx

Index of the initially selected image in Slider. Use only if selectable is True.

type: int

default value: None

image_slider = ImageSlider(data=image_urls, selectable=True, preview_idx=4)

preview_url

URL of the initially selected image in Slider. Use only if selectable is True.

type: str

default value: None

image_slider = ImageSlider(
    data=image_urls, selectable=True, preview_url=image_urls[2]
)

widget_id

ID of the widget.

type: str

default value: None

Attributes and Methods
Description

get_selected_preview()

Get URL of the selected image in the slider.

set_selected_preview(value: str)

Sets URL of the image to be displayed as the preview image in the slider.

get_selected_idx()

Retrieves the index of the currently selected image in the slider.

set_selected_idx(value: int)

Sets the index of the image to be displayed as the preview image in the slider.

get_selected_examples()

Retrieves the list of URLs of the currently selected image examples in the slider.

is_selectable

Returns a boolean indicating whether image selection is enabled or disabled.

enable_selection()

Enables image selection.

disable_selection()

Disables image selection.

get_data_length()

Retrieves the length of the image URL list.

get_data()

Retrieves the list of image URLs and examples.

set_data(previews: List[str], examples: List[List[str]], combined_data: List[dict])

Sets the list of image URLs and examples.

append_data(previews: List[str], examples: List[List[str]], combined_data: List[dict])

Extends the list of image URLs and examples.

Mini App Example

You can find this example in our GitHub repository:

supervisely-ecosystem/ui-widgets-demos/media/011_image_slider/src/main.py

import os
import json

from dotenv import load_dotenv
import supervisely as sly
from supervisely.app.widgets import Button, Card, Container, Editor, Flexbox, Text, ImageSlider

Init API client

Init API for communicating with Supervisely Instance. First, we load environment variables with credentials:

if sly.is_development():
    load_dotenv("local.env")
    load_dotenv(os.path.expanduser("~/supervisely.env"))
api = sly.Api()

Prepare data for ImageSlider widget


image_urls = [
    "https://www.w3schools.com/howto/img_nature.jpg",
    "https://www.quackit.com/pix/samples/18m.jpg",
    "https://i.imgur.com/35pUPD2.jpg",
    "https://i.imgur.com/fB2DBcM.jpeg",
    "https://i.imgur.com/OpSj3JE.jpg",
]

example_urls = [
    [
        "https://www.w3schools.com/howto/img_nature.jpg",
        "https://www.quackit.com/pix/samples/18m.jpg",
        "https://i.imgur.com/35pUPD2.jpg",
        "https://i.imgur.com/fB2DBcM.jpeg",
        "https://i.imgur.com/OpSj3JE.jpg",
    ],
    ["https://www.quackit.com/pix/samples/18m.jpg"],
    ["https://i.imgur.com/35pUPD2.jpg"],
    ["https://i.imgur.com/fB2DBcM.jpeg"],
    ["https://i.imgur.com/OpSj3JE.jpg"],
]


combined_data = [
    {
        "preview": "https://i.imgur.com/0XbKOJ3.jpeg",
        "moreExamples": ["https://i.imgur.com/0XbKOJ3.jpeg", "https://i.imgur.com/mIcObyL.jpeg"],
    },
    {
        "preview": "https://i.imgur.com/3rYHhEu.jpeg",
        "moreExamples": [
            "https://i.imgur.com/3rYHhEu.jpeg",
            "https://i.imgur.com/pSafUhg.jpeg",
            "https://i.imgur.com/yKc0xTb.jpeg",
        ],
    },
]

Initialize ImageSlider widget

image_slider = ImageSlider(previews=image_urls, examples=example_urls)

Initialize Text, Editor and Button widgets, we will use

image_url = Text(status="info")
image_index = Text(status="info")
image_examples = Text(status="info")
data_length = Text(status="info")

text_container = Container(widgets=[image_url, image_index, image_examples, data_length])

button_url = Button(text="Get info")
button_set_url = Button(text="Select image by url")
button_set_index = Button(text="Select image by index")
button_extend_data = Button(text="Extend data")
button_set_data = Button(text="Set new data")
button_toggle_selectable = Button(text="Toggle selectable")
button_get_data = Button(text="Get data")

buttons_container = Flexbox(
    widgets=[
        button_url,
        button_set_url,
        button_set_index,
        button_extend_data,
        button_set_data,
        button_toggle_selectable,
        button_get_data,
    ]
)

all_data = Editor(height_lines=20)
all_data.hide()

Create app layout

Prepare a layout for the app using Card widget with the content parameter and place widgets that we've just created in the Container widget. Place order in the Container is important, we want buttons to be displayed above the Text widget.

card = Card(
    title="Image Slider",
    content=Container([image_slider, text_container, buttons_container, all_data]),
)

layout = Container(widgets=[card])

Create an app using the layout

Create an app object with the layout parameter.

app = sly.Application(layout=layout)

Our app layout is ready. It's time to handle button clicks.

Add functions to control widgets from python code

@button_url.click
def get_info():
    image_url.text = f"Image URL: {image_slider.get_selected_preview()}"
    image_index.text = f"Image index on slider: {image_slider.get_selected_idx()}"
    image_examples.text = f"Image examples: {image_slider.get_selected_examples()}"
    data_length.text = f"Data length: {image_slider.get_data_length()}"


@button_set_url.click
def set_url():
    image_slider.set_selected_preview("https://i.imgur.com/OpSj3JE.jpg")
    get_info()


@button_set_index.click
def set_index():
    image_slider.set_selected_idx(2)
    get_info()


@button_extend_data.click
def extend_data():
    image_slider.append_data(combined_data=combined_data[:1])
    get_info()


@button_set_data.click
def set_data():
    image_slider.set_data(combined_data=combined_data)
    get_info()


@button_toggle_selectable.click
def toggle_selectable():
    if image_slider.is_selectable:
        image_slider.disable_selection()
    else:
        image_slider.enable_selection()


@button_get_data.click
def get_data():
    data = image_slider.get_data()
    all_data.set_text(json.dumps(data, indent=4))
    all_data.show()

Last updated