Application

class Application(*args, **kwargs)[source]

Bases: object

Supervisely application runtime built on top of FastAPI and widgets.

Parameters:
layout : Widget

Main layout of the application.

templates_dir : str, optional

Directory with Jinja2 templates. It is preferred to use layout instead of templates_dir.

static_dir : str, optional

Directory with static files (e.g. CSS, JS), used for serving static content.

hot_reload : bool, optional

Whether to enable hot reload during development (default is False).

session_info_extra_content : Widget, optional

Additional content to be displayed in the session info area.

session_info_solid : bool, optional

Whether to use solid background for the session info area.

ready_check_function : Callable, optional

Function to check if the app is ready for requests.

show_header : bool, optional

Whether to show the header in the application.

hide_health_check_logs : bool, optional

Whether to hide health check logs in info level.

health_check_endpoints : List[str], optional

List of additional endpoints to check health of the app. Add your custom endpoints here to be able to manage logging of health check requests on info level with hide_health_check_logs.

Methods

call_before_shutdown

event

Decorator to register posts to specific endpoints.

get_server

get_static_dir

handle_stop

Contextmanager to suppress StopException and control graceful shutdown.

is_stopped

Indicates whether the application is in the process of being terminated.

reload_page

render

set_ready_check_function

shutdown

stop

exception StopException[source]

Bases: Exception

Raise to stop the function from running in app.handle_stop

add_note()

Exception.add_note(note) – add a note to the exception

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

event(event, use_state=False)[source]

Decorator to register posts to specific endpoints. Supports both async and sync functions.

Parameters:
event

event to register (e.g. Event.Brush.LeftMouseReleased)

use_state : bool, optional

if set to True, data will be extracted from request.state.state, otherwise from request.state.context, defaults to False

Returns:

decorator

Return type:

Callable

Usage Example:
import supervisely as sly

app = sly.Application(layout=layout)

@app.event(sly.Event.Brush.LeftMouseReleased)
def some_function(api: sly.Api, event: sly.Event.Brush.LeftMouseReleased):
    # do something
    pass
handle_stop(graceful=True)[source]

Contextmanager to suppress StopException and control graceful shutdown.

Parameters:
graceful=True

Whether to perform a graceful shutdown if a StopException is raised.

If set to False and shutdown request recieved (i.e. app.is_stopped() is True), the application will be terminated immediately, defaults to True :type graceful: bool :returns: context manager :rtype: _type_

is_stopped()[source]

Indicates whether the application is in the process of being terminated.