From 2d280edf0c878f3f52a13d113eccabdc1fa0680a Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 4 Jun 2018 10:13:53 +0200 Subject: [PATCH 1/4] 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 ccb5194bb5ecb1a5623824f3d99938824c1a5be1 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 4 Jun 2018 10:18:59 +0200 Subject: [PATCH 2/4] make fallback method a method of _show instead of show fix #1529 --- src/backends/plotly.jl | 3 +-- src/backends/plotlyjs.jl | 5 ++--- src/output.jl | 9 ++------- 3 files changed, 5 insertions(+), 12 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 diff --git a/src/output.jl b/src/output.jl index e4b7e657..55d677e3 100644 --- a/src/output.jl +++ b/src/output.jl @@ -177,7 +177,7 @@ const _best_html_output_type = KW( ) # 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) +function _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) @@ -191,7 +191,7 @@ function Base.show(io::IO, ::MIME"text/html", plt::Plot) elseif output_type == :txt show(io, MIME("text/plain"), plt) else - error("only png or svg allowed. got: $output_type") + error("only png or svg allowed. got: $(repr(output_type))") end end @@ -309,11 +309,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 69a7a8a04e0bdf488c465b8a4930907f7a08e4d8 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 4 Jun 2018 10:39:07 +0200 Subject: [PATCH 3/4] define show for more mime-types --- src/output.jl | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/output.jl b/src/output.jl index 55d677e3..a839464c 100644 --- a/src/output.jl +++ b/src/output.jl @@ -157,17 +157,6 @@ end # --------------------------------------------------------- -const _mimeformats = Dict( - "application/eps" => "eps", - "image/eps" => "eps", - "application/pdf" => "pdf", - "image/png" => "png", - "application/postscript" => "ps", - "image/svg+xml" => "svg", - "text/plain" => "txt", - "application/x-tex" => "tex", -) - const _best_html_output_type = KW( :pyplot => :png, :unicodeplots => :txt, @@ -205,8 +194,10 @@ function _display(plt::Plot) end # for writing to io streams... first prepare, then callback -for mime in keys(_mimeformats) - @eval function Base.show(io::IO, m::MIME{Symbol($mime)}, plt::Plot{B}) where B +for mime in ("text/plain", "text/html", "image/png", "image/eps", "image/svg+xml", + "application/eps", "application/pdf", "application/postscript", + "application/x-tex") + @eval function Base.show(io::IO, m::MIME{Symbol($mime)}, plt::Plot) prepare_output(plt) _show(io, m, plt) end From 32ec9e82bf5273bde184b7f7ca0c2a05330f2dbe Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 4 Jun 2018 12:34:14 +0200 Subject: [PATCH 4/4] make UnicodePlots print to the given io and add support for text/plain in Plots own display_dict fix #1514 --- src/backends/unicodeplots.jl | 2 +- src/output.jl | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) 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 diff --git a/src/output.jl b/src/output.jl index a839464c..cebfe920 100644 --- a/src/output.jl +++ b/src/output.jl @@ -284,7 +284,10 @@ end output_type = get(_best_html_output_type, backend_name(plt.backend), :svg) end out = Dict() - if output_type == :png + if output_type == :txt + mime = "text/plain" + out[mime] = sprint(show, MIME(mime), plt) + elseif output_type == :png mime = "image/png" out[mime] = base64encode(show, MIME(mime), plt) elseif output_type == :svg