-
Notifications
You must be signed in to change notification settings - Fork 11
Export a png #23
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
Comments
PNG export would indeed be really useful, but it is much harder to implement than SVG export, because you need a complete HTML renderer (such as a browser) to convert to a bitmap image file. Maybe I'll try to see how other libraries implement this. |
Observable use this. Happy to try adding a function to pyobsplot-js if you think that would make sense |
Oh I didn't know about this, it seems really promising ! Where did you see that Observable uses it ? If you want to try to integrate it in pyobsplot, of course don't hesitate. Maybe the difficulty will be to make it work in both the |
Great - I'll give that a go. It was mentioned in a discussion here. |
This was unfortunately the case for html-to-image "but it is much harder to implement than SVG export, because you need a complete HTML renderer (such as a browser) to convert to a bitmap image file.". In Observable notebooks the figure has already been rendered in the browser so export to png via html-to-image is possible. I've had a look at what other visualisation libraries do and they all appear to take a similar approach as described here. A simple approach that worked but proved limited was using the python package html2image. The issue I had was getting the correct width and height. If the html just contains an SVG tag then there's no issue we can grab the width and height attributes. However, if there is a title/legend/caption etc. then the height will be too small. Bokeh has an implementation to dynamically get the width and height which we could try and borrow when the figure is a HTML object (export_to_png and dynamically estimate width & height )? Not ideal, there would also need to be an additional docs section similar to this. Let me know what you think |
For including observable plots in a scientific paper I also came across the need of exporting into png, pdf or svg (a single svg including title, caption, legend, etc). For my needs I got the best results using typst, see a few examples in my repo. For me this approach worked much better than anything I tried using headless browser exports. The resulting figures have tight configurable margins without cutting off title or caption. Supported formats are The entire code for the export logic is in obsplot.py, the additional external dependencies are Of course such an reverse-engineering type approach might not be ideal for everyone, in particular my script so far probably does not cover all observable features (title, subtitle, caption, discrete legends, continuous legends are supported, but maybe something else is missing?). So far themes are also not supported, although that would be an easy addition. If there is interest here despite these drawbacks, I'd be happy to open a pull request. |
That is very interesting, I didn't know that typst could do this type of thing and that it could be installed directly via pip. I'll take a look at your code, many thanks for taking the time to describe the way you did this and to share your code. |
I added this functionality to Another issue I could not solve is making this work with
but this fails for Plot.auto.
|
Very excited about the prospect of this addition and would love to see a pull request! Are there any blockers to making it happen? If we aren't sure yet that it supports all permutations would a way forward in the short term be to note the functionality is experimental? Going to try the code above and I'll feedback if something doesn't work. |
Sorry I've been busy and didn't work a lot on |
Experimental PNG support added to development version. |
It would be great to be able to be able to export a png and have it include the title, subtitle and figcaption as is the case when using the three dots to export a png here.
Split out from this #21
The text was updated successfully, but these errors were encountered: