1

我正在尝试将时间戳列添加到此 Python Dash 示例中显示的表中:

https://github.com/plotly/dash-sample-apps/blob/main/apps/dash-image-annotation/app.py

目的是为每个创建的对象设置一个时间戳。

到目前为止,我设法:

  1. 在输出表中新建一列(Github 代码第 48 行)

  2. 尝试通过将其添加到“modify_table_entries”函数(Github 代码中的第 466 行)来附加时间戳:

    annotations_table_data[0]["timestamp"] = time_passed(annotations_store_data["starttime"])
    

这仅给了我输出表中第一个条目的时间戳:

在此处输入图像描述

然而,我现在尝试了几天。我相信我必须在代码的这个函数中以某种方式将时间戳附加到每个创建的对象:

def modify_table_entries(
    previous_n_clicks,
    next_n_clicks,
    graph_relayoutData,
    annotations_table_data,
    image_files_data,
    annotations_store_data,
    annotation_type,
):
    cbcontext = [p["prop_id"] for p in dash.callback_context.triggered][0]
    if cbcontext == "graph.relayoutData":
        #debug_print("graph_relayoutData:", graph_relayoutData)
        #debug_print("annotations_table_data before:", annotations_table_data)
        if "shapes" in graph_relayoutData.keys():
            # this means all the shapes have been passed to this function via
            # graph_relayoutData, so we store them
            annotations_table_data = [
                shape_to_table_row(sh) for sh in graph_relayoutData["shapes"]
            ]            
        elif re.match("shapes\[[0-9]+\].x0", list(graph_relayoutData.keys())[0]):
            # this means a shape was updated (e.g., by clicking and dragging its
            # vertices), so we just update the specific shape
            annotations_table_data = annotations_table_shape_resize(
                annotations_table_data, graph_relayoutData
            )
        if annotations_table_data is None:
            return dash.no_update
        else:
            debug_print("annotations_table_data after:", annotations_table_data)
            annotations_table_data[0]["timestamp"] = time_passed(annotations_store_data["starttime"])
            return (annotations_table_data, image_files_data)
    image_index_change = 0
    if cbcontext == "previous.n_clicks":
        image_index_change = -1
    if cbcontext == "next.n_clicks":
        image_index_change = 1
    image_files_data["current"] += image_index_change
    image_files_data["current"] %= len(image_files_data["files"])
    if image_index_change != 0:
        # image changed, update annotations_table_data with new data
        annotations_table_data = []
        filename = image_files_data["files"][image_files_data["current"]]
        #debug_print(annotations_store_data[filename])
        for sh in annotations_store_data[filename]["shapes"]:
            annotations_table_data.append(shape_to_table_row(sh))
        return (annotations_table_data, image_files_data)
    else:
        return dash.no_update

任何帮助深表感谢!

4

0 回答 0