-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: change plotly figure widget to allow image annotation #285
Changes from 2 commits
52c7b5e
b71aeff
7aed04d
a4db7ad
1255f02
29935be
6f2487f
63585de
c8e89f9
592678e
6da86b8
8f09f8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""# 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" | ||
shapes = solara.reactive(None) | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed? Does plotly give back some objects? If so, it would be good to explain this in this piece of code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, I added more explanation about it. |
||
|
||
@solara.component | ||
def Page(): | ||
def on_relayout(data): | ||
print(data) | ||
if data is None: | ||
return | ||
|
||
relayout_data = data['relayout_data'] | ||
|
||
if "shapes" in relayout_data: | ||
shapes.value = relayout_data["shapes"] | ||
|
||
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) | ||
|
||
if not shapes.value: | ||
solara.Markdown("## Draw on the canvas") | ||
else: | ||
solara.Markdown("## Data returned by drawing") | ||
formatted_shapes = str(json.dumps(shapes.value, indent=2, cls=CustomEncoder)) | ||
solara.Preformatted(formatted_shapes) |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why was this needed? Did you have trouble running this part? Is there something can do about it?
In any case, for this PR I think you want to revert this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maartenbreddels yes, I had issues running the documentation* and ended up removing this files to run the documentation. This wasn't suppose to be deleted, it should be revered now.
*I followed the setup docs but had problems running the tests and docs. I'll try to setup again later and open some issues if needed.