Merge 3e7e68e988458ff20159bacef4965cf3b4fc4373 into afe4817150a9dc22a8c704a5255534532d2e0819

This commit is contained in:
Fredrik Ekre 2018-06-03 15:04:01 +00:00 committed by GitHub
commit 6978f88196
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 67 deletions

View File

@ -922,17 +922,7 @@ end
# ---------------------------------------------------------------- # ----------------------------------------------------------------
function _show(io::IO, ::MIME"image/png", plt::Plot{PlotlyBackend}) function _show(io::IO, ::MIME"text/html", plt::Plot{PlotlyBackend})
# show_png_from_html(io, plt)
error("png output from the plotly backend is not supported. Please use plotlyjs instead.")
end
function _show(io::IO, ::MIME"image/svg+xml", plt::Plot{PlotlyBackend})
error("svg output from the plotly backend is not supported. Please use plotlyjs instead.")
end
function Base.show(io::IO, ::MIME"text/html", plt::Plot{PlotlyBackend})
prepare_output(plt)
write(io, html_head(plt) * html_body(plt)) write(io, html_head(plt) * html_body(plt))
end end

View File

@ -1,5 +1,5 @@
@require Revise begin @require Revise begin
Revise.track(Plots, joinpath(Pkg.dir("Plots"), "src", "backends", "plotlyjs.jl")) Revise.track(Plots, joinpath(Pkg.dir("Plots"), "src", "backends", "plotlyjs.jl"))
end end
# https://github.com/spencerlyon2/PlotlyJS.jl # https://github.com/spencerlyon2/PlotlyJS.jl
@ -88,8 +88,7 @@ end
# ---------------------------------------------------------------- # ----------------------------------------------------------------
function Base.show(io::IO, ::MIME"text/html", plt::Plot{PlotlyJSBackend}) function _show(io::IO, ::MIME"text/html", plt::Plot{PlotlyJSBackend})
prepare_output(plt)
if isijulia() && !_use_remote[] if isijulia() && !_use_remote[]
write(io, PlotlyJS.html_body(PlotlyJS.JupyterPlot(plt.o))) write(io, PlotlyJS.html_body(PlotlyJS.JupyterPlot(plt.o)))
else else

View File

@ -212,7 +212,7 @@ end
function _show(io::IO, ::MIME"text/plain", plt::Plot{UnicodePlotsBackend}) function _show(io::IO, ::MIME"text/plain", plt::Plot{UnicodePlotsBackend})
unicodeplots_rebuild(plt) unicodeplots_rebuild(plt)
map(show, plt.o) foreach(x -> show(io, x), plt.o)
nothing nothing
end end

View File

@ -168,37 +168,11 @@ const _mimeformats = Dict(
"application/x-tex" => "tex", "application/x-tex" => "tex",
) )
const _best_html_output_type = KW( # delegate mimewritable (showable on julia 0.7) to _show instead
:pyplot => :png, function Base.mimewritable(m::M, plt::P) where {M<:MIME, P<:Plot}
:unicodeplots => :txt, return method_exists(_show, Tuple{IO, M, P})
:glvisualize => :png,
:plotlyjs => :html,
:plotly => :html
)
# a backup for html... passes to svg or png depending on the html_output_format arg
function Base.show(io::IO, ::MIME"text/html", plt::Plot)
output_type = Symbol(plt.attr[:html_output_format])
if output_type == :auto
output_type = get(_best_html_output_type, backend_name(plt.backend), :svg)
end
if output_type == :png
# info("writing png to html output")
print(io, "<img src=\"data:image/png;base64,", base64encode(show, MIME("image/png"), plt), "\" />")
elseif output_type == :svg
# info("writing svg to html output")
show(io, MIME("image/svg+xml"), plt)
elseif output_type == :txt
show(io, MIME("text/plain"), plt)
else
error("only png or svg allowed. got: $output_type")
end
end end
function _show(io::IO, m, plt::Plot{B}) where B
# Base.show_backtrace(STDOUT, backtrace())
warn("_show is not defined for this backend. m=", string(m))
end
function _display(plt::Plot) function _display(plt::Plot)
warn("_display is not defined for this backend.") warn("_display is not defined for this backend.")
end end
@ -262,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
@ -286,31 +268,24 @@ end
out out
end end
function IJulia.display_dict(plt::Plot) # use mimewritable to "trick" IJulia to choose the right output type
output_type = Symbol(plt.attr[:html_output_format]) # Union needed to avoid method overwrite from the non-IJulia definition of mimewritable
if output_type == :auto AllBackends = Union{PyPlotBackend, UnicodePlotsBackend, PlotlyBackend, PlotlyJSBackend,
output_type = get(_best_html_output_type, backend_name(plt.backend), :svg) GRBackend, GLVisualizeBackend, PGFPlotsBackend, InspectDRBackend, HDF5Backend}
end function Base.mimewritable(mime::M, plt::Plot{P}) where {M<:MIME, P<:AllBackends}
out = Dict() sym = Symbol(plt.attr[:html_output_format])
if output_type == :png sym = sym === (:auto) ? get(_best_IJulia_output_type, P, :svg) : sym
mime = "image/png" sym === :txt && M === MIME"text/plain" && return invoke(mimewritable, Tuple{MIME,Plot}, mime, plt)
out[mime] = base64encode(show, MIME(mime), plt) sym === :png && M === MIME"image/png" && return invoke(mimewritable, Tuple{MIME,Plot}, mime, plt)
elseif output_type == :svg sym === :svg && M === MIME"image/svg+xml" && return invoke(mimewritable, Tuple{MIME,Plot}, mime, plt)
mime = "image/svg+xml" sym === :html && M === MIME"text/html" && return invoke(mimewritable, Tuple{MIME,Plot}, mime, plt)
out[mime] = sprint(show, MIME(mime), plt) return false
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)
out
end end
# default text/plain passes to html... handles Interact issues function IJulia.display_dict(plt::Plot)
function Base.show(io::IO, m::MIME"text/plain", plt::Plot) out = invoke(IJulia.display_dict, Tuple{Any}, plt)
show(io, MIME("text/html"), plt) _extra_mime_info!(plt, out)
return out
end end
ENV["MPLBACKEND"] = "Agg" ENV["MPLBACKEND"] = "Agg"