ReloadableArea widget in Supervisely is a tool that allows dynamically creating and changing widgets in the application UI without reloading the whole page. It can be used to create a dynamic UI that changes depending on user actions without any noticeable delays.
Function signature
ReloadableArea(content=None,widget_id=None,)
Parameters
Parameters
Type
Description
content
Widget
Widget to be displayed in the area.
widget_id
str
ID of the widget
content
Determine the Widget to be displayed in the ReloadableArea.
type:Widget
default value:None
Prepare widgets and put them into a container:
Initialize the ReloadableArea with the container:
widget_id
The ID of the widget.
type:str
default value:None
Initialize widget with ID:
Methods and attributes
Methods and attributes
Description
set_content(content: Widget)
Replaces content of the ReloadableArea with new widget
reload()
Reloads the content of the ReloadableArea widget in UI
Mini app example
You can find this example in our GitHub repository:
We're importing all those widgets just to use them in the demonstration of ReloadableArea.
Init API client
First, we load environment variables with credentials and init API for communicating with Supervisely Instance:
Preparing widgets for app initialization
Initialize ReloadableArea
Adding event handlers for buttons to add and remove widgets
This event will be triggered when the user clicks the Add widget button. The random widget will be chosen from the list and added to the UI.
This event will be triggered when the user clicks the Remove widget button. The last widget in the list will be removed from the UI. In both cases, we need to use the .reload() method of ReloadableArea to show the changes in the UI.
Create an app using the layout
Prepare a layout for the app using Card widget with the content parameter. Then create an app object with the layout parameter.
import os
import supervisely as sly
from dotenv import load_dotenv
from supervisely.app.widgets import (
Card,
ReloadableArea,
Container,
Text,
Input,
InputNumber,
Checkbox,
Button,
Flexbox,
Switch,
Slider,
)
load_dotenv("local.env")
load_dotenv(os.path.expanduser("~/supervisely.env"))
api = sly.Api()
# Create widgets, that will be shown on app initialization.
start_text = Text("Click a button to add or remove random widget.", status="info")
add_widget_button = Button("Add widget")
remove_widget_button = Button("Remove widget", button_type="danger")
buttons_flexbox = Flexbox([add_widget_button, remove_widget_button])
# Create a reloadable area.
reloadable_area = ReloadableArea()
# Put widgets into the Container.
main_container = Container(widgets=[start_text, buttons_flexbox])
# Add the Container to the reloadable area.
reloadable_area.set_content(main_container)
@add_widget_button.click
def add_widget():
# Picking random widget from the list to add to the UI.
random_widget = choice(
[
Input("Random input"),
Checkbox("Random checkbox"),
InputNumber(),
Switch(),
Slider(),
]
)
main_container._widgets.append(random_widget)
# This is the most important part: we need to reload the ReloadableArea
# to show the new widget in th UI.
reloadable_area.reload()
@remove_widget_button.click
def remove_widget():
if len(main_container._widgets) > 2:
main_container._widgets.pop()
# Same as above:
# we need to reload the ReloadableArea when removing the widget.
reloadable_area.reload()
# Creating Card widget, which will contain the ReloadableArea widget.
card = Card(title="ReloadableArea", content=reloadable_area)
# Initializing the application.
app = sly.Application(layout=card)