Skip to main content

get_data_item_with_default

FunctionSource

item: AnnoItem | T = get_data_item_with_default(
data: Annotations | [AnnoItem],
id: str,
default: T = None,
)

Get an annotation item by its unique ID with a default value provided.

Note that this method will not validate the format of data. If the item cannot be found, will return the default value.

Aliases

This function can be acquired by

import dash_picture_annotation as dpa


dpa.get_data_item_with_default
dpa.utilities.get_data_item_with_default

Arguments

Requires

ArgumentTypeRequired
Description
dataAnnotations | [AnnoItem]The annotation data that is queried.
idstrThe ID of the annotation item to be found.
defaultTIf the ID is not found in the data, return this value.

Returns

ArgumentType
Description
itemAnnoItem | TThe queried value or the default value given by this method.

Examples

Get an annotation with a default value

get_anno_item_by_id_with_default.py
import pprint
import dash_picture_annotation as dpa


data = [
{"id": "a", "mark": {"x": 0, "y": 0, "width": 0, "height": 0, "type": "RECT"}},
{"id": "b", "mark": {"x": 0, "y": 0, "width": 0, "height": 0, "type": "RECT"}},
{"id": "c", "mark": {"x": 0, "y": 0, "width": 0, "height": 0, "type": "RECT"}},
{"id": "c", "mark": {"x": 1, "y": 1, "width": 0, "height": 0, "type": "RECT"}},
]

data_wrapped = {"timestamp": 0, "data": data}

pprint.pprint(dpa.get_data_item_with_default(data, "a"))
pprint.pprint(dpa.get_data_item_with_default(data_wrapped, "a"))
pprint.pprint(dpa.get_data_item_with_default(data, "c"))
pprint.pprint(dpa.get_data_item_with_default(data_wrapped, "c"))
pprint.pprint(dpa.get_data_item_with_default(data, "d"))
pprint.pprint(dpa.get_data_item_with_default(data, "d", 0))