python -m venv .venv
source ./venv/bin/activate
Getting started
3 minute read
Getting started with the Python SDK
-
Create a virtual environment and activate it.
NoteCheck out the official documentation for further instructions and other platforms. -
Install the latest datavision-beeyard-sdk package.
python -m pip install datavision-beeyard-sdk
-
Also install Pillow to be able to run the examples that work with images.
python -m pip install pillow
-
Login into BeeYard.
-
You can login using username, password and client id
import datavision_beeyard_sdk from datavision_beeyard_sdk import AuthenticatedClient uri = "https://demo.beeyard.ai/hive/" username = "yourusername" password = "yourpassword" id = "byard" client = AuthenticatedClient(uri, username, password, client_id=id)
-
Or you can login using client credentials (client id and client secret)
import datavision_beeyard_sdk from datavision_beeyard_sdk import AuthenticatedClient uri = "https://demo.beeyard.ai/hive/" id = "yourclientid" secret = "yourclientsecret" login_uri = "https://demo.beeyard.ai/id/" client = AuthenticatedClient( uri, grant_type="client_credentials", client_id=id, client_secret=secret, login_url=login_uri, )
-
Reading Hive system information
-
Liveness
from datavision_beeyard_sdk.api.health import get_liveness liveness = get_liveness.live(client=client) print("Live: %s\n" % (liveness))
-
Readiness
from datavision_beeyard_sdk.api.health import get_readiness readiness = get_readiness.ready(client=client) print("Ready: %s\n" % (readiness))
-
Version
from datavision_beeyard_sdk.api.version import read_version version = read_version.version(client=client) print("System version: %s\n" % version)
Working with Workspaces
-
List available Workspaces.
from datavision_beeyard_sdk.api.workspace import search_workspaces resp = search_workspaces.search(client=client) if resp: print("List of available workspaces: ") for ws in range(len(resp)): print("\t",resp[ws]["name"])
-
Create a new Workspace.
from datavision_beeyard_sdk.models.workspace_descriptor_dto import WorkspaceDescriptorDto from datavision_beeyard_sdk.api.workspace import create_workspace import json name = "beeyard_sdk_test" workspace_desc = WorkspaceDescriptorDto(name=name, namespace=name) ws = create_workspace.create(client=client, request_body=workspace_desc) ws_id = json.loads(ws.content)["workspaceId"] print("Workspace created\n")
Working with Cells
-
Add tag templates to the Workspace.
from datavision_beeyard_sdk.models.tag_template_dto import TagTemplateDto from datavision_beeyard_sdk.api.workspace import add_tag_templates temp1 = TagTemplateDto(section="fruit", name="apple") temp2 = TagTemplateDto(section="fruit", name="banana") temp3 = TagTemplateDto(section="car", name="skoda") temlpates_to_add = [temp1, temp2, temp3] resp = add_tag_templates.add( workspace_id=ws_id, client=client, tag_list=temlpates_to_add ) print("Tag templates added\n")
-
Create a Cell in the just created Workspace.
from datavision_beeyard_sdk.api.cell import create_cell cell = create_cell.create(client=client, namespace=name) time_created = json.loads(cell.content.decode())["descriptor"]["created"] print("Cell created\n")
-
Search Cells.
from datavision_beeyard_sdk.api.cell import search_cells cells_available = search_cells.query(client=client, namespace=name) for c in cells_available: if c["created"][:19] == time_created[:19]: cell_id = c["id"] print(cell_id) print("\n")
-
Add description to the Cell.
from datavision_beeyard_sdk.models.modify_cell_input_dto import ModifyCellInputDto from datavision_beeyard_sdk.api.cell import modify_cell_description modifier = ModifyCellInputDto("My first Cell created from Python SDK") resp = modify_cell_description.update_description( id=cell_id, client=client, description=modifier ) print("Description added\n")
-
Add Tag to just created Cell.
from datavision_beeyard_sdk.models.tag_dto import TagDto from datavision_beeyard_sdk.api.cell import add_tags new_tag = TagDto(category="beeyard_sdk_test.fruit", name="apple") tags_to_add = [new_tag] resp = add_tags.add(id=cell_id, client=client, tag_list=tags_to_add) print("Tag added\n")
-
Add Property to just created Cell.
from datavision_beeyard_sdk.models.property_dto import PropertyDto from datavision_beeyard_sdk.api.cell import add_properties prop = PropertyDto(key="key1", value="Example property") props_to_add = [prop] resp = add_properties.add(id=cell_id, client=client, props_list=props_to_add) print("Property added\n")
-
Add image to Cell.
from PIL import Image import io from datavision_beeyard_sdk.models.add_images_multipart_data import AddImagesMultipartData from datavision_beeyard_sdk.api.image import add_images from datavision_beeyard_sdk.types import File im = Image.new("RGB", (300, 200), color="green") tmp = io.BytesIO() im.save(tmp, format="png") data = tmp.getvalue() im.close() f = File(payload=data, file_name="test.png", mime_type="image/png") image_to_load = AddImagesMultipartData([f]) resp = add_images.add(id=cell_id, client=client, multipart_data=image_to_load) print("Image added\n")
-
Read image from Cell.
from datavision_beeyard_sdk.api.file import read_file file_name = "test.png" resp = read_file.read(id=cell_id, filename=file_name, client=client) image = Image.open(io.BytesIO(resp)) im = image.convert("RGB")
-
Remove image from Cell.
from datavision_beeyard_sdk.api.file import remove_files resp = remove_files.remove(id=cell_id, client=client, filenames=["test.png"]) print("File test.png removed\n")
Cleaning up
-
Remove the Cell from the Workspace.
from datavision_beeyard_sdk.api.cell import remove_cell resp = remove_cell.remove(id=cell_id, client=client) print("Cell removed\n")
-
Remove Workspace.
from datavision_beeyard_sdk.api.workspace import remove_workspace name = "beeyard_sdk_test" query = json.dumps({"namespace": name}) resp = search_workspaces.search(client=client, query=query) if resp: ws_id = resp[0]["workspaceId"] resp = remove_workspace.delete(workspace_id=ws_id, client=client) print("Workspace removed\n")