Skip to content
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

show(::text/plain) is called even when ::text/html is defined #574

Closed
cstjean opened this issue Jul 20, 2017 · 7 comments
Closed

show(::text/plain) is called even when ::text/html is defined #574

cstjean opened this issue Jul 20, 2017 · 7 comments

Comments

@cstjean
Copy link
Contributor

cstjean commented Jul 20, 2017

struct Blag
end

Base.show(io::IO, ::MIME"text/html", ::Blag) = write(io, "html")
Base.show(io::IO, ::MIME"text/plain", ::Blag) = (warn("plain called"); write(io, "plain"))

Blag()

shows:

html
WARNING: plain called

The output is correct, but why is plain called at all?

It seems to be this line

@stevengj
Copy link
Member

This is how Jupyter works. The kernel sends a dictionary containing multiple representations of the object, called a "mime-bundle", and the front-end selects which one to display. You can even have multiple front-ends connected simultaneously to the same kernel — one front-end might be a notebook that displays HTML, and another might be a console that displays only text.

@KristofferC
Copy link
Sponsor Member

I believe this is a big performance trap. Typically you define your show methods for png and svg etc independently even though a lot of the rendering pipeline could be reused. In my case, I am calling LaTeX to generate the figure which is now called twice for every figure that is displayed. Is there any way to return the whole "mime-bundle" in one call?

@stevengj
Copy link
Member

@KristofferC, you could cache the re-used information in a global or something, though I'm not sure what parts of LaTeX are re-used between PNG and SVG output?

You could return the whole MIME bundle in one call by overloading IJulia.display_dict(x), though that would add an IJulia dependency.

(The basic problem here is that some backends can display either SVG or PNG, while some can only display PNG, so it is not great to send SVG without sending PNG too.)

@KristofferC
Copy link
Sponsor Member

KristofferC commented Mar 16, 2018

you could cache the re-used information in a global or something, though I'm not sure what parts of LaTeX are re-used between PNG and SVG output?

Everything from LaTeX is reused since both PNG and SVG are just conversions from the generated PDF. And generating the PDF is orders of magnitude more costly than converting it. Right now, I am using globals and some hashing of the latex code to try to detect when IJulia asks for the same plot twice in different formats.

This seems like a bad way of doing stuff... FIgures in formats that are never shown are requested and the figure generator does not get any context that it is actually the same figure that should be rendered. My guess is that almost every package showing things in IJulia are double rendering things right now.

@stevengj
Copy link
Member

stevengj commented Mar 16, 2018

How do you propose to solve this, since there is no way in the Jupyter protocol for the kernel (IJulia) to know which formats can be displayed?

In PyPlot, I don't emit SVG by default, since SVG is slow to display anyway for complicated plots.

@KristofferC
Copy link
Sponsor Member

How do you propose to solve this, since there is no way in the Jupyter protocol for the kernel (IJulia) to know which formats can be displayed?

I'm not saying IJulia is doing anything wrong, it just seems like a bad situation in general.

In PyPlot, I don't emit SVG by default, since SVG is slow to display anyway for complicated plots.

I expect most people that use PGFPlotsX want to have vector graphics by default since PGFPlots is slow anyway for complicated plots and I believe is often used because of the high-quality plots it produces.

@cstjean
Copy link
Contributor Author

cstjean commented Jan 30, 2019

Workaround for users: use display(MIME"text/html"(), Blag())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants