get_data_item
函数源码
item: AnnoItem = get_data_item(
    data: Annotations | [AnnoItem],
    id: str,
)
按照唯一ID获取一个标记数据项。
注意,该方法不会验证数据data的格式。若未找到数据项,抛出KeyError。
别名
该函数可以按以下方式之一获取
import dash_picture_annotation as dpa
dpa.get_data_item
dpa.utilities.get_data_item
参数
输入
| 参数 | 类型 | 必选 | |
|---|---|---|---|
data | Annotations | [AnnoItem] | 要检索的标记数据。 | |
id | str | 需要查询的标记数据项ID。 | 
输出
| 参数 | 类型 | |
|---|---|---|
item | AnnoItem | 检索到的标记数据项。未找到时,抛出KeyError。 | 
范例
按照ID检索标记数据项
- 代码
 - 结果
 
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.'