From 1ffbc70e76b04b1ce569c452c7d10afc9e94439e Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 21 May 2018 15:09:50 +0200 Subject: [PATCH 1/6] add text/plain default, fix #1515 --- src/backends/hdf5.jl | 4 ---- src/backends/inspectdr.jl | 1 - src/output.jl | 6 +++--- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/backends/hdf5.jl b/src/backends/hdf5.jl index 2cbe7a76..a82a277f 100644 --- a/src/backends/hdf5.jl +++ b/src/backends/hdf5.jl @@ -240,10 +240,6 @@ end # ---------------------------------------------------------------- -_show(io::IO, mime::MIME"text/plain", plt::Plot{HDF5Backend}) = nothing #Don't show - -# ---------------------------------------------------------------- - # Display/show the plot (open a GUI window, or browser page, for example). function _display(plt::Plot{HDF5Backend}) msg = "HDF5 interface does not support `display()` function." diff --git a/src/backends/inspectdr.jl b/src/backends/inspectdr.jl index d0d3e285..8cc2a22c 100644 --- a/src/backends/inspectdr.jl +++ b/src/backends/inspectdr.jl @@ -523,7 +523,6 @@ for (mime, fmt) in _inspectdr_mimeformats_nodpi _inspectdr_show(io, mime, _inspectdr_getmplot(plt.o), plt[:size]...) end end -_show(io::IO, mime::MIME"text/plain", plt::Plot{InspectDRBackend}) = nothing #Don't show # ---------------------------------------------------------------- diff --git a/src/output.jl b/src/output.jl index ca1461c8..6344ba9c 100644 --- a/src/output.jl +++ b/src/output.jl @@ -211,6 +211,9 @@ for mime in keys(_mimeformats) end end +# default text/plain for all backends +_show(io::IO, ::MIME{Symbol("text/plain")}, plt::Plot) = show(io, plt) + "Close all open gui windows of the current backend" closeall() = closeall(backend()) @@ -323,9 +326,6 @@ end if Juno.isactive() Media.media(Plot, Media.Plot) - - _show(io::IO, m::MIME"text/plain", plt::Plot{B}) where {B} = print(io, "Plot{$B}()") - function Juno.render(e::Juno.Editor, plt::Plot) Juno.render(e, nothing) end From 45b97b3ec5edcf8cbf9a9d8f0406fedb1363a39a Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Fri, 18 May 2018 11:05:32 +0200 Subject: [PATCH 2/6] forward showable call to _show instead of show since all backends return true otherwise --- src/backends/plotly.jl | 9 --------- src/output.jl | 7 ++++--- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 30d0c05c..64c9f21d 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -922,15 +922,6 @@ end # ---------------------------------------------------------------- -function _show(io::IO, ::MIME"image/png", 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)) diff --git a/src/output.jl b/src/output.jl index 6344ba9c..e4b7e657 100644 --- a/src/output.jl +++ b/src/output.jl @@ -195,10 +195,11 @@ function Base.show(io::IO, ::MIME"text/html", plt::Plot) 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)) +# delegate mimewritable (showable on julia 0.7) to _show instead +function Base.mimewritable(m::M, plt::P) where {M<:MIME, P<:Plot} + return method_exists(_show, Tuple{IO, M, P}) end + function _display(plt::Plot) warn("_display is not defined for this backend.") end From 26d0338f09c9b34f8378ae0f36a79cb529420c86 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Fri, 18 May 2018 11:06:12 +0200 Subject: [PATCH 3/6] remove bad show methods we can not change the output type and not use what was requested fix #1529 --- src/output.jl | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/src/output.jl b/src/output.jl index e4b7e657..b2232c64 100644 --- a/src/output.jl +++ b/src/output.jl @@ -176,25 +176,6 @@ const _best_html_output_type = KW( :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, "") - 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 - # delegate mimewritable (showable on julia 0.7) to _show instead function Base.mimewritable(m::M, plt::P) where {M<:MIME, P<:Plot} return method_exists(_show, Tuple{IO, M, P}) @@ -309,11 +290,6 @@ end out end - # default text/plain passes to html... handles Interact issues - function Base.show(io::IO, m::MIME"text/plain", plt::Plot) - show(io, MIME("text/html"), plt) - end - ENV["MPLBACKEND"] = "Agg" end end From 9f46e19084f66ec2a87a455c6e523d417d92c436 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 21 May 2018 09:13:31 +0200 Subject: [PATCH 4/6] remove Base.show methods for plotly and plotlyjs to unify all backend show to go through the same path --- src/backends/plotly.jl | 3 +-- src/backends/plotlyjs.jl | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 64c9f21d..474efb68 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -922,8 +922,7 @@ end # ---------------------------------------------------------------- -function Base.show(io::IO, ::MIME"text/html", plt::Plot{PlotlyBackend}) - prepare_output(plt) +function _show(io::IO, ::MIME"text/html", plt::Plot{PlotlyBackend}) write(io, html_head(plt) * html_body(plt)) end diff --git a/src/backends/plotlyjs.jl b/src/backends/plotlyjs.jl index 1bcc3845..10213214 100644 --- a/src/backends/plotlyjs.jl +++ b/src/backends/plotlyjs.jl @@ -1,5 +1,5 @@ @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 # https://github.com/spencerlyon2/PlotlyJS.jl @@ -88,8 +88,7 @@ end # ---------------------------------------------------------------- -function Base.show(io::IO, ::MIME"text/html", plt::Plot{PlotlyJSBackend}) - prepare_output(plt) +function _show(io::IO, ::MIME"text/html", plt::Plot{PlotlyJSBackend}) if isijulia() && !_use_remote[] write(io, PlotlyJS.html_body(PlotlyJS.JupyterPlot(plt.o))) else From cfe07784e63cf68655519d0ba2b3b432684ecc01 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 21 May 2018 15:15:17 +0200 Subject: [PATCH 5/6] make UnicodePlots.show print to the given io --- src/backends/unicodeplots.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backends/unicodeplots.jl b/src/backends/unicodeplots.jl index 6b4e124d..f644966d 100644 --- a/src/backends/unicodeplots.jl +++ b/src/backends/unicodeplots.jl @@ -212,7 +212,7 @@ end function _show(io::IO, ::MIME"text/plain", plt::Plot{UnicodePlotsBackend}) unicodeplots_rebuild(plt) - map(show, plt.o) + foreach(x -> show(io, x), plt.o) nothing end From 3e7e68e988458ff20159bacef4965cf3b4fc4373 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 21 May 2018 15:15:22 +0200 Subject: [PATCH 6/6] 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 --- src/output.jl | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/src/output.jl b/src/output.jl index b2232c64..3a88df8c 100644 --- a/src/output.jl +++ b/src/output.jl @@ -168,14 +168,6 @@ const _mimeformats = Dict( "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 function Base.mimewritable(m::M, plt::P) where {M<:MIME, P<:Plot} return method_exists(_show, Tuple{IO, M, P}) @@ -244,6 +236,14 @@ end # IJulia # --------------------------------------------------------- +const _best_IJulia_output_type = Dict( + PyPlotBackend => :png, + UnicodePlotsBackend => :txt, + GLVisualizeBackend => :png, + PlotlyJSBackend => :html, + PlotlyBackend => :html +) + @require IJulia begin if IJulia.inited @@ -268,26 +268,24 @@ end out 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) - 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 - 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 + out = invoke(IJulia.display_dict, Tuple{Any}, plt) _extra_mime_info!(plt, out) - out + return out end ENV["MPLBACKEND"] = "Agg"