get_data_item
FunctionSource
item: AnnoItem = get_data_item(
data: Annotations | [AnnoItem],
id: str,
)
Get an annotation item by its unique ID.
Note that this method will not validate the format of data
. If the item cannot
be found, will raise a KeyError
.
Aliases
This function can be acquired by
import dash_picture_annotation as dpa
dpa.get_data_item
dpa.utilities.get_data_item
Arguments
Requires
Argument | Type | Required | |
---|---|---|---|
data | Annotations | [AnnoItem] | The annotation data that is queried. | |
id | str | The ID of the annotation item to be found. |
Returns
Argument | Type | |
---|---|---|
item | AnnoItem | The queried value. If not found, raise an KeyError . |
Examples
Get an annotation item by quering its ID
- Codes
- Results
get_anno_item_by_id.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(data, "a"))
pprint.pprint(dpa.get_data_item(data_wrapped, "a"))
pprint.pprint(dpa.get_data_item(data, "c"))
pprint.pprint(dpa.get_data_item(data_wrapped, "c"))
try:
pprint.pprint(dpa.get_data_item(data, "d"))
except KeyError as e:
print(e)
{'id': 'a', 'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 0, 'y': 0}}
{'id': 'a', 'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 0, 'y': 0}}
{'id': 'c', 'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 0, 'y': 0}}
{'id': 'c', 'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 0, 'y': 0}}
'The ID "d" is not found in the data.'