From d0e01a8d882370d3244baf1d62ddbc66d038c1ea Mon Sep 17 00:00:00 2001 From: Jks Liu Date: Mon, 11 Jan 2021 22:48:04 +0800 Subject: [PATCH 1/4] Correct version string of plotly cache file name --- src/init.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/init.jl b/src/init.jl index 2a64844d..6673c220 100644 --- a/src/init.jl +++ b/src/init.jl @@ -79,7 +79,7 @@ function __init__() end if get(ENV, "PLOTS_HOST_DEPENDENCY_LOCAL", "false") == "true" - global plotly_local_file_path[] = joinpath(@get_scratch!("plotly"), "plotly-1.54.2.min.js") + global plotly_local_file_path[] = joinpath(@get_scratch!("plotly"), "plotly-1.57.1.min.js") if !isfile(plotly_local_file_path[]) download("https://cdn.plot.ly/plotly-1.57.1.min.js", plotly_local_file_path[]) end From 4bcafe3f5198542ddcc46e6e10299a5fb249561f Mon Sep 17 00:00:00 2001 From: Jks Liu Date: Tue, 12 Jan 2021 22:09:48 +0800 Subject: [PATCH 2/4] const variable for plotly JavaScript file name --- src/Plots.jl | 4 ++++ src/backends/plotly.jl | 2 +- src/init.jl | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Plots.jl b/src/Plots.jl index 1ce8bf65..50b4205c 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -187,6 +187,10 @@ import RecipesPipeline: SliceIt, datetimeformatter, timeformatter +# Use fixed version of Plotly instead of the latest one for stable dependency +# Ref: https://github.com/JuliaPlots/Plots.jl/pull/2779 +const _plotly_min_js_filename = "plotly-1.57.1.min.js" + include("types.jl") include("utils.jl") include("axes.jl") diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 89c97e54..005ecead 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -887,7 +887,7 @@ const ijulia_initialized = Ref(false) function plotly_html_head(plt::Plot) plotly = - use_local_dependencies[] ? ("file:///" * plotly_local_file_path[]) : "https://cdn.plot.ly/plotly-1.57.1.min.js" + use_local_dependencies[] ? ("file:///" * plotly_local_file_path[]) : "https://cdn.plot.ly/$(_plotly_min_js_filename)" include_mathjax = get(plt[:extra_plot_kwargs], :include_mathjax, "") mathjax_file = include_mathjax != "cdn" ? ("file://" * include_mathjax) : "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML" diff --git a/src/init.jl b/src/init.jl index 6673c220..22bb4b51 100644 --- a/src/init.jl +++ b/src/init.jl @@ -79,9 +79,9 @@ function __init__() end if get(ENV, "PLOTS_HOST_DEPENDENCY_LOCAL", "false") == "true" - global plotly_local_file_path[] = joinpath(@get_scratch!("plotly"), "plotly-1.57.1.min.js") + global plotly_local_file_path[] = joinpath(@get_scratch!("plotly"), _plotly_min_js_filename) if !isfile(plotly_local_file_path[]) - download("https://cdn.plot.ly/plotly-1.57.1.min.js", plotly_local_file_path[]) + download("https://cdn.plot.ly/$(_plotly_min_js_filename)", plotly_local_file_path[]) end use_local_plotlyjs[] = true From 509a8cdc7e476a0514c60f2e685311ace9a88fb1 Mon Sep 17 00:00:00 2001 From: Jks Liu Date: Mon, 18 Jan 2021 22:59:16 +0800 Subject: [PATCH 3/4] Config plotly.js during init plotly.js will be loaded only once, only when first time used plots show correctly when page re-load or re-open --- src/backends/plotly.jl | 29 ++++++++++++++--------------- src/init.jl | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 005ecead..ed7a48d1 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -883,8 +883,6 @@ plotly_series_json(plt::Plot) = JSON.json(plotly_series(plt), 4) html_head(plt::Plot{PlotlyBackend}) = plotly_html_head(plt) html_body(plt::Plot{PlotlyBackend}) = plotly_html_body(plt) -const ijulia_initialized = Ref(false) - function plotly_html_head(plt::Plot) plotly = use_local_dependencies[] ? ("file:///" * plotly_local_file_path[]) : "https://cdn.plot.ly/$(_plotly_min_js_filename)" @@ -893,20 +891,11 @@ function plotly_html_head(plt::Plot) mathjax_file = include_mathjax != "cdn" ? ("file://" * include_mathjax) : "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML" mathjax_head = include_mathjax == "" ? "" : "\n\t\t" - 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", """ - - """) - ijulia_initialized[] = true + if isijulia() + mathjax_head + else + "$mathjax_head" end - return "$mathjax_head" end function plotly_html_body(plt, style = nothing) @@ -914,12 +903,22 @@ function plotly_html_body(plt, style = nothing) w, h = plt[:size] style = "width:$(w)px;height:$(h)px;" end + + requirejs_prefix = "" + requirejs_suffix = "" + if isijulia() + requirejs_prefix = "require(['Plotly'], function (Plotly) {" + requirejs_suffix = "});" + end + uuid = UUIDs.uuid4() html = """
""" html diff --git a/src/init.jl b/src/init.jl index 22bb4b51..d340fda7 100644 --- a/src/init.jl +++ b/src/init.jl @@ -89,6 +89,23 @@ function __init__() use_local_dependencies[] = use_local_plotlyjs[] + if isijulia() + # require.js adds .js automatically + plotly_no_ext = + use_local_dependencies[] ? ("file:///" * plotly_local_file_path[]) : "https://cdn.plot.ly/$(_plotly_min_js_filename)" + plotly_no_ext = plotly_no_ext[1:end-3] + + display("text/html", """ + + """) + end + @require FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" begin _show(io::IO, mime::MIME"image/png", plt::Plot{<:PDFBackends}) = _show_pdfbackends(io, mime, plt) From 50a6dd9fb36a49d27441fa3ecbd10eead30edfc7 Mon Sep 17 00:00:00 2001 From: Jks Liu Date: Wed, 20 Jan 2021 22:51:17 +0800 Subject: [PATCH 4/4] let plotly config and use in same cell in case of config cell re-run or is deleted --- src/backends/plotly.jl | 14 +++++++++++++- src/init.jl | 17 ----------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index ed7a48d1..ce2b704c 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -907,7 +907,19 @@ function plotly_html_body(plt, style = nothing) requirejs_prefix = "" requirejs_suffix = "" if isijulia() - requirejs_prefix = "require(['Plotly'], function (Plotly) {" + # require.js adds .js automatically + plotly_no_ext = + use_local_dependencies[] ? ("file:///" * plotly_local_file_path[]) : "https://cdn.plot.ly/$(_plotly_min_js_filename)" + plotly_no_ext = plotly_no_ext[1:end-3] + + requirejs_prefix = """ + requirejs.config({ + paths: { + Plotly: '$(plotly_no_ext)' + } + }); + require(['Plotly'], function (Plotly) { + """ requirejs_suffix = "});" end diff --git a/src/init.jl b/src/init.jl index d340fda7..22bb4b51 100644 --- a/src/init.jl +++ b/src/init.jl @@ -89,23 +89,6 @@ function __init__() use_local_dependencies[] = use_local_plotlyjs[] - if isijulia() - # require.js adds .js automatically - plotly_no_ext = - use_local_dependencies[] ? ("file:///" * plotly_local_file_path[]) : "https://cdn.plot.ly/$(_plotly_min_js_filename)" - plotly_no_ext = plotly_no_ext[1:end-3] - - display("text/html", """ - - """) - end - @require FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" begin _show(io::IO, mime::MIME"image/png", plt::Plot{<:PDFBackends}) = _show_pdfbackends(io, mime, plt)