working on plotly/web

This commit is contained in:
Thomas Breloff 2015-11-20 16:25:50 -05:00
parent f3a7734584
commit 6d2a1aafd2
4 changed files with 108 additions and 38 deletions

View File

@ -71,52 +71,79 @@ function Base.writemime(io::IO, ::MIME"image/png", plt::PlottingObject{PlotlyPac
# TODO: write a png to io # TODO: write a png to io
end end
function open_browser_window(filename::AbstractString)
@osx_only return run(`open $(filename)`) # function build_plotly_json()
@linux_only return run(`xdg-open $(filename)`) # end
@windows_only return run(`$(ENV["COMSPEC"]) /c start $(filename)`)
warn("Unknown OS... cannot open browser window.") const _pyplot_head_template = mt"""
<script src="{{:src}}"></script>
"""
const _pyplot_body_template = mt"""
<div id="myplot" style="width:{{:width}}px;height:{{:height}}px;"></div>
<script charset="utf-8">
PLOT = document.getElementById('myplot');
Plotly.plot(PLOT, {{:seriesargs}}, {{:plotargs}});
</script>
"""
function html_head(plt::Plot{PlotlyPackage})
render(_pyplot_head_template, src = Pkg.dir("Plots","deps","plotly-latest.min.js"))
end 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 end
function Base.display(::PlotsDisplay, plt::Plot{PlotlyPackage}) function Base.display(::PlotsDisplay, plt::Plot{PlotlyPackage})
standalone_html_window(plt)
filename = string(tempname(), ".html") # filename = string(tempname(), ".html")
output = open(filename, "w") # output = open(filename, "w")
w, h = plt.plotargs[:size] # w, h = plt.plotargs[:size]
write(output, # write(output,
""" # """
<!DOCTYPE html> # <!DOCTYPE html>
<html> # <html>
<head> # <head>
<title>Plots.jl (Plotly)</title> # <title>Plots.jl (Plotly)</title>
<meta charset="utf-8"> # <meta charset="utf-8">
<script src="$(Pkg.dir("Plots"))/src/backends/plotly-latest.min.js"></script> # <script src="$(Pkg.dir("Plots"))/src/backends/plotly-latest.min.js"></script>
</head> # </head>
<div id="myplot" style="width:$(w)px;height:$(h)px;"></div> # <div id="myplot" style="width:$(w)px;height:$(h)px;"></div>
<script charset="utf-8"> # <script charset="utf-8">
PLOT = document.getElementById('myplot'); # PLOT = document.getElementById('myplot');
Plotly.plot(PLOT, [{ # Plotly.plot(PLOT, [{
x: [1, 2, 3, 4, 5], # x: [1, 2, 3, 4, 5],
y: [1, 2, 4, 8, 16] # y: [1, 2, 4, 8, 16]
}], # }],
{margin: { t: 0 }}); # {margin: { t: 0 }});
""") # """)
# build_plotly_json(plt) # # build_plotly_json(plt)
# print(output, json) # # print(output, json)
write(output, # write(output,
""" # """
</script> # </script>
</html> # </html>
""") # """)
close(output) # close(output)
open_browser_window(filename) # open_browser_window(filename)
end end
function Base.display(::PlotsDisplay, plt::Subplot{PlotlyPackage}) function Base.display(::PlotsDisplay, plt::Subplot{PlotlyPackage})

42
src/backends/web.jl Normal file
View File

@ -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"""
<!DOCTYPE html>
<html>
<head>
<title>{{:title}}</title>
{{:head}}
</head>
<body>
{{:body}}
</body>
</html>
"""
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

View File

@ -49,7 +49,7 @@ include("backends/pyplot.jl")
include("backends/immerse.jl") include("backends/immerse.jl")
include("backends/winston.jl") include("backends/winston.jl")
include("backends/bokeh.jl") include("backends/bokeh.jl")
include("backends/plotly.jl") # include("backends/plotly.jl")
# --------------------------------------------------------- # ---------------------------------------------------------
@ -236,7 +236,8 @@ function backend()
elseif currentBackendSymbol == :plotly elseif currentBackendSymbol == :plotly
try try
# TODO: any setup @eval include("src/backends/web.jl")
@eval include("src/backends/plotly.jl")
catch err catch err
warn("Couldn't setup Plotly") warn("Couldn't setup Plotly")
rethrow(err) rethrow(err)