Merge pull request #1777 from JuliaPlots/sd-remote
use remote urls plotly
This commit is contained in:
commit
f5b793f80c
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,3 +6,4 @@ examples/.ipynb_checkpoints/*
|
|||||||
examples/meetup/.ipynb_checkpoints/*
|
examples/meetup/.ipynb_checkpoints/*
|
||||||
deps/plotly-latest.min.js
|
deps/plotly-latest.min.js
|
||||||
deps/build.log
|
deps/build.log
|
||||||
|
deps/deps.jl
|
||||||
|
|||||||
20
deps/build.jl
vendored
20
deps/build.jl
vendored
@ -1,8 +1,18 @@
|
|||||||
|
|
||||||
#TODO: download https://cdn.plot.ly/plotly-latest.min.js to deps/ if it doesn't exist
|
#TODO: download https://cdn.plot.ly/plotly-latest.min.js to deps/ if it doesn't exist
|
||||||
|
file_path = ""
|
||||||
local_fn = joinpath(dirname(@__FILE__), "plotly-latest.min.js")
|
if get(ENV, "PLOTS_HOST_DEPENDENCY_LOCAL", "false") == "true"
|
||||||
if !isfile(local_fn)
|
global file_path
|
||||||
@info("Cannot find deps/plotly-latest.min.js... downloading latest version.")
|
local_fn = joinpath(dirname(@__FILE__), "plotly-latest.min.js")
|
||||||
download("https://cdn.plot.ly/plotly-latest.min.js", local_fn)
|
if !isfile(local_fn)
|
||||||
|
@info("Cannot find deps/plotly-latest.min.js... downloading latest version.")
|
||||||
|
download("https://cdn.plot.ly/plotly-latest.min.js", local_fn)
|
||||||
|
isfile(local_fn) && (file_path = local_fn)
|
||||||
|
else
|
||||||
|
file_path = local_fn
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
open("deps.jl", "w") do io
|
||||||
|
println(io, "const plotly_local_file_path = $(repr(file_path))")
|
||||||
end
|
end
|
||||||
|
|||||||
11
src/Plots.jl
11
src/Plots.jl
@ -20,6 +20,17 @@ import JSON
|
|||||||
|
|
||||||
using Requires
|
using Requires
|
||||||
|
|
||||||
|
if isfile(joinpath(@__DIR__, "..", "deps", "deps.jl"))
|
||||||
|
include(joinpath(@__DIR__, "..", "deps", "deps.jl"))
|
||||||
|
else
|
||||||
|
# This is a bit dirty, but I don't really see why anyone should be forced
|
||||||
|
# to build Plots, while it will just include exactly the below line
|
||||||
|
# as long as `ENV["PLOTS_HOST_DEPENDENCY_LOCAL"] = "true"` is not set.
|
||||||
|
# If the above env is set + `plotly_local_file_path == ""``,
|
||||||
|
# it will warn in the __init__ function to run build
|
||||||
|
const plotly_local_file_path = ""
|
||||||
|
end
|
||||||
|
|
||||||
export
|
export
|
||||||
grid,
|
grid,
|
||||||
bbox,
|
bbox,
|
||||||
|
|||||||
@ -17,30 +17,9 @@ end
|
|||||||
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------------------------
|
# --------------------------------------------------------------------------------------
|
||||||
|
const plotly_remote_file_path = "https://cdn.plot.ly/plotly-latest.min.js"
|
||||||
|
|
||||||
|
|
||||||
const _plotly_js_path = joinpath(dirname(@__FILE__), "..", "..", "deps", "plotly-latest.min.js")
|
|
||||||
const _plotly_js_path_remote = "https://cdn.plot.ly/plotly-latest.min.js"
|
|
||||||
|
|
||||||
_js_code = open(read, _plotly_js_path, "r")
|
|
||||||
|
|
||||||
# borrowed from https://github.com/plotly/plotly.py/blob/2594076e29584ede2d09f2aa40a8a195b3f3fc66/plotly/offline/offline.py#L64-L71 c/o @spencerlyon2
|
|
||||||
_js_script = """
|
|
||||||
<script type='text/javascript'>
|
|
||||||
define('plotly', function(require, exports, module) {
|
|
||||||
$(_js_code)
|
|
||||||
});
|
|
||||||
require(['plotly'], function(Plotly) {
|
|
||||||
window.Plotly = Plotly;
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
"""
|
|
||||||
|
|
||||||
# if we're in IJulia call setupnotebook to load js and css
|
|
||||||
if isijulia()
|
|
||||||
display("text/html", _js_script)
|
|
||||||
end
|
|
||||||
|
|
||||||
# if isatom()
|
# if isatom()
|
||||||
# import Atom
|
# import Atom
|
||||||
# Atom.@msg evaljs(_js_code)
|
# Atom.@msg evaljs(_js_code)
|
||||||
@ -48,8 +27,6 @@ end
|
|||||||
using UUIDs
|
using UUIDs
|
||||||
|
|
||||||
push!(_initialized_backends, :plotly)
|
push!(_initialized_backends, :plotly)
|
||||||
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------
|
# ----------------------------------------------------------------
|
||||||
|
|
||||||
const _plotly_legend_pos = KW(
|
const _plotly_legend_pos = KW(
|
||||||
@ -61,7 +38,7 @@ const _plotly_legend_pos = KW(
|
|||||||
:bottomright => [1., 0.],
|
:bottomright => [1., 0.],
|
||||||
:topright => [1., 1.],
|
:topright => [1., 1.],
|
||||||
:topleft => [0., 1.]
|
:topleft => [0., 1.]
|
||||||
)
|
)
|
||||||
|
|
||||||
plotly_legend_pos(pos::Symbol) = get(_plotly_legend_pos, pos, [1.,1.])
|
plotly_legend_pos(pos::Symbol) = get(_plotly_legend_pos, pos, [1.,1.])
|
||||||
plotly_legend_pos(v::Tuple{S,T}) where {S<:Real, T<:Real} = v
|
plotly_legend_pos(v::Tuple{S,T}) where {S<:Real, T<:Real} = v
|
||||||
@ -828,12 +805,27 @@ plotly_series_json(plt::Plot) = JSON.json(plotly_series(plt))
|
|||||||
|
|
||||||
# ----------------------------------------------------------------
|
# ----------------------------------------------------------------
|
||||||
|
|
||||||
const _use_remote = Ref(false)
|
const ijulia_initialized = Ref(false)
|
||||||
|
|
||||||
function html_head(plt::Plot{PlotlyBackend})
|
function html_head(plt::Plot{PlotlyBackend})
|
||||||
jsfilename = _use_remote[] ? _plotly_js_path_remote : ("file://" * _plotly_js_path)
|
local_file = ("file://" * plotly_local_file_path)
|
||||||
# "<script src=\"$(joinpath(dirname(@__FILE__),"..","..","deps","plotly-latest.min.js"))\"></script>"
|
plotly = use_local_dependencies[] ? local_file : plotly_remote_file_path
|
||||||
"<script src=\"$jsfilename\"></script>"
|
if isijulia() && !ijulia_initialized[]
|
||||||
|
# using requirejs seems to be key to load a js depency in IJulia!
|
||||||
|
# https://requirejs.org/docs/start.html
|
||||||
|
# https://github.com/JuliaLang/IJulia.jl/issues/345
|
||||||
|
display("text/html", """
|
||||||
|
<script type="text/javascript">
|
||||||
|
requirejs([$(repr(plotly))], function(p) {
|
||||||
|
window.Plotly = p
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
""")
|
||||||
|
ijulia_initialized[] = true
|
||||||
|
end
|
||||||
|
# IJulia just needs one initialization
|
||||||
|
isijulia() && return ""
|
||||||
|
return "<script src=$(repr(plotly))></script>"
|
||||||
end
|
end
|
||||||
|
|
||||||
function html_body(plt::Plot{PlotlyBackend}, style = nothing)
|
function html_body(plt::Plot{PlotlyBackend}, style = nothing)
|
||||||
@ -854,8 +846,8 @@ end
|
|||||||
|
|
||||||
function js_body(plt::Plot{PlotlyBackend}, uuid)
|
function js_body(plt::Plot{PlotlyBackend}, uuid)
|
||||||
js = """
|
js = """
|
||||||
PLOT = document.getElementById('$(uuid)');
|
PLOT = document.getElementById('$(uuid)');
|
||||||
Plotly.plot(PLOT, $(plotly_series_json(plt)), $(plotly_layout_json(plt)));
|
Plotly.plot(PLOT, $(plotly_series_json(plt)), $(plotly_layout_json(plt)));
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -42,8 +42,14 @@ function write_temp_html(plt::AbstractPlot)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function standalone_html_window(plt::AbstractPlot)
|
function standalone_html_window(plt::AbstractPlot)
|
||||||
|
old = use_local_dependencies[] # save state to restore afterwards
|
||||||
|
# if we open a browser ourself, we can host local files, so
|
||||||
|
# when we have a local plotly downloaded this is the way to go!
|
||||||
|
use_local_dependencies[] = isfile(plotly_local_file_path)
|
||||||
filename = write_temp_html(plt)
|
filename = write_temp_html(plt)
|
||||||
open_browser_window(filename)
|
open_browser_window(filename)
|
||||||
|
# restore for other backends
|
||||||
|
use_local_dependencies[] = old
|
||||||
end
|
end
|
||||||
|
|
||||||
# uses wkhtmltopdf/wkhtmltoimage: http://wkhtmltopdf.org/downloads.html
|
# uses wkhtmltopdf/wkhtmltoimage: http://wkhtmltopdf.org/downloads.html
|
||||||
|
|||||||
18
src/init.jl
18
src/init.jl
@ -1,4 +1,7 @@
|
|||||||
|
const use_local_dependencies = Ref(false)
|
||||||
|
|
||||||
function __init__()
|
function __init__()
|
||||||
|
|
||||||
if isdefined(Main, :PLOTS_DEFAULTS)
|
if isdefined(Main, :PLOTS_DEFAULTS)
|
||||||
if haskey(Main.PLOTS_DEFAULTS, :theme)
|
if haskey(Main.PLOTS_DEFAULTS, :theme)
|
||||||
theme(Main.PLOTS_DEFAULTS[:theme])
|
theme(Main.PLOTS_DEFAULTS[:theme])
|
||||||
@ -27,10 +30,11 @@ function __init__()
|
|||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
# IJulia
|
# IJulia
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
|
use_local = false
|
||||||
@require IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a" begin
|
@require IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a" begin
|
||||||
if IJulia.inited
|
if IJulia.inited
|
||||||
|
# IJulia is more stable with local file
|
||||||
|
use_local = isfile(plotly_local_file_path)
|
||||||
"""
|
"""
|
||||||
Add extra jupyter mimetypes to display_dict based on the plot backed.
|
Add extra jupyter mimetypes to display_dict based on the plot backed.
|
||||||
|
|
||||||
@ -81,6 +85,16 @@ function __init__()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if haskey(ENV, "PLOTS_HOST_DEPENDENCY_LOCAL")
|
||||||
|
use_local = ENV["PLOTS_HOST_DEPENDENCY_LOCAL"] == "true"
|
||||||
|
use_local_dependencies[] = isfile(plotly_local_file_path) && use_local
|
||||||
|
if use_local && !isfile(plotly_local_file_path)
|
||||||
|
@warn("PLOTS_HOST_DEPENDENCY_LOCAL is set to true, but no local plotly file found. run Pkg.build(\"Plots\") and make sure PLOTS_HOST_DEPENDENCY_LOCAL is set to true")
|
||||||
|
end
|
||||||
|
else
|
||||||
|
use_local_dependencies[] = use_local
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user