fix IJulia

This commit is contained in:
SimonDanisch 2018-10-05 12:27:09 +02:00
parent ee4fc2803b
commit 7e0960e7a0
2 changed files with 19 additions and 17 deletions

View File

@ -21,7 +21,11 @@ using Requires
if isfile(joinpath(@__DIR__, "..", "deps", "deps.jl")) if isfile(joinpath(@__DIR__, "..", "deps", "deps.jl"))
include(joinpath(@__DIR__, "..", "deps", "deps.jl")) include(joinpath(@__DIR__, "..", "deps", "deps.jl"))
else else
error("Plots not build, please run Pkg.build(\"Plots\")") # 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 now ENV["PLOTS_HOST_DEPENDENCY_LOCAL"] = "true" is given
# If the above env is set + plotly_local_file_path = "", it will warn in the __init__ functtion
const plotly_local_file_path = ""
end end
export export

View File

@ -862,26 +862,24 @@ plotly_series_json(plt::Plot) = JSON.json(plotly_series(plt))
const ijulia_initialized = Ref(false) const ijulia_initialized = Ref(false)
function html_head(plt::Plot{PlotlyBackend}) function html_head(plt::Plot{PlotlyBackend})
if isfile(plotly_local_file_path) && isijulia() && !ijulia_initialized[] local_file = ("file://" * plotly_local_file_path)
js_code = read(plotly_local_file_path, String) plotly = use_local_dependencies[] ? local_file : plotly_remote_file_path
# borrowed from https://github.com/plotly/plotly.py/blob/2594076e29584ede2d09f2aa40a8a195b3f3fc66/plotly/offline/offline.py#L64-L71 c/o @spencerlyon2 if isijulia() && !ijulia_initialized[]
js_script = """ # using requirejs seems to be key to load a js depency in IJulia!
<script type='text/javascript'> # https://requirejs.org/docs/start.html
define('plotly', function(require, exports, module) { # https://github.com/JuliaLang/IJulia.jl/issues/345
$(js_code) display("text/html", """
}); <script type="text/javascript">
require(['plotly'], function(Plotly) { requirejs([$(repr(plotly))], function(p) {
window.Plotly = Plotly; window.Plotly = p
}); });
</script> </script>
""" """)
# if we're in IJulia call setupnotebook to load js and css
display("text/html", js_script)
ijulia_initialized[] = true ijulia_initialized[] = true
end end
local_file = ("file://" * plotly_local_file_path) # IJulia just needs one initialization
jsfilename = use_local_dependencies[] ? local_file : plotly_remote_file_path isijulia() && return ""
"<script src=\"$jsfilename\"></script>" return "<script src=$(repr(plotly))></script>"
end end
function html_body(plt::Plot{PlotlyBackend}, style = nothing) function html_body(plt::Plot{PlotlyBackend}, style = nothing)