-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change plotly figure widget to allow image annotation
- Loading branch information
1 parent
115d423
commit 7aed04d
Showing
2 changed files
with
83 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"""# Image annotation with Solara | ||
This example displays how to annotate images with different drawing tools in plotly figures. Use the canvas | ||
below to draw shapes and visualize the canvas callback. | ||
Check [plotly docs](https://dash.plotly.com/annotations) for more information about image annotation. | ||
""" | ||
import json | ||
import plotly.graph_objects as go | ||
|
||
import solara | ||
|
||
title = "Plotly Image Annotator" | ||
text = solara.reactive('Draw on canvas') | ||
|
||
class CustomEncoder(json.JSONEncoder): | ||
def default(self, o): | ||
# Convert object instances to their string representation | ||
if isinstance(o, object): | ||
return str(o) | ||
return super().default(o) | ||
|
||
@solara.component | ||
def Page(): | ||
def on_relayout(data): | ||
if data is None: | ||
return | ||
|
||
relayout_data = data['relayout_data'] | ||
|
||
if "shapes" in relayout_data: | ||
text.value = str(json.dumps(relayout_data["shapes"], indent=2, cls=CustomEncoder)) | ||
|
||
with solara.Div() as main: | ||
|
||
fig = go.FigureWidget( | ||
layout=go.Layout( | ||
showlegend=False, | ||
autosize=False, | ||
width=600, | ||
height=600, | ||
modebar={ | ||
"add": [ | ||
"drawclosedpath", | ||
"drawcircle", | ||
"drawrect", | ||
"eraseshape", | ||
] | ||
} | ||
) | ||
) | ||
|
||
solara.FigurePlotly(fig, on_relayout=on_relayout) | ||
solara.Preformatted(text.value) | ||
|
||
return main |