Merge pull request #1419 from sglyon/sl/plotly_mime

ENH: provide "application/vnd.plotly.v1+json" for Plotly and PlotlyJS backends
This commit is contained in:
Michael Krabbe Borregaard 2018-03-01 07:41:12 +01:00 committed by GitHub
commit ee2e7d509c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 8 deletions

View File

@ -14,6 +14,7 @@ using Base.Meta
@reexport using PlotThemes
import Showoff
import StatsBase
import JSON
using Requires

View File

@ -86,8 +86,6 @@ const _plotly_js_path_remote = "https://cdn.plot.ly/plotly-latest.min.js"
function _initialize_backend(::PlotlyBackend; kw...)
@eval begin
import JSON
_js_code = open(readstring, _plotly_js_path, "r")
# borrowed from https://github.com/plotly/plotly.py/blob/2594076e29584ede2d09f2aa40a8a195b3f3fc66/plotly/offline/offline.py#L64-L71 c/o @spencerlyon2
@ -740,15 +738,17 @@ function plotly_hover!(d_out::KW, hover)
end
# get a list of dictionaries, each representing the series params
function plotly_series_json(plt::Plot)
function plotly_series(plt::Plot)
slist = []
for series in plt.series_list
append!(slist, plotly_series(plt, series))
end
JSON.json(slist)
# JSON.json(map(series -> plotly_series(plt, series), plt.series_list))
slist
end
# get json string for a list of dictionaries, each representing the series params
plotly_series_json(plt::Plot) = JSON.json(plotly_series(plt))
# ----------------------------------------------------------------
const _use_remote = Ref(false)

View File

@ -261,23 +261,47 @@ end
@require IJulia begin
if IJulia.inited
"""
Add extra jupyter mimetypes to display_dict based on the plot backed.
The default is nothing, except for plotly based backends, where it
adds data for `application/vnd.plotly.v1+json` that is used in
frontends like jupyterlab and nteract.
"""
_extra_mime_info!(plt::Plot, out::Dict) = out
function _extra_mime_info!(plt::Plot{PlotlyJSBackend}, out::Dict)
out["application/vnd.plotly.v1+json"] = JSON.lower(plt.o)
out
end
function _extra_mime_info!(plt::Plot{PlotlyBackend}, out::Dict)
out["application/vnd.plotly.v1+json"] = Dict(
:data => plotly_series(plt),
:layout => plotly_layout(plt)
)
out
end
function IJulia.display_dict(plt::Plot)
output_type = Symbol(plt.attr[:html_output_format])
if output_type == :auto
output_type = get(_best_html_output_type, backend_name(plt.backend), :svg)
end
out = Dict()
if output_type == :png
mime = "image/png"
Dict{String,String}(mime => base64encode(show, MIME(mime), plt))
out[mime] = base64encode(show, MIME(mime), plt)
elseif output_type == :svg
mime = "image/svg+xml"
Dict{String,String}(mime => sprint(show, MIME(mime), plt))
out[mime] = sprint(show, MIME(mime), plt)
elseif output_type == :html
mime = "text/html"
Dict{String,String}(mime => sprint(show, MIME(mime), plt))
out[mime] = sprint(show, MIME(mime), plt)
else
error("Unsupported output type $output_type")
end
_extra_mime_info!(plt, out)
out
end
# default text/plain passes to html... handles Interact issues