From d671ccd6ba7089ff65d44226b1f714d0cb5d7c6f Mon Sep 17 00:00:00 2001 From: Sebastian Pfitzner Date: Thu, 8 Jun 2017 15:23:39 +0200 Subject: [PATCH 1/7] better juno integration --- REQUIRE | 1 + src/Plots.jl | 4 ++- src/output.jl | 73 ++++++++++++++++++++++++--------------------------- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/REQUIRE b/REQUIRE index 44e1623d..30e92d35 100644 --- a/REQUIRE +++ b/REQUIRE @@ -11,3 +11,4 @@ Showoff StatsBase 0.14.0 JSON NaNMath +Requires diff --git a/src/Plots.jl b/src/Plots.jl index a23d6a8d..9e2fa598 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -242,8 +242,10 @@ end const CURRENT_BACKEND = CurrentBackend(:none) function __init__() + # for compatibility with Requires.jl: + @init + setup_ijulia() - setup_atom() if isdefined(Main, :PLOTS_DEFAULTS) for (k,v) in Main.PLOTS_DEFAULTS diff --git a/src/output.jl b/src/output.jl index 814f8884..bd444ba0 100644 --- a/src/output.jl +++ b/src/output.jl @@ -278,48 +278,45 @@ end # --------------------------------------------------------- # Atom PlotPane # --------------------------------------------------------- +using Requires +@require Juno begin + import Hiccup, Media + Media.media(Plot, Media.Plot) -function setup_atom() - if isatom() - @eval import Atom, Media - Media.media(Plot, Media.Plot) + # default text/plain so it doesn't complain + function Base.show{B}(io::IO, ::MIME"text/plain", plt::Plot{B}) + print(io, "Plot{$B}()") + end - # default text/plain so it doesn't complain - function Base.show{B}(io::IO, ::MIME"text/plain", plt::Plot{B}) - print(io, "Plot{$B}()") + function Juno.render(e::Juno.Editor, plt::Plot) + Juno.render(e, nothing) + end + + if get(ENV, "PLOTS_USE_ATOM_PLOTPANE", true) in (true, 1, "1", "true", "yes") + # this is like "display"... sends an html div with the plot to the PlotPane + function Juno.render(pane::Juno.PlotPane, plt::Plot) + # temporarily overwrite size to be Atom.plotsize + sz = plt[:size] + plt[:size] = Juno.plotsize() + Juno.render(pane, Hiccup.div(".fill", HTML(stringmime(MIME("text/html"), plt)))) + plt[:size] = sz end - - function Media.render(e::Atom.Editor, plt::Plot) - Media.render(e, nothing) - end - - if get(ENV, "PLOTS_USE_ATOM_PLOTPANE", true) in (true, 1, "1", "true", "yes") - # this is like "display"... sends an html div with the plot to the PlotPane - function Media.render(pane::Atom.PlotPane, plt::Plot) - # temporarily overwrite size to be Atom.plotsize - sz = plt[:size] - plt[:size] = Juno.plotsize() - Media.render(pane, Atom.div(".fill", Atom.HTML(stringmime(MIME("text/html"), plt)))) - plt[:size] = sz - end - # special handling for PlotlyJS - function Media.render(pane::Atom.PlotPane, plt::Plot{PlotlyJSBackend}) - display(Plots.PlotsDisplay(), plt) - end - else - # - function Media.render(pane::Atom.PlotPane, plt::Plot) - display(Plots.PlotsDisplay(), plt) - s = "PlotPane turned off. Unset ENV[\"PLOTS_USE_ATOM_PLOTPANE\"] and restart Julia to enable it." - Media.render(pane, Atom.div(Atom.HTML(s))) - end - end - - # special handling for plotly... use PlotsDisplay - function Media.render(pane::Atom.PlotPane, plt::Plot{PlotlyBackend}) + # special handling for PlotlyJS + function Juno.render(pane::Juno.PlotPane, plt::Plot{PlotlyJSBackend}) display(Plots.PlotsDisplay(), plt) - s = "PlotPane turned off. The plotly backend cannot render in the PlotPane due to javascript issues. Plotlyjs is similar to plotly and is compatible with the plot pane." - Media.render(pane, Atom.div(Atom.HTML(s))) + end + else + function Juno.render(pane::Juno.PlotPane, plt::Plot) + display(Plots.PlotsDisplay(), plt) + s = "PlotPane turned off. Unset ENV[\"PLOTS_USE_ATOM_PLOTPANE\"] and restart Julia to enable it." + Juno.render(pane, Hiccup.div(HTML(s))) end end + + # special handling for plotly... use PlotsDisplay + function Juno.render(pane::Juno.PlotPane, plt::Plot{PlotlyBackend}) + display(Plots.PlotsDisplay(), plt) + s = "PlotPane turned off. The plotly backend cannot render in the PlotPane due to javascript issues. Plotlyjs is similar to plotly and is compatible with the plot pane." + Juno.render(pane, Hiccup.div(HTML(s))) + end end From 5e5f23b5e5776266566bb95955ed00643dae3b3c Mon Sep 17 00:00:00 2001 From: Sebastian Pfitzner Date: Fri, 16 Jun 2017 15:26:30 +0200 Subject: [PATCH 2/7] fix world age error --- src/backends.jl | 6 +++--- src/output.jl | 16 +++++++--------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/backends.jl b/src/backends.jl index 19a9c6ea..9248d05b 100644 --- a/src/backends.jl +++ b/src/backends.jl @@ -135,7 +135,7 @@ function pickDefaultBackend() Pkg.installed(env_default) # this will error if not installed sym = Symbol(lowercase(env_default)) if haskey(_backendType, sym) - return backend(sym) + return @eval backend($sym) else warn("You have set PLOTS_DEFAULT_BACKEND=$env_default but it is not a valid backend package. Choose from:\n\t", join(sort(_backends), "\n\t")) @@ -150,12 +150,12 @@ function pickDefaultBackend() # features, speed, and robustness for pkgstr in ("GR", "PyPlot", "PlotlyJS", "PGFPlots", "UnicodePlots", "InspectDR", "GLVisualize") if Pkg.installed(pkgstr) != nothing - return backend(Symbol(lowercase(pkgstr))) + return @eval backend(Symbol(lowercase($pkgstr))) end end # the default if nothing else is installed - backend(:plotly) + @eval backend(:plotly) end diff --git a/src/output.jl b/src/output.jl index bd444ba0..a838a542 100644 --- a/src/output.jl +++ b/src/output.jl @@ -283,22 +283,20 @@ using Requires import Hiccup, Media Media.media(Plot, Media.Plot) - # default text/plain so it doesn't complain - function Base.show{B}(io::IO, ::MIME"text/plain", plt::Plot{B}) - print(io, "Plot{$B}()") - end - function Juno.render(e::Juno.Editor, plt::Plot) Juno.render(e, nothing) end if get(ENV, "PLOTS_USE_ATOM_PLOTPANE", true) in (true, 1, "1", "true", "yes") - # this is like "display"... sends an html div with the plot to the PlotPane function Juno.render(pane::Juno.PlotPane, plt::Plot) # temporarily overwrite size to be Atom.plotsize sz = plt[:size] + jsize = Juno.plotsize() + jsize[1] == 0 && (jsize[1] = 400) + jsize[2] == 0 && (jsize[2] = 500) + plt[:size] = Juno.plotsize() - Juno.render(pane, Hiccup.div(".fill", HTML(stringmime(MIME("text/html"), plt)))) + Juno.render(pane, HTML(stringmime(MIME("text/html"), plt))) plt[:size] = sz end # special handling for PlotlyJS @@ -309,7 +307,7 @@ using Requires function Juno.render(pane::Juno.PlotPane, plt::Plot) display(Plots.PlotsDisplay(), plt) s = "PlotPane turned off. Unset ENV[\"PLOTS_USE_ATOM_PLOTPANE\"] and restart Julia to enable it." - Juno.render(pane, Hiccup.div(HTML(s))) + Juno.render(pane, HTML(s)) end end @@ -317,6 +315,6 @@ using Requires function Juno.render(pane::Juno.PlotPane, plt::Plot{PlotlyBackend}) display(Plots.PlotsDisplay(), plt) s = "PlotPane turned off. The plotly backend cannot render in the PlotPane due to javascript issues. Plotlyjs is similar to plotly and is compatible with the plot pane." - Juno.render(pane, Hiccup.div(HTML(s))) + Juno.render(pane, HTML(s)) end end From c385035ac58fe5bb323f9c0a2f2e8a2686f8c6d4 Mon Sep 17 00:00:00 2001 From: Sebastian Pfitzner Date: Wed, 12 Jul 2017 10:38:15 +0200 Subject: [PATCH 3/7] handle ijulia with requires and properly check if juno/ijulia are actually used and not just loaded --- src/Plots.jl | 2 -- src/output.jl | 99 ++++++++++++++++++++++++++------------------------- 2 files changed, 50 insertions(+), 51 deletions(-) diff --git a/src/Plots.jl b/src/Plots.jl index 9e2fa598..d31d0258 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -245,8 +245,6 @@ function __init__() # for compatibility with Requires.jl: @init - setup_ijulia() - if isdefined(Main, :PLOTS_DEFAULTS) for (k,v) in Main.PLOTS_DEFAULTS default(k, v) diff --git a/src/output.jl b/src/output.jl index a838a542..fe46d992 100644 --- a/src/output.jl +++ b/src/output.jl @@ -250,71 +250,72 @@ end const _ijulia_output = String["text/html"] -function setup_ijulia() - # override IJulia inline display - if isijulia() - @eval begin - import IJulia - export set_ijulia_output - function set_ijulia_output(mimestr::AbstractString) - # info("Setting IJulia output format to $mimestr") - global _ijulia_output - _ijulia_output[1] = mimestr - end - function IJulia.display_dict(plt::Plot) - global _ijulia_output - Dict{String, String}(_ijulia_output[1] => sprint(show, _ijulia_output[1], plt)) - end +using Requires +@require IJulia begin + if IJulia.inited + export set_ijulia_output - # 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 + function set_ijulia_output(mimestr::AbstractString) + # info("Setting IJulia output format to $mimestr") + global _ijulia_output + _ijulia_output[1] = mimestr end - @eval set_ijulia_output("text/html") + function IJulia.display_dict(plt::Plot) + global _ijulia_output + Dict{String, String}(_ijulia_output[1] => sprint(show, _ijulia_output[1], plt)) + 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 + + set_ijulia_output("text/html") end end # --------------------------------------------------------- # Atom PlotPane # --------------------------------------------------------- -using Requires @require Juno begin import Hiccup, Media - Media.media(Plot, Media.Plot) - function Juno.render(e::Juno.Editor, plt::Plot) - Juno.render(e, nothing) - end + if Juno.isactive() + Media.media(Plot, Media.Plot) - if get(ENV, "PLOTS_USE_ATOM_PLOTPANE", true) in (true, 1, "1", "true", "yes") - function Juno.render(pane::Juno.PlotPane, plt::Plot) - # temporarily overwrite size to be Atom.plotsize - sz = plt[:size] - jsize = Juno.plotsize() - jsize[1] == 0 && (jsize[1] = 400) - jsize[2] == 0 && (jsize[2] = 500) - - plt[:size] = Juno.plotsize() - Juno.render(pane, HTML(stringmime(MIME("text/html"), plt))) - plt[:size] = sz + function Juno.render(e::Juno.Editor, plt::Plot) + Juno.render(e, nothing) end - # special handling for PlotlyJS - function Juno.render(pane::Juno.PlotPane, plt::Plot{PlotlyJSBackend}) - display(Plots.PlotsDisplay(), plt) + + if get(ENV, "PLOTS_USE_ATOM_PLOTPANE", true) in (true, 1, "1", "true", "yes") + function Juno.render(pane::Juno.PlotPane, plt::Plot) + # temporarily overwrite size to be Atom.plotsize + sz = plt[:size] + jsize = Juno.plotsize() + jsize[1] == 0 && (jsize[1] = 400) + jsize[2] == 0 && (jsize[2] = 500) + + plt[:size] = Juno.plotsize() + Juno.render(pane, HTML(stringmime(MIME("text/html"), plt))) + plt[:size] = sz + end + # special handling for PlotlyJS + function Juno.render(pane::Juno.PlotPane, plt::Plot{PlotlyJSBackend}) + display(Plots.PlotsDisplay(), plt) + end + else + function Juno.render(pane::Juno.PlotPane, plt::Plot) + display(Plots.PlotsDisplay(), plt) + s = "PlotPane turned off. Unset ENV[\"PLOTS_USE_ATOM_PLOTPANE\"] and restart Julia to enable it." + Juno.render(pane, HTML(s)) + end end - else - function Juno.render(pane::Juno.PlotPane, plt::Plot) + + # special handling for plotly... use PlotsDisplay + function Juno.render(pane::Juno.PlotPane, plt::Plot{PlotlyBackend}) display(Plots.PlotsDisplay(), plt) - s = "PlotPane turned off. Unset ENV[\"PLOTS_USE_ATOM_PLOTPANE\"] and restart Julia to enable it." + s = "PlotPane turned off. The plotly backend cannot render in the PlotPane due to javascript issues. Plotlyjs is similar to plotly and is compatible with the plot pane." Juno.render(pane, HTML(s)) end end - - # special handling for plotly... use PlotsDisplay - function Juno.render(pane::Juno.PlotPane, plt::Plot{PlotlyBackend}) - display(Plots.PlotsDisplay(), plt) - s = "PlotPane turned off. The plotly backend cannot render in the PlotPane due to javascript issues. Plotlyjs is similar to plotly and is compatible with the plot pane." - Juno.render(pane, HTML(s)) - end end From 9cf246a03e51269c11fe1f16dc2516016447cb3b Mon Sep 17 00:00:00 2001 From: Sebastian Pfitzner Date: Wed, 12 Jul 2017 10:40:26 +0200 Subject: [PATCH 4/7] fix juno plot sizing fallback --- src/output.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/output.jl b/src/output.jl index fe46d992..bdf60d9e 100644 --- a/src/output.jl +++ b/src/output.jl @@ -295,7 +295,7 @@ end jsize[1] == 0 && (jsize[1] = 400) jsize[2] == 0 && (jsize[2] = 500) - plt[:size] = Juno.plotsize() + plt[:size] = jsize Juno.render(pane, HTML(stringmime(MIME("text/html"), plt))) plt[:size] = sz end From bc7f6a6fac35f65319a106799f8b2ccdd702db9f Mon Sep 17 00:00:00 2001 From: Sebastian Pfitzner Date: Sat, 15 Jul 2017 11:40:11 +0200 Subject: [PATCH 5/7] circumvent warnings --- src/Plots.jl | 6 ++---- src/output.jl | 3 +++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Plots.jl b/src/Plots.jl index d31d0258..10e7bf6e 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -241,10 +241,8 @@ end const CURRENT_BACKEND = CurrentBackend(:none) -function __init__() - # for compatibility with Requires.jl: - @init - +# for compatibility with Requires.jl: +@init begin if isdefined(Main, :PLOTS_DEFAULTS) for (k,v) in Main.PLOTS_DEFAULTS default(k, v) diff --git a/src/output.jl b/src/output.jl index bdf60d9e..cdb6777b 100644 --- a/src/output.jl +++ b/src/output.jl @@ -283,6 +283,9 @@ end if Juno.isactive() Media.media(Plot, Media.Plot) + + _show{B}(io::IO, m::MIME"text/plain", plt::Plot{B}) = show(io, m, plt) + function Juno.render(e::Juno.Editor, plt::Plot) Juno.render(e, nothing) end From 95c60b9554effa3c5b13ec90080276a68390f121 Mon Sep 17 00:00:00 2001 From: Sebastian Pfitzner Date: Sat, 15 Jul 2017 12:01:00 +0200 Subject: [PATCH 6/7] unrecursify this definition --- src/output.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/output.jl b/src/output.jl index cdb6777b..06a049e5 100644 --- a/src/output.jl +++ b/src/output.jl @@ -284,7 +284,7 @@ end Media.media(Plot, Media.Plot) - _show{B}(io::IO, m::MIME"text/plain", plt::Plot{B}) = show(io, m, plt) + _show{B}(io::IO, m::MIME"text/plain", plt::Plot{B}) = print(io, "Plot{$B}()") function Juno.render(e::Juno.Editor, plt::Plot) Juno.render(e, nothing) From 6d6619064492ab776d46ebae8d0cc13fac76231a Mon Sep 17 00:00:00 2001 From: Sebastian Pfitzner Date: Mon, 17 Jul 2017 15:16:20 +0200 Subject: [PATCH 7/7] no eval shenanigans --- src/backends.jl | 6 +++--- src/output.jl | 4 ---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/backends.jl b/src/backends.jl index 9248d05b..19a9c6ea 100644 --- a/src/backends.jl +++ b/src/backends.jl @@ -135,7 +135,7 @@ function pickDefaultBackend() Pkg.installed(env_default) # this will error if not installed sym = Symbol(lowercase(env_default)) if haskey(_backendType, sym) - return @eval backend($sym) + return backend(sym) else warn("You have set PLOTS_DEFAULT_BACKEND=$env_default but it is not a valid backend package. Choose from:\n\t", join(sort(_backends), "\n\t")) @@ -150,12 +150,12 @@ function pickDefaultBackend() # features, speed, and robustness for pkgstr in ("GR", "PyPlot", "PlotlyJS", "PGFPlots", "UnicodePlots", "InspectDR", "GLVisualize") if Pkg.installed(pkgstr) != nothing - return @eval backend(Symbol(lowercase($pkgstr))) + return backend(Symbol(lowercase(pkgstr))) end end # the default if nothing else is installed - @eval backend(:plotly) + backend(:plotly) end diff --git a/src/output.jl b/src/output.jl index 06a049e5..5c036fe7 100644 --- a/src/output.jl +++ b/src/output.jl @@ -224,10 +224,6 @@ if is_installed("FileIO") end end - - - - # function html_output_format(fmt) # if fmt == "png" # @eval function Base.show(io::IO, ::MIME"text/html", plt::Plot)