Convert widget into image of type "image/jpeg" based on event
kolibril13 opened this issue · comments
Hi there!
When I share a notebook with colleagues, it would be great if they could directly see an image of the widget.
Currently, a widget would be displayed like Image(value=b'\xff\xd8\xff\xe0\x00\x10JFIF ...
when reopening the notebook or displaying it on GitHub.
Code example:
import requests
response = requests.get("https://github.com/Octoframes/jupyter_capture_output/blob/main/assets/cute_dog.jpg?raw=true")
import ipywidgets
ipywidgets.Image(value = response.content, format='jpg', width=500)
In order to display the actual content of the image, I can add the mimebundle["image/jpeg"] = encoded_image
like this:
import ipywidgets
import requests
response = requests.get("https://github.com/Octoframes/jupyter_capture_output/blob/main/assets/cute_dog.jpg?raw=true")
class MyImageWidget(ipywidgets.Image):
def _repr_mimebundle_(self, *args, **kwargs):
mimebundle = super()._repr_mimebundle_(*args, **kwargs)
import base64
encoded_image = base64.encodebytes(self.value).decode("utf-8")
mimebundle["image/jpeg"] = encoded_image
metadata = {"image/jpeg": {"width": self.width}}
return mimebundle, metadata
MyImageWidget(value = response.content, format='jpg', width="500px")
![image](https://private-user-images.githubusercontent.com/44469195/290848506-ec3b1048-17e4-430e-bfc2-6ab8e7be4f74.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTA3MjU5ODIsIm5iZiI6MTcxMDcyNTY4MiwicGF0aCI6Ii80NDQ2OTE5NS8yOTA4NDg1MDYtZWMzYjEwNDgtMTdlNC00MzBlLWJmYzItNmFiOGU3YmU0Zjc0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDAzMTglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwMzE4VDAxMzQ0MlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTQ0YTI2MzBkMTVjYWYzMTYzZTIxZTdiOGEwZDhmNjIwMWM0NGFlZWQ2ZDEwN2RiMmQ0NTJlNjllYjUzNzQ1NTQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.hL4aJDwXD99Q70doH5JHcXrHAcFEVBlyZbs1NDdy2Ok)
but this would make the image static directly.
It would be great to make a widget static based on an event after all interaction is done.
This event could e.g. be a button like this one:
button_value = False
import ipywidgets
button = ipywidgets.Button(description = "Embed me!")
def on_button_clicked(b):
button_value = True
b.description = "True"
button.on_click(on_button_clicked)
display(button)
I'm grateful for any ideas on how to convert a widget into a static image based on an event :)
cc. @stevejpurves, you might be interested in this feature as well for Myst-markdown documents.
This issue is related to manzt/anywidget#396 .