working on plotly/web
This commit is contained in:
parent
f3a7734584
commit
6d2a1aafd2
@ -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"""
|
||||
<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
|
||||
|
||||
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,
|
||||
"""
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Plots.jl (Plotly)</title>
|
||||
<meta charset="utf-8">
|
||||
<script src="$(Pkg.dir("Plots"))/src/backends/plotly-latest.min.js"></script>
|
||||
</head>
|
||||
<div id="myplot" style="width:$(w)px;height:$(h)px;"></div>
|
||||
<script charset="utf-8">
|
||||
PLOT = document.getElementById('myplot');
|
||||
Plotly.plot(PLOT, [{
|
||||
x: [1, 2, 3, 4, 5],
|
||||
y: [1, 2, 4, 8, 16]
|
||||
}],
|
||||
{margin: { t: 0 }});
|
||||
""")
|
||||
# write(output,
|
||||
# """
|
||||
# <!DOCTYPE html>
|
||||
# <html>
|
||||
# <head>
|
||||
# <title>Plots.jl (Plotly)</title>
|
||||
# <meta charset="utf-8">
|
||||
# <script src="$(Pkg.dir("Plots"))/src/backends/plotly-latest.min.js"></script>
|
||||
# </head>
|
||||
# <div id="myplot" style="width:$(w)px;height:$(h)px;"></div>
|
||||
# <script charset="utf-8">
|
||||
# PLOT = document.getElementById('myplot');
|
||||
# Plotly.plot(PLOT, [{
|
||||
# x: [1, 2, 3, 4, 5],
|
||||
# y: [1, 2, 4, 8, 16]
|
||||
# }],
|
||||
# {margin: { t: 0 }});
|
||||
# """)
|
||||
|
||||
# build_plotly_json(plt)
|
||||
# print(output, json)
|
||||
# # build_plotly_json(plt)
|
||||
# # print(output, json)
|
||||
|
||||
write(output,
|
||||
"""
|
||||
</script>
|
||||
</html>
|
||||
""")
|
||||
close(output)
|
||||
# write(output,
|
||||
# """
|
||||
# </script>
|
||||
# </html>
|
||||
# """)
|
||||
# close(output)
|
||||
|
||||
open_browser_window(filename)
|
||||
# open_browser_window(filename)
|
||||
end
|
||||
|
||||
function Base.display(::PlotsDisplay, plt::Subplot{PlotlyPackage})
|
||||
|
||||
42
src/backends/web.jl
Normal file
42
src/backends/web.jl
Normal 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
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user