/
PlotlyJS.jl
175 lines (144 loc) · 6.02 KB
/
PlotlyJS.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
module PlotlyJS
using Base64
using Reexport
@reexport using PlotlyBase
using JSON
using REPL, Pkg, Pkg.Artifacts, DelimitedFiles # stdlib
# need to import some functions because methods are meta-generated
import PlotlyBase:
restyle!, relayout!, update!, addtraces!, deletetraces!, movetraces!,
redraw!, extendtraces!, prependtraces!, purge!, to_image, download_image,
restyle, relayout, update, addtraces, deletetraces, movetraces, redraw,
extendtraces, prependtraces, prep_kwargs, sizes, _tovec,
react, react!, add_trace!
using WebIO
using JSExpr
using JSExpr: @var, @new
using Blink
using Pkg.Artifacts
if !isdefined(Base, :get_extension)
using Requires
end
export plot, dataset, list_datasets, make_subplots, savefig, mgrid
# globals for this package
const _pkg_root = dirname(dirname(@__FILE__))
const _js_path = joinpath(artifact"plotly-artifacts", "plotly.min.js")
const _js_version = include(joinpath(_pkg_root, "deps", "plotly_cdn_version.jl"))
const _js_cdn_path = "https://cdn.plot.ly/plotly-$(_js_version).min.js"
const _mathjax_cdn_path =
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_SVG"
struct PlotlyJSDisplay <: AbstractDisplay end
# include the rest of the core parts of the package
include("display.jl")
include("util.jl")
include("kaleido.jl")
make_subplots(;kwargs...) = plot(Layout(Subplots(;kwargs...)))
@doc (@doc Subplots) make_subplots
function docs()
schema_path = joinpath(dirname(dirname(@__FILE__)), "deps", "schema.html")
if !isfile(schema_path)
msg = "schema docs not built. Run `Pkg.build(\"PlotlyJS\")` to generate"
error(msg)
end
w = Blink.Window()
wait(w.content)
Blink.content!(w, "html", open(f -> read(f, String), schema_path), fade=false, async=false)
end
@enum RENDERERS BLINK IJULIA BROWSER DOCS
const DEFAULT_RENDERER = Ref(BLINK)
function set_default_renderer(s::RENDERERS)
global DEFAULT_RENDERER
DEFAULT_RENDERER[] = s
end
@inline get_renderer() = DEFAULT_RENDERER[]
list_datasets() = readdir(joinpath(artifact"plotly-artifacts", "datasets"))
function check_dataset_exists(name::String)
ds = list_datasets()
name_ext = Dict(name => strip(ext, '.') for (name, ext) in splitext.(ds))
if !haskey(name_ext, name)
error("Unknown dataset $name, known datasets are $(collect(keys(name_ext)))")
end
ds_path = joinpath(artifact"plotly-artifacts", "datasets", "$(name).$(name_ext[name])")
return ds_path
end
function dataset(name::String)::Dict{String,Any}
ds_path = check_dataset_exists(name)
if endswith(ds_path, "csv")
# if csv, use DelimitedFiles and convert to dict
data = DelimitedFiles.readdlm(ds_path, ',')
return Dict(zip(data[1, :], data[2:end, i] for i in 1:size(data, 2)))
elseif endswith(ds_path, "json")
# use json
return JSON.parsefile(ds_path)
end
error("should not ever get here!!! Please file an issue")
end
function __init__()
_build_log = joinpath(_pkg_root, "deps", "build.log")
if isfile(_build_log) && occursin("Warning:", read(_build_log, String))
@warn("Warnings were generated during the last build of PlotlyJS: please check the build log at $_build_log")
end
kaleido_task = Base.Threads.@spawn _start_kaleido_process()
if !isfile(_js_path)
@info("plotly.js javascript libary not found -- downloading now")
include(joinpath(_pkg_root, "deps", "build.jl"))
end
# set default renderer
# First check env var
env_val = get(ENV, "PLOTLY_RENDERER_JULIA", missing)
if !ismissing(env_val)
env_symbol = Symbol(uppercase(env_val))
options = Dict(v => k for (k, v) in collect(Base.Enums.namemap(PlotlyJS.RENDERERS)))
renderer_int = get(options, env_symbol, missing)
if ismissing(renderer_int)
@warn "Unknown value for env var `PLOTLY_RENDERER_JULIA` \"$(env_val)\", known options are $(string.(keys(options)))"
else
set_default_renderer(RENDERERS(renderer_int))
end
else
# we have no env-var
# check IJULIA
isdefined(Main, :IJulia) && Main.IJulia.inited && set_default_renderer(IJULIA)
end
# set up display
insert!(Base.Multimedia.displays, findlast(x -> x isa Base.TextDisplay || x isa REPL.REPLDisplay, Base.Multimedia.displays) + 1, PlotlyJSDisplay())
atreplinit(i -> begin
while PlotlyJSDisplay() in Base.Multimedia.displays
popdisplay(PlotlyJSDisplay())
end
insert!(Base.Multimedia.displays, findlast(x -> x isa REPL.REPLDisplay, Base.Multimedia.displays) + 1, PlotlyJSDisplay())
end)
@static if !isdefined(Base, :get_extension)
@require JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" include("../ext/JSON3Ext.jl")
@require IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a" include("../ext/IJuliaExt.jl")
@require CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" begin
include("../ext/CSVExt.jl")
@require DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" begin
include("../ext/DataFramesExt.jl")
end
end
end
wait(kaleido_task)
if ccall(:jl_generating_output, Cint, ()) == 1
# ensure precompilation of packages depending on PlotlyJS finishes
if isdefined(P, :proc)
close(P.stdin)
wait(P.proc)
end
end
end
# for methods that update the layout, first apply to the plot, then let plotly.js
# deal with the rest via the react function
for (k, v) in vcat(PlotlyBase._layout_obj_updaters, PlotlyBase._layout_vector_updaters)
@eval function PlotlyBase.$(k)(p::SyncPlot, args...;kwargs...)
$(k)(p.plot, args...; kwargs...)
send_command(p.scope, :react, p.plot.data, p.plot.layout)
end
end
for k in [:add_hrect!, :add_hline!, :add_vrect!, :add_vline!, :add_shape!, :add_layout_image!]
@eval function PlotlyBase.$(k)(p::SyncPlot, args...;kwargs...)
$(k)(p.plot, args...; kwargs...)
send_command(p.scope, :react, p.plot.data, p.plot.layout)
end
end
end # module