ENH: provide "application/vnd.plotly.v1+json" for Plotly and PlotlyJS backends
For use in jupyterlab and nteract closes #1418 closes #1386
This commit is contained in:
parent
18032bb9af
commit
461e93fc56
@ -14,6 +14,7 @@ using Base.Meta
|
|||||||
@reexport using PlotThemes
|
@reexport using PlotThemes
|
||||||
import Showoff
|
import Showoff
|
||||||
import StatsBase
|
import StatsBase
|
||||||
|
import JSON
|
||||||
|
|
||||||
using Requires
|
using Requires
|
||||||
|
|
||||||
|
|||||||
@ -86,8 +86,6 @@ const _plotly_js_path_remote = "https://cdn.plot.ly/plotly-latest.min.js"
|
|||||||
|
|
||||||
function _initialize_backend(::PlotlyBackend; kw...)
|
function _initialize_backend(::PlotlyBackend; kw...)
|
||||||
@eval begin
|
@eval begin
|
||||||
import JSON
|
|
||||||
|
|
||||||
_js_code = open(readstring, _plotly_js_path, "r")
|
_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
|
# 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
|
end
|
||||||
|
|
||||||
# get a list of dictionaries, each representing the series params
|
# get a list of dictionaries, each representing the series params
|
||||||
function plotly_series_json(plt::Plot)
|
function plotly_series(plt::Plot)
|
||||||
slist = []
|
slist = []
|
||||||
for series in plt.series_list
|
for series in plt.series_list
|
||||||
append!(slist, plotly_series(plt, series))
|
append!(slist, plotly_series(plt, series))
|
||||||
end
|
end
|
||||||
JSON.json(slist)
|
slist
|
||||||
# JSON.json(map(series -> plotly_series(plt, series), plt.series_list))
|
|
||||||
end
|
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)
|
const _use_remote = Ref(false)
|
||||||
|
|||||||
@ -261,23 +261,47 @@ end
|
|||||||
@require IJulia begin
|
@require IJulia begin
|
||||||
if IJulia.inited
|
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)
|
function IJulia.display_dict(plt::Plot)
|
||||||
output_type = Symbol(plt.attr[:html_output_format])
|
output_type = Symbol(plt.attr[:html_output_format])
|
||||||
if output_type == :auto
|
if output_type == :auto
|
||||||
output_type = get(_best_html_output_type, backend_name(plt.backend), :svg)
|
output_type = get(_best_html_output_type, backend_name(plt.backend), :svg)
|
||||||
end
|
end
|
||||||
|
out = Dict()
|
||||||
if output_type == :png
|
if output_type == :png
|
||||||
mime = "image/png"
|
mime = "image/png"
|
||||||
Dict{String,String}(mime => base64encode(show, MIME(mime), plt))
|
out[mime] = base64encode(show, MIME(mime), plt)
|
||||||
elseif output_type == :svg
|
elseif output_type == :svg
|
||||||
mime = "image/svg+xml"
|
mime = "image/svg+xml"
|
||||||
Dict{String,String}(mime => sprint(show, MIME(mime), plt))
|
out[mime] = sprint(show, MIME(mime), plt)
|
||||||
elseif output_type == :html
|
elseif output_type == :html
|
||||||
mime = "text/html"
|
mime = "text/html"
|
||||||
Dict{String,String}(mime => sprint(show, MIME(mime), plt))
|
out[mime] = sprint(show, MIME(mime), plt)
|
||||||
else
|
else
|
||||||
error("Unsupported output type $output_type")
|
error("Unsupported output type $output_type")
|
||||||
end
|
end
|
||||||
|
_extra_mime_info!(plt, out)
|
||||||
|
out
|
||||||
end
|
end
|
||||||
|
|
||||||
# default text/plain passes to html... handles Interact issues
|
# default text/plain passes to html... handles Interact issues
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user