From 7ae43ee9f6a437adec17dfa47c35df044791c808 Mon Sep 17 00:00:00 2001 From: Daniel Schwabeneder Date: Wed, 7 Aug 2019 16:40:52 +0200 Subject: [PATCH] use plotly implementation for html/js/ijulia --- src/backends/plotly.jl | 20 +++++++++++++------- src/backends/plotlyjs.jl | 14 +++++++++++--- src/ijulia.jl | 5 ++++- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 007c3d05..1896d37a 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -804,9 +804,12 @@ plotly_series_json(plt::Plot) = JSON.json(plotly_series(plt), 4) # ---------------------------------------------------------------- +html_head(plt::Plot{PlotlyBackend}) = plotly_html_head(plt) +html_body(plt::Plot{PlotlyBackend}) = plotly_html_body(plt) + const ijulia_initialized = Ref(false) -function html_head(plt::Plot{PlotlyBackend}) +function plotly_html_head(plt::Plot) local_file = ("file://" * plotly_local_file_path) plotly = use_local_dependencies[] ? local_file : plotly_remote_file_path if isijulia() && !ijulia_initialized[] @@ -825,7 +828,7 @@ function html_head(plt::Plot{PlotlyBackend}) return "" end -function html_body(plt::Plot{PlotlyBackend}, style = nothing) +function plotly_html_body(plt, style = nothing) if style == nothing w, h = plt[:size] style = "width:$(w)px;height:$(h)px;" @@ -841,17 +844,14 @@ function html_body(plt::Plot{PlotlyBackend}, style = nothing) html end -function js_body(plt::Plot{PlotlyBackend}, uuid) +function js_body(plt::Plot, uuid) js = """ PLOT = document.getElementById('$(uuid)'); Plotly.plot(PLOT, $(plotly_series_json(plt)), $(plotly_layout_json(plt))); """ end - -# ---------------------------------------------------------------- - -function _show(io::IO, ::MIME"application/vnd.plotly.v1+json", plot::Plot{PlotlyBackend}) +function plotly_show_js(io::IO, plot::Plot) data = [] for series in plot.series_list append!(data, plotly_series(plot, series)) @@ -860,6 +860,12 @@ function _show(io::IO, ::MIME"application/vnd.plotly.v1+json", plot::Plot{Plotly JSON.print(io, Dict(:data => data, :layout => layout)) end +# ---------------------------------------------------------------- + +function _show(io::IO, ::MIME"application/vnd.plotly.v1+json", plot::Plot{PlotlyBackend}) + plotly_show_js(io, plot) +end + function _show(io::IO, ::MIME"text/html", plt::Plot{PlotlyBackend}) write(io, standalone_html(plt)) diff --git a/src/backends/plotlyjs.jl b/src/backends/plotlyjs.jl index ede8cbf6..8e742e4f 100644 --- a/src/backends/plotlyjs.jl +++ b/src/backends/plotlyjs.jl @@ -77,9 +77,17 @@ end const _plotlyjs_showformats = ["text/html", "application/vnd.plotly.v1+json"] -for mime in ["text/html", "application/vnd.plotly.v1+json"] - @eval _show(io::IO, mime::MIME{Symbol($mime)}, plt::Plot{PlotlyJSBackend}) = show(io, mime, plotlyjs_syncplot(plt)) -end +# for mime in ["text/html", "application/vnd.plotly.v1+json"] +# @eval _show(io::IO, mime::MIME{Symbol($mime)}, plt::Plot{PlotlyJSBackend}) = show(io, mime, plotlyjs_syncplot(plt)) +# end + +# Use the Plotly implementation for json and html: +_show(io::IO, mime::MIME"application/vnd.plotly.v1+json", plt::Plot{PlotlyJSBackend}) = plotly_show_js(io, plot) + +html_head(plt::Plot{PlotlyJSBackend}) = plotly_html_head(plt) +html_body(plt::Plot{PlotlyJSBackend}) = plotly_html_body(plt) + +_show(io::IO, ::MIME"text/html", plt::Plot{PlotlyJSBackend}) = write(io, standalone_html(plt)) # _show(io::IO, ::MIME"text/html", plt::Plot{PlotlyJSBackend}) = show(io, ::MIME"text/html", plt.o) # _show(io::IO, ::MIME"image/svg+xml", plt::Plot{PlotlyJSBackend}) = PlotlyJS.savefig(io, plt.o, format="svg") diff --git a/src/ijulia.jl b/src/ijulia.jl index a54c0ca9..1f0ef219 100644 --- a/src/ijulia.jl +++ b/src/ijulia.jl @@ -20,7 +20,10 @@ frontends like jupyterlab and nteract. _ijulia__extra_mime_info!(plt::Plot, out::Dict) = out function _ijulia__extra_mime_info!(plt::Plot{PlotlyJSBackend}, out::Dict) - out["application/vnd.plotly.v1+json"] = JSON.lower(plt.o) + out["application/vnd.plotly.v1+json"] = Dict( + :data => plotly_series(plt), + :layout => plotly_layout(plt) + ) out end