diff --git a/src/backends/plotly-latest.min.js b/deps/plotly-latest.min.js similarity index 100% rename from src/backends/plotly-latest.min.js rename to deps/plotly-latest.min.js diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 21bc6cd8..544b7284 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -71,52 +71,79 @@ function Base.writemime(io::IO, ::MIME"image/png", plt::PlottingObject{PlotlyPac # TODO: write a png to io end -function open_browser_window(filename::AbstractString) - @osx_only return run(`open $(filename)`) - @linux_only return run(`xdg-open $(filename)`) - @windows_only return run(`$(ENV["COMSPEC"]) /c start $(filename)`) - warn("Unknown OS... cannot open browser window.") + +# function build_plotly_json() +# end + +const _pyplot_head_template = mt""" + +""" + +const _pyplot_body_template = mt""" +
+ +""" + + +function html_head(plt::Plot{PlotlyPackage}) + render(_pyplot_head_template, src = Pkg.dir("Plots","deps","plotly-latest.min.js")) end -function build_plotly_json() +function html_body(plt::Plot{PlotlyPackage}) + w, h = plt.plotargs[:size] + + # TODO: get the real ones + seriesargs = [Dict(:x => collect(1:5), :y => rand(5))] + plotargs = Dict(:margin => 0) + + render(_pyplot_body_template, + width=w, + height=h, + seriesargs = JSON.json(seriesargs), + plotargs = JSON.json(plotargs) + ) end function Base.display(::PlotsDisplay, plt::Plot{PlotlyPackage}) + standalone_html_window(plt) - filename = string(tempname(), ".html") - output = open(filename, "w") - w, h = plt.plotargs[:size] + # filename = string(tempname(), ".html") + # output = open(filename, "w") + # w, h = plt.plotargs[:size] - write(output, - """ - - - - Plots.jl (Plotly) - - - -
- + # + #
+ # - - """) - close(output) + # write(output, + # """ + # + # + # """) + # close(output) - open_browser_window(filename) + # open_browser_window(filename) end function Base.display(::PlotsDisplay, plt::Subplot{PlotlyPackage}) diff --git a/src/backends/web.jl b/src/backends/web.jl new file mode 100644 index 00000000..1d7ffefd --- /dev/null +++ b/src/backends/web.jl @@ -0,0 +1,42 @@ + +# NOTE: backend should implement `html_body` and `html_head` + +# CREDIT: parts of this implementation were inspired by @joshday's PlotlyLocal.jl + +using Mustache, JSON + +const _html_template = mt""" + + + + {{:title}} + {{:head}} + + + {{:body}} + + +""" + + +function standalone_html(plt::PlottingObject; title::AbstractString = "Plots.jl ($(backend_name(plt.backend)))") + render(_html_template, title = title, body = html_body(plt), head = html_head(plt)) +end + +function open_browser_window(filename::AbstractString) + @osx_only return run(`open $(filename)`) + @linux_only return run(`xdg-open $(filename)`) + @windows_only return run(`$(ENV["COMSPEC"]) /c start $(filename)`) + warn("Unknown OS... cannot open browser window.") +end + +function standalone_html_window(plt::PlottingObject; kw...) + html = standalone_html(plt; kw...) + println(html) + filename = string(tempname(), ".html") + output = open(filename, "w") + write(output, html) + close(output) + open_browser_window(filename) +end + diff --git a/src/plotter.jl b/src/plotter.jl index da06da06..6ec45ad2 100644 --- a/src/plotter.jl +++ b/src/plotter.jl @@ -49,7 +49,7 @@ include("backends/pyplot.jl") include("backends/immerse.jl") include("backends/winston.jl") include("backends/bokeh.jl") -include("backends/plotly.jl") +# include("backends/plotly.jl") # --------------------------------------------------------- @@ -236,7 +236,8 @@ function backend() elseif currentBackendSymbol == :plotly try - # TODO: any setup + @eval include("src/backends/web.jl") + @eval include("src/backends/plotly.jl") catch err warn("Couldn't setup Plotly") rethrow(err)