ApiContext

class ApiContext(api, project_id=None, dataset_id=None, project_meta=None, with_alpha_masks=True)[source]

Bases: object

Context manager for the API object for optimization purposes.

Use this context manager when you need to perform a series of operations on the same project or dataset. It allows you to avoid redundant API calls to get the same project or dataset info multiple times.

Parameters:
api

API object.

project_id : int, optional

Project ID.

dataset_id : int, optional

Dataset ID.

project_meta=None

ProjectMeta object.

Raises:

RuntimeError – if api is None.

Usage Example:
import os
from dotenv import load_dotenv

import supervisely as sly

# Load secrets and create API object from .env file (recommended)
# Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication
if sly.is_development():
    load_dotenv(os.path.expanduser("~/supervisely.env"))

api = sly.Api.from_env()

with ApiContext(
    api,
    project_id=33333,
    dataset_id=99999,
    project_meta=project_meta,
    with_alpha_masks=True,
):
    api.annotation.upload_paths(image_ids, ann_paths, anns_progress)
    # another code here

Methods