diff --git a/callbacks/image_viewer.py b/callbacks/image_viewer.py index c6f3f972..aea82bba 100644 --- a/callbacks/image_viewer.py +++ b/callbacks/image_viewer.py @@ -1,4 +1,4 @@ -from dash import Input, Output, State, callback, ctx, clientside_callback +from dash import Input, Output, State, callback, ctx, Patch, clientside_callback import dash_mantine_components as dmc from tifffile import imread import plotly.express as px @@ -8,6 +8,7 @@ @callback( Output("image-viewer", "figure"), + Output("annotation-store", "data", allow_duplicate=True), Output("image-viewer-loading", "zIndex", allow_duplicate=True), Input("image-selection-slider", "value"), State("project-name-src", "value"), @@ -62,8 +63,17 @@ def render_image( if str(image_idx) in annotation_store["annotations"]: fig["layout"]["shapes"] = annotation_store["annotations"][str(image_idx)] + view = annotation_store["view"] + if "xaxis_range_0" in view and annotation_store["image_size"] == tf.size: + fig.update_layout( + xaxis=dict(range=[view["xaxis_range_0"], view["xaxis_range_1"]]), + yaxis=dict(range=[view["yaxis_range_0"], view["yaxis_range_1"]]), + ) + patched_annotation_store = Patch() + patched_annotation_store["image_size"] = tf.size fig_loading_overlay = -1 - return fig, fig_loading_overlay + + return fig, patched_annotation_store, fig_loading_overlay clientside_callback( @@ -92,6 +102,13 @@ def locally_store_annotations(relayout_data, img_idx, annotation_store): """ if "shapes" in relayout_data: annotation_store["annotations"][str(img_idx - 1)] = relayout_data["shapes"] + + if "xaxis.range[0]" in relayout_data: + annotation_store["view"]["xaxis_range_0"] = relayout_data["xaxis.range[0]"] + annotation_store["view"]["xaxis_range_1"] = relayout_data["xaxis.range[1]"] + annotation_store["view"]["yaxis_range_0"] = relayout_data["yaxis.range[0]"] + annotation_store["view"]["yaxis_range_1"] = relayout_data["yaxis.range[1]"] + return annotation_store diff --git a/components/control_bar.py b/components/control_bar.py index f5a882c8..cd36ac0d 100644 --- a/components/control_bar.py +++ b/components/control_bar.py @@ -246,7 +246,13 @@ def layout(): ), dcc.Store( id="annotation-store", - data={"dragmode": "drawopenpath", "visible": True, "annotations": {}}, + data={ + "dragmode": "drawopenpath", + "visible": True, + "annotations": {}, + "view": {}, + "image_size": [], + }, ), dcc.Store(id="project-data"), html.Div(id="dummy-output"),