Supervisely
About SuperviselyEcosystemContact usSlack
  • 💻Supervisely Developer Portal
  • 🎉Getting Started
    • Installation
    • Basics of authentication
    • Intro to Python SDK
    • Environment variables
    • Supervisely annotation format
      • Project Structure
      • Project Meta: Classes, Tags, Settings
      • Objects
      • Tags
      • Image Annotation
      • Video Annotation
      • Point Clouds Annotation
      • Point Cloud Episode Annotation
      • Volumes Annotation
    • Python SDK tutorials
      • Images
        • Images
        • Image and object tags
        • Spatial labels on images
        • Keypoints (skeletons)
        • Multispectral images
        • Multiview images
        • Advanced: Optimized Import
        • Advanced: Export
      • Videos
        • Videos
        • Video and object tags
        • Spatial labels on videos
      • Point Clouds
        • Point Clouds (LiDAR)
        • Point Cloud Episodes and object tags
        • 3D point cloud object segmentation based on sensor fusion and 2D mask guidance
        • 3D segmentation masks projection on 2D photo context image
      • Volumes
        • Volumes (DICOM)
        • Spatial labels on volumes
      • Common
        • Iterate over a project
        • Iterate over a local project
        • Progress Bar tqdm
        • Cloning projects for development
    • Command Line Interface (CLI)
      • Enterprise CLI Tool
        • Instance administration
        • Workflow automation
      • Supervisely SDK CLI
    • Connect your computer
      • Linux
      • Windows WSL
      • Troubleshooting
  • 🔥App development
    • Basics
      • Create app from any py-script
      • Configuration file
        • config.json
        • Example 1. Headless
        • Example 2. App with GUI
        • v1 - Legacy
          • Example 1. v1 Modal Window
          • Example 2. v1 app with GUI
      • Add private app
      • Add public app
      • App Compatibility
    • Apps with GUI
      • Hello World!
      • App in the Image Labeling Tool
      • App in the Video Labeling Tool
      • In-browser app in the Labeling Tool
    • Custom import app
      • Overview
      • From template - simple
      • From scratch - simple
      • From scratch GUI - advanced
      • Finding directories with specific markers
    • Custom export app
      • Overview
      • From template - simple
      • From scratch - advanced
    • Neural Network integration
      • Overview
      • Serving App
        • Introduction
        • Instance segmentation
        • Object detection
        • Semantic segmentation
        • Pose estimation
        • Point tracking
        • Object tracking
        • Mask tracking
        • Image matting
        • How to customize model inference
        • Example: Custom model inference with probability maps
      • Serving App with GUI
        • Introduction
        • How to use default GUI template
        • Default GUI template customization
        • How to create custom user interface
      • Inference API
      • Training App
        • Overview
        • Tensorboard template
        • Object detection
      • High level scheme
      • Custom inference pipeline
      • Train and predict automation model pipeline
    • Advanced
      • Advanced debugging
      • How to make your own widget
      • Tutorial - App Engine v1
        • Chapter 1 Headless
          • Part 1 — Hello world! [From your Python script to Supervisely APP]
          • Part 2 — Errors handling [Catching all bugs]
          • Part 3 — Site Packages [Customize your app]
          • Part 4 — SDK Preview [Lemons counter app]
          • Part 5 — Integrate custom tracker into Videos Annotator tool [OpenCV Tracker]
        • Chapter 2 Modal Window
          • Part 1 — Modal window [What is it?]
          • Part 2 — States and Widgets [Customize modal window]
        • Chapter 3 UI
          • Part 1 — While True Script [It's all what you need]
          • Part 2 — UI Rendering [Simplest UI Application]
          • Part 3 — APP Handlers [Handle Events and Errors]
          • Part 4 — State and Data [Mutable Fields]
          • Part 5 — Styling your app [Customizing the UI]
        • Chapter 4 Additionals
          • Part 1 — Remote Developing with PyCharm [Docker SSH Server]
      • Custom Configuration
        • Fixing SSL Certificate Errors in Supervisely
        • Fixing 400 HTTP errors when using HTTP instead of HTTPS
      • Autostart
      • Coordinate System
      • MLOps Workflow integration
    • Widgets
      • Input
        • Input
        • InputNumber
        • InputTag
        • BindedInputNumber
        • DatePicker
        • DateTimePicker
        • ColorPicker
        • TimePicker
        • ClassesMapping
        • ClassesColorMapping
      • Controls
        • Button
        • Checkbox
        • RadioGroup
        • Switch
        • Slider
        • TrainValSplits
        • FileStorageUpload
        • Timeline
        • Pagination
      • Text Elements
        • Text
        • TextArea
        • Editor
        • Copy to Clipboard
        • Markdown
        • Tooltip
        • ElementTag
        • ElementTagsList
      • Media
        • Image
        • LabeledImage
        • GridGallery
        • Video
        • VideoPlayer
        • ImagePairSequence
        • Icons
        • ObjectClassView
        • ObjectClassesList
        • ImageSlider
        • Carousel
        • TagMetaView
        • TagMetasList
        • ImageAnnotationPreview
        • ClassesMappingPreview
        • ClassesListPreview
        • TagsListPreview
        • MembersListPreview
      • Selection
        • Select
        • SelectTeam
        • SelectWorkspace
        • SelectProject
        • SelectDataset
        • SelectItem
        • SelectTagMeta
        • SelectAppSession
        • SelectString
        • Transfer
        • DestinationProject
        • TeamFilesSelector
        • FileViewer
        • Dropdown
        • Cascader
        • ClassesListSelector
        • TagsListSelector
        • MembersListSelector
        • TreeSelect
        • SelectCudaDevice
      • Thumbnails
        • ProjectThumbnail
        • DatasetThumbnail
        • VideoThumbnail
        • FolderThumbnail
        • FileThumbnail
      • Status Elements
        • Progress
        • NotificationBox
        • DoneLabel
        • DialogMessage
        • TaskLogs
        • Badge
        • ModelInfo
        • Rate
        • CircleProgress
      • Layouts and Containers
        • Card
        • Container
        • Empty
        • Field
        • Flexbox
        • Grid
        • Menu
        • OneOf
        • Sidebar
        • Stepper
        • RadioTabs
        • Tabs
        • TabsDynamic
        • ReloadableArea
        • Collapse
        • Dialog
        • IFrame
      • Tables
        • Table
        • ClassicTable
        • RadioTable
        • ClassesTable
        • RandomSplitsTable
        • FastTable
      • Charts and Plots
        • LineChart
        • GridChart
        • HeatmapChart
        • ApexChart
        • ConfusionMatrix
        • LinePlot
        • GridPlot
        • ScatterChart
        • TreemapChart
        • PieChart
      • Compare Data
        • MatchDatasets
        • MatchTagMetas
        • MatchObjClasses
        • ClassBalance
        • CompareAnnotations
      • Widgets demos on github
  • 😎Advanced user guide
    • Objects binding
    • Automate with Python SDK & API
      • Start and stop app
      • User management
      • Labeling Jobs
  • 🖥️UI widgets
    • Element UI library
    • Supervisely UI widgets
    • Apexcharts - modern & interactive charts
    • Plotly graphing library
  • 📚API References
    • REST API Reference
    • Python SDK Reference
Powered by GitBook
On this page
  • Introduction
  • How to debug this tutorial
  • Python code
  • Import libraries
  • Init API client
  • Init variables with demo data
  • Load information about app from Ecosystem
  • App input arguments
  • Start application
  • Wait until app is finished
  • List existing app sessions in Team
  • Recap

Was this helpful?

Edit on GitHub
  1. Advanced user guide
  2. Automate with Python SDK & API

Start and stop app

This guide explains how to manage application sessions using API

PreviousAutomate with Python SDK & APINextUser management

Last updated 6 months ago

Was this helpful?

Introduction

has hundreds of different apps from different categories: from import and data manipulation to neural network training. The huge value for end users is in applications.

However, sometimes it is needed to run multiple apps in a specific order to solve custom task and it may be a bit inconvenient or boring for users. Supervisely SDK allows automate this procedure and build custom pipelines of apps where multiple apps can be run one by one or in parallel in a specific order.

This tutorial demonstrates basic functionality for managing app sessions from Python code: how to start and stop an app, how to wait for a task to end, handle exceptions and iterate over application sessions in your team.

📗 Everything you need to reproduce : source code and demo data.

How to debug this tutorial

Step 1. Prepare ~/supervisely.env file with credentials.

Step 2. Clone with source code and demo data and create .

git clone https://github.com/supervisely-ecosystem/automation-with-python-sdk-and-api
cd automation-with-python-sdk-and-api
./create_venv.sh

Step 3. Open repository directory in Visual Studio Code.

code -r .

Step 4. Go into the folder with the source code for the current tutorial and prepare values inexamples/start-stop-app/local.env file before debug

Step 5. Start debugging examples/start-stop-app/main.py

Python code

Import libraries

import os
import json
from dotenv import load_dotenv
import supervisely as sly

Init API client

Init API for communicating with Supervisely Instance. First, we load environment variables with credentials and additional demo values from local.env :

load_dotenv("examples/start-stop-app/local.env")
load_dotenv(os.path.expanduser("~/supervisely.env"))
api = sly.Api()

Init variables with demo data

We are going to run export-to-pascal-voc application. You can change it to any other app and slightly change the script.

team_id = sly.env.team_id()
workspace_id = sly.env.workspace_id()
agent_id = sly.env.agent_id()
project_id = sly.env.project_id()
app_slug = "supervisely-ecosystem/export-to-pascal-voc"

ℹ️ If you need to run a private application, you can obtain app_slug from the application's page:

Load information about app from Ecosystem

To work with app sessions we need module_id - identifier of application in Ecosystem.

module_id = api.app.get_ecosystem_module_id(app_slug)
# module_id = 83  # or copy module ID of application in ecosystem
module_info = api.app.get_ecosystem_module_info(module_id)
print("Start app: ", module_info.name)

Output:

Start app:  Export to Pascal VOC

It can be obtained by using application slug (text identifier in Ecosystem) value in the following formatsupervisely-ecosystem/<repository-name>)

Or alternatively, you can copy module ID from the application page in Ecosystem on your Supervisely Instance.

App input arguments

Every app has its own input arguments. They are defined by optional configurable parameters in the modal dialog window before the app starts.

And also in most cases there is a valiable for the context menu of some Supervisely entity. For example, if the application starts from the context menu of images project - id of this project is the required input argument.

Supervisely SDK alows to get the list of needed arguments for every app and also validates missing ones automatically.

print("List of available app arguments for developers (like --help in terminal):")
module_info.arguments_help()

Output:

List of available app arguments for developers (like --help in terminal):
App 'Export to Pascal VOC' has additional options that can be configured manually 
in modal dialog window before running app. You can change them or keep defaults: 
{
    "pascalContourThickness": 3,
    "trainSplitCoef": 0.8
}
App has to be started from the context menus:
images_project : Context menu of images project. Target value is project id.
It is needed to call get_arguments method with defined target 
argument (pass one of the values above).

As you can see from the output, the application has two optional values with already defined default values (we can change them) and that app starts from the context menu of the images project. It means that one argument images_project is required.

Python SDK allows to generate needed arguments and validate them:

params = module_info.get_arguments(images_project=project_id)
print("App input arguments with predefined default values:")
print(json.dumps(params, indent=4))

# Let's modify some optional input arguments for this app:
params["trainSplitCoef"] = 0.7
params["pascalContourThickness"] = 2

Here is the correspondence between input arguments and the modal window. It is worth mentioning that now all apps have paramaters in modal window. So that arguments_help method helps to identify existing parameters automatically.

Start application

session = api.app.start(
    agent_id=agent_id,
    module_id=module_id,
    workspace_id=workspace_id,
    task_name="custom session name",
    params=params,
)
print("App is started, task_id = ", session.task_id)
print(session)

Output:

App is started, task_id =  21043
SessionInfo(task_id=21043, user_id=6, module_id=83, app_id=578, details={})

Wait until app is finished

Option 1 - wait task finish or specific task status

api.app.wait(session.task_id, target_status=api.task.Status.FINISHED)

Option 2 - wait until task end without any limitations (can work infinite in the worst case)

api.task.wait(session.task_id)

Option 3 - set timeout and handle corresponding exceptions:

try:
    # it is also possible to limit maximum waiting time
    # in the example below maximum waiting time will be 20*3=60 seconds
    api.app.wait(
        session.task_id,
        target_status=api.task.Status.FINISHED,
        attempts=20,
        attempt_delay_sec=3,
    )

except sly.WaitingTimeExceeded as e:
    print(e)
    # we don't want to wait more, let's stop our long-lived or "zombie" task
    api.app.stop(session.task_id)
except sly.TaskFinishedWithError as e:
    print(e)

print("Task status: ", api.app.get_status(session.task_id))

As you can see there is a method for getting the app session status api.app.get_status. You can use it to implement your custom logic for waitings or timeouts.

Method api.app.stop is used to send stop signal to the app.

List existing app sessions in Team

Let's list all sessions of a specific app in our team with additional optional filtering by statuses [finished].

sessions = api.app.get_sessions(
    team_id=team_id, module_id=module_id, statuses=[api.task.Status.FINISHED]
)
for session_info in sessions:
    print(session_info)

Output:

SessionInfo(task_id=21043, user_id=6, module_id=83, app_id=578, details={"...": "... dict with lots of technical information"})
SessionInfo(task_id=21042, user_id=6, module_id=83, app_id=578, details={"...": "... dict with lots of technical information"})
SessionInfo(task_id=21041, user_id=6, module_id=83, app_id=578, details={"...": "... dict with lots of technical information"})

Recap

In this tutorial we learned how to use python to

  • get information from Ecosystem about certain application

  • how to work and prepare input arguments for application

  • how to start application

  • how to wait session of application, monitor its status (task status) and handle exceptions

  • how to stop application

Slug for a private application
Application slug - text identifier in Ecosystem
Copy module ID from application page in Ecosystem
App input arguments and modal dialog window
😎
Supervisely Ecosystem
this tutorial is on GitHub
repository
Virtual Environment
Learn more here.