use IJulia's implementation of display_dict

and use Base.mimewritable (Base.showable) to trick IJulia
to only generate one plot and display the correct output
fix #1514
This commit is contained in:
Fredrik Ekre 2018-05-21 15:15:22 +02:00
parent cfe07784e6
commit 3e7e68e988

View File

@ -168,14 +168,6 @@ const _mimeformats = Dict(
"application/x-tex" => "tex", "application/x-tex" => "tex",
) )
const _best_html_output_type = KW(
:pyplot => :png,
:unicodeplots => :txt,
:glvisualize => :png,
:plotlyjs => :html,
:plotly => :html
)
# delegate mimewritable (showable on julia 0.7) to _show instead # delegate mimewritable (showable on julia 0.7) to _show instead
function Base.mimewritable(m::M, plt::P) where {M<:MIME, P<:Plot} function Base.mimewritable(m::M, plt::P) where {M<:MIME, P<:Plot}
return method_exists(_show, Tuple{IO, M, P}) return method_exists(_show, Tuple{IO, M, P})
@ -244,6 +236,14 @@ end
# IJulia # IJulia
# --------------------------------------------------------- # ---------------------------------------------------------
const _best_IJulia_output_type = Dict(
PyPlotBackend => :png,
UnicodePlotsBackend => :txt,
GLVisualizeBackend => :png,
PlotlyJSBackend => :html,
PlotlyBackend => :html
)
@require IJulia begin @require IJulia begin
if IJulia.inited if IJulia.inited
@ -268,26 +268,24 @@ end
out out
end end
# use mimewritable to "trick" IJulia to choose the right output type
# Union needed to avoid method overwrite from the non-IJulia definition of mimewritable
AllBackends = Union{PyPlotBackend, UnicodePlotsBackend, PlotlyBackend, PlotlyJSBackend,
GRBackend, GLVisualizeBackend, PGFPlotsBackend, InspectDRBackend, HDF5Backend}
function Base.mimewritable(mime::M, plt::Plot{P}) where {M<:MIME, P<:AllBackends}
sym = Symbol(plt.attr[:html_output_format])
sym = sym === (:auto) ? get(_best_IJulia_output_type, P, :svg) : sym
sym === :txt && M === MIME"text/plain" && return invoke(mimewritable, Tuple{MIME,Plot}, mime, plt)
sym === :png && M === MIME"image/png" && return invoke(mimewritable, Tuple{MIME,Plot}, mime, plt)
sym === :svg && M === MIME"image/svg+xml" && return invoke(mimewritable, Tuple{MIME,Plot}, mime, plt)
sym === :html && M === MIME"text/html" && return invoke(mimewritable, Tuple{MIME,Plot}, mime, plt)
return false
end
function IJulia.display_dict(plt::Plot) function IJulia.display_dict(plt::Plot)
output_type = Symbol(plt.attr[:html_output_format]) out = invoke(IJulia.display_dict, Tuple{Any}, plt)
if output_type == :auto
output_type = get(_best_html_output_type, backend_name(plt.backend), :svg)
end
out = Dict()
if output_type == :png
mime = "image/png"
out[mime] = base64encode(show, MIME(mime), plt)
elseif output_type == :svg
mime = "image/svg+xml"
out[mime] = sprint(show, MIME(mime), plt)
elseif output_type == :html
mime = "text/html"
out[mime] = sprint(show, MIME(mime), plt)
else
error("Unsupported output type $output_type")
end
_extra_mime_info!(plt, out) _extra_mime_info!(plt, out)
out return out
end end
ENV["MPLBACKEND"] = "Agg" ENV["MPLBACKEND"] = "Agg"