heliosdrm / GRUtils.jl

Tools for using the GR framework in Julia
Other
31 stars 6 forks source link

Make show+mimetype work even if the backend has not decided to set that mime_type #86

Closed oxinabox closed 3 years ago

oxinabox commented 3 years ago

If i run show(::IO, "image/png", figure) from the REPL it will do nothing. (and showable will return false) It would be useful if it actually did the thing. we have the technology in GR to save things as PNG In GR.jl: (for a filename) https://github.com/jheinen/GR.jl/blob/39d9cea254d4747ae7db55fdcbca38bc5a92bc17/src/jlgr.jl#L2495-L2501

In Plots.jl (i think easier to apply since it is already a IO) https://github.com/JuliaPlots/Plots.jl/blob/514a7c4fffd05441cf2e3dd1d444f40def7ba7ad/src/backends/gr.jl#L1765-L1788

As I understand it, right now, show(io, mimetype, ::Figure) only works if GR.mime_type is set to the right thing. AFAICT GR.mime_type is a weird global constant the GR sets at start time deoending on if it detect that it is in JuPyTer/Pluto or a bunch of other conditions. I don't get it, and I suspect it is evil, but it is out of scope here.

oxinabox commented 3 years ago

The reason I want this is that then FileIO.jl will work on figures and thus ReferenceTests.jl will work on them

heliosdrm commented 3 years ago

Thanks for the suggestion. To be honest the way in which show works for mime types different from text escapes my knowledge. For GRUtils, I just tried to replicate what GR does. Thus, for producing png files I also wrote a savefig function just like in GR: https://github.com/heliosdrm/GRUtils.jl/blob/f86dfc795eab4af029291b53ab83d02a1305e748/src/frontend.jl#L1437-L1449

Any hint about what should I do to make show(::IO, "image/png", ::Figure) work as wanted will be welcome. :pray:

oxinabox commented 3 years ago

Any hint about what should I do to make show(::IO, "image/png", ::Figure) work as wanted will be welcome. 🙏

I think literally copying what Plots.jl does for GR and updating the types.

Roughly speaking the way the whole display stack works: As I understand it (which may not be perfect).

High level: display(object) calls in and asked all current stack of displays to show an object, it finds one that can and then calls show

Breakdown:

heliosdrm commented 3 years ago

@oxinabox: I'm not sure if #94 fixes the issue in all the relevant cases. Can you try it please? I've made the change only for png and svg, which are the image formats that I have more or less clear how GR deals with.

Probably I could extend this for video formats (MIME"video/mp4" and `MIME"video/webm"). For other formats (pdf, ps...) this straightforward solution does not work.

heliosdrm commented 3 years ago

@oxinabox : is this issue solved by #94?

oxinabox commented 3 years ago

sorry, testing this is in my TODO, but I haven't gotten around to it yet

oxinabox commented 3 years ago

Tested. Works well for me