mosaic icon indicating copy to clipboard operation
mosaic copied to clipboard

Static Image Export in Python

Open harrylojames opened this issue 1 year ago • 3 comments

Hi,

Would it be possible to have static image export functionality similar to that found in Plotly (https://plotly.com/python/static-image-export/)?

  • function to export static image
  • an interactive button to download a screenshot of the current context

There was some similar work done on the great package pyobsplot which also interacts with Observable Plot if of help. https://github.com/juba/pyobsplot/pull/32

harrylojames avatar Dec 09 '24 13:12 harrylojames

Can you explain how that would work from python and how interactivity works with that? I'm also not sure whether we can even support python since we just support widgets in Jupyter and so maybe the functionality should be there? Or are you saying you want a feature to get a rendered chart in a notebook environment?

Right now, I'd recommend printing the website which you can also do from something like playwright.

domoritz avatar Dec 09 '24 13:12 domoritz

Apologies for the ambiguity I'll try expand.

"Or are you saying you want a feature to get a rendered chart in a notebook environment?"

Once the chart has been rendered in a notebook environment I am envisioning the below.

image

While I can get a one off screenshot say using devtools or just manually the use case was that if I share the notebook with someone is there an easy way for them to grab a particular image.

"I'm also not sure whether we can even support python since we just support widgets in Jupyter and so maybe the functionality should be there? "

I don't know enough about anywidget to know if it should live there but it certainly could be a broadly applicable feature. Assuming that was what you meant.

"Can you explain how that would work from python and how interactivity works with that?"

Not sure what the cleanest way to achieve this would look like... The behaviour could be something like:

MosaicWidget(spec, data = {"weather": weather}, path = "output.png")

Where if a path is provided it simply exports a static image.

Or:

fig = MosaicWidget(spec, data = {"weather": weather}, path = "output.png") fig.export("output.png")

Of course this is foregoing any interactivity it just takes the initial view. This to me is still useful as it means I can work with one tool and syntax for both interactive work and when needing to compile figures for print.

Hope that provides some more clarity. These could be split into two separate issues if helpful.

harrylojames avatar Dec 09 '24 15:12 harrylojames

Ahh, thanks for the details. I think we could support a button in the web view to download an image with something like https://stackoverflow.com/questions/6887183/how-to-take-screenshot-of-a-div-with-javascript.

domoritz avatar Dec 09 '24 19:12 domoritz