Merge pull request #916 from pfitzseb/sp/juno

WIP: Better Juno integration
This commit is contained in:
Daniel Schwabeneder 2017-07-17 20:40:48 +02:00 committed by GitHub
commit fc427e618a
3 changed files with 41 additions and 47 deletions

View File

@ -11,3 +11,4 @@ Showoff
StatsBase 0.14.0 StatsBase 0.14.0
JSON JSON
NaNMath NaNMath
Requires

View File

@ -241,10 +241,8 @@ end
const CURRENT_BACKEND = CurrentBackend(:none) const CURRENT_BACKEND = CurrentBackend(:none)
function __init__() # for compatibility with Requires.jl:
setup_ijulia() @init begin
setup_atom()
if isdefined(Main, :PLOTS_DEFAULTS) if isdefined(Main, :PLOTS_DEFAULTS)
for (k,v) in Main.PLOTS_DEFAULTS for (k,v) in Main.PLOTS_DEFAULTS
default(k, v) default(k, v)

View File

@ -224,10 +224,6 @@ if is_installed("FileIO")
end end
end end
# function html_output_format(fmt) # function html_output_format(fmt)
# if fmt == "png" # if fmt == "png"
# @eval function Base.show(io::IO, ::MIME"text/html", plt::Plot) # @eval function Base.show(io::IO, ::MIME"text/html", plt::Plot)
@ -250,76 +246,75 @@ end
const _ijulia_output = String["text/html"] const _ijulia_output = String["text/html"]
function setup_ijulia() using Requires
# override IJulia inline display @require IJulia begin
if isijulia() if IJulia.inited
@eval begin export set_ijulia_output
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
# default text/plain passes to html... handles Interact issues function set_ijulia_output(mimestr::AbstractString)
function Base.show(io::IO, m::MIME"text/plain", plt::Plot) # info("Setting IJulia output format to $mimestr")
show(io, MIME("text/html"), plt) global _ijulia_output
end _ijulia_output[1] = mimestr
end 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
end end
# --------------------------------------------------------- # ---------------------------------------------------------
# Atom PlotPane # Atom PlotPane
# --------------------------------------------------------- # ---------------------------------------------------------
@require Juno begin
import Hiccup, Media
function setup_atom() if Juno.isactive()
if isatom()
@eval import Atom, Media
Media.media(Plot, Media.Plot) 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 Media.render(e::Atom.Editor, plt::Plot) _show{B}(io::IO, m::MIME"text/plain", plt::Plot{B}) = print(io, "Plot{$B}()")
Media.render(e, nothing)
function Juno.render(e::Juno.Editor, plt::Plot)
Juno.render(e, nothing)
end end
if get(ENV, "PLOTS_USE_ATOM_PLOTPANE", true) in (true, 1, "1", "true", "yes") 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)
function Media.render(pane::Atom.PlotPane, plt::Plot)
# temporarily overwrite size to be Atom.plotsize # temporarily overwrite size to be Atom.plotsize
sz = plt[:size] sz = plt[:size]
plt[:size] = Juno.plotsize() jsize = Juno.plotsize()
Media.render(pane, Atom.div(".fill", Atom.HTML(stringmime(MIME("text/html"), plt)))) jsize[1] == 0 && (jsize[1] = 400)
jsize[2] == 0 && (jsize[2] = 500)
plt[:size] = jsize
Juno.render(pane, HTML(stringmime(MIME("text/html"), plt)))
plt[:size] = sz plt[:size] = sz
end end
# special handling for PlotlyJS # special handling for PlotlyJS
function Media.render(pane::Atom.PlotPane, plt::Plot{PlotlyJSBackend}) function Juno.render(pane::Juno.PlotPane, plt::Plot{PlotlyJSBackend})
display(Plots.PlotsDisplay(), plt) display(Plots.PlotsDisplay(), plt)
end end
else else
# function Juno.render(pane::Juno.PlotPane, plt::Plot)
function Media.render(pane::Atom.PlotPane, plt::Plot)
display(Plots.PlotsDisplay(), plt) display(Plots.PlotsDisplay(), plt)
s = "PlotPane turned off. Unset ENV[\"PLOTS_USE_ATOM_PLOTPANE\"] and restart Julia to enable it." s = "PlotPane turned off. Unset ENV[\"PLOTS_USE_ATOM_PLOTPANE\"] and restart Julia to enable it."
Media.render(pane, Atom.div(Atom.HTML(s))) Juno.render(pane, HTML(s))
end end
end end
# special handling for plotly... use PlotsDisplay # special handling for plotly... use PlotsDisplay
function Media.render(pane::Atom.PlotPane, plt::Plot{PlotlyBackend}) function Juno.render(pane::Juno.PlotPane, plt::Plot{PlotlyBackend})
display(Plots.PlotsDisplay(), plt) 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." 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))) Juno.render(pane, HTML(s))
end end
end end
end end