Base.show and plotpane

Hello,

I’m trying to modify the Base.show function to show images in the plot pane of Atom/Juno following the instructions there. But nothing shows in my plot pane when I execute the commands (either directly from the atom REPL or by executing lines in a file opened with atom)

struct Baz
    fn::String
end
Base.show(io::IO, ::MIME"image/png", b::Baz) = write(io, read(b.fn))
Baz("sandbox/test.png")

50%20am

when running it, the plot pane stays empty (I’m showing the last line to show that the file exists). Not sure what I’m doing wrong? I also tried to copy paste the HTML example but same thing happened.

Thanks!

PS:

Julia Version 1.1.0-DEV.663
Commit 26f1ba56f9 (2018-11-15 22:17 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin14.5.0)
  CPU: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Environment:
  JULIA_EDITOR = atom -a
  JULIA_NUM_THREADS = 2

and up to date Atom.jl + Juno.jl

The read was successful by the looks, as you have some Uint8 data. I’d guess it wants base64 data and probably a header. There is a method hiding in Markdown somewhere, but this should also work

using Base64

bazstr = "data:image/png;base64," * base64encode(read(b.fn));

So after much head scratching I finally got it: here’s an approach for others who may be interested (thanks @y4lu for the short hint of using Base64)

using Base64
struct Baz
    fn::String
end

function Base.show(io::IO, b::Baz)
    mime = "image/png"
    suffix = ";base64,"
    str = stringmime(mime, read(b.fn))
    str = string("<img src=\"data:", mime, suffix, str, "\">")
    Juno.render(Juno.PlotPane(), HTML(str))
end

b = Baz("sandbox/test.png")
1 Like

Ah, you could probably also put the full path to the image in the source tag, or even a local path if you know where the working directory is.

I have a similar difficulty with a basic http server, serving text is fine but images have been a bit too tricky so far

That solution isn’t really a good idea – I wouldn’t recommend overloading Juno.render anymore since you do basically everything with Base.show now. If you want to use Juno.render directly then it’d be best to overload it directly for your type instead of going through a show method.


I have no idea why the solution in your original post isn’t working though:

My two best guesses would be that either I changed something on Atom.jl master (or atom-julia-client master) that un-breaks this, or that your png is weird somehow.

I typed pretty much exactly what’s in your screencast (thanks for taking the time) and tried with several random png images found on google images with just the same result

I’ll try to pull Atom#master and report

Edit: did just that and no difference in output (even after fully restarting atom).

FWIW, I also tried the SVG example (copy pasting from the doc, with different svg files) and the HTML example, none showed in the plot pane. Could it be related to the fact that I’m on a Mac? Can anyone else reproduce this?

Works fine on my Mojave iMac, on both Julia 1.0 and 1.1.

thanks, that’s my exact setup, I also tried with both 1.0 and 1.1, up to date Juno, Atom & all packages in the main Atom app. No idea what’s going on then.

Can you open the Atom dev tools with Cmd-Shift-I and see if there are any errors that pop up when you’re trying to plot something?

Can’t see anything there either

No, the show function for image/png should not be producing base64 data, it should be producing raw binary data. (If the display backend needs base64 encoding, it needs to do that itself with the data provided by show.)

Your original Base.show(io::IO, ::MIME"image/png", b::Baz) = write(io, read(b.fn)) code works just fine for me in IJulia. If it doesn’t work in Juno, then you should file a issue in Juno.

In particular, you should not be overloading Base.show(io::IO, b::Baz) to output HTML with embedded base64-encoded HTML. The 2-argument show function should produce plain text — otherwise, non-Juno displays (the REPL, for example), will be broken. If you want HTML output, you should overload show(io::IO, ::MIME"text/html", b::Baz).

1 Like

Ah, what might be going on here is that you have the plot pane disabled in the julia-client settings – can you check that real quick?

Juno doesn’t pick those up by default – you’ll need to opt-in by using the application/prs.juno.plotpane+html mime type as described in the docs.

1 Like

that was it, thanks!! I wasn’t aware there was this setting. Would it make sense to maybe have a default where if the plot pane is opened this setting is set to true by default? thanks again!

Yeah, makes sense. I’ve openend an issue here:
https://github.com/JunoLab/atom-julia-client/issues/532

1 Like