From 01561c252bbfa142f8535c4d1a2309cf8d81f861 Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Fri, 5 Jan 2024 11:23:00 +0100 Subject: [PATCH 1/4] Get the plotly.min.js file from PlotlyJS.jl --- Project.toml | 4 +--- ext/IJuliaExt.jl | 4 +--- src/backends/plotly.jl | 2 +- src/backends/web.jl | 3 +-- src/init.jl | 10 +++------- test/test_misc.jl | 5 +---- 6 files changed, 8 insertions(+), 20 deletions(-) diff --git a/Project.toml b/Project.toml index b73284b1d..29825a4da 100644 --- a/Project.toml +++ b/Project.toml @@ -31,7 +31,6 @@ RecipesPipeline = "01d81517-befc-4cb6-b9ec-a95719d0359c" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" RelocatableFolders = "05181044-ff0b-4ac5-8273-598c1e38db00" Requires = "ae029012-a4dd-5104-9daa-d747884805df" -Scratch = "6c6a2e73-6563-6170-7368-637461726353" Showoff = "992d4aef-0814-514b-bc4d-f2e9a6c4116f" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" @@ -76,7 +75,7 @@ PGFPlotsX = "1" PlotThemes = "2, 3" PlotUtils = "1" PlotlyBase = "0.7 - 0.8" -PlotlyJS = "0.18" +PlotlyJS = "0.18.12" PlotlyKaleido = "1" PrecompileTools = "1" Preferences = "1" @@ -87,7 +86,6 @@ RecipesPipeline = "0.6.10" Reexport = "0.2, 1" RelocatableFolders = "0.3, 1" Requires = "1" -Scratch = "1" Showoff = "0.3.1, 1" Statistics = "1" StatsBase = "0.33, 0.34" diff --git a/ext/IJuliaExt.jl b/ext/IJuliaExt.jl index 5322c155b..0317de7f5 100644 --- a/ext/IJuliaExt.jl +++ b/ext/IJuliaExt.jl @@ -8,9 +8,7 @@ const IJulia = function _init_ijulia_plotting() # IJulia is more stable with local file - Plots._use_local_plotlyjs[] = - Plots._plotly_local_file_path[] === nothing ? false : - isfile(Plots._plotly_local_file_path[]) + Plots._use_local_plotlyjs[] = true ENV["MPLBACKEND"] = "Agg" end diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 1c251c639..4f0be045e 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -1055,7 +1055,7 @@ html_body(plt::Plot{PlotlyBackend}) = plotly_html_body(plt) plotly_url() = if _use_local_dependencies[] - "file:///" * _plotly_local_file_path[] + "file:///" * _plotly_local_file_path else "https://cdn.plot.ly/$_plotly_min_js_filename" end diff --git a/src/backends/web.jl b/src/backends/web.jl index cfa7e454c..7e912dc64 100644 --- a/src/backends/web.jl +++ b/src/backends/web.jl @@ -45,8 +45,7 @@ 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[] = - _plotly_local_file_path[] === nothing ? false : isfile(_plotly_local_file_path[]) + _use_local_dependencies[] = true filename = write_temp_html(plt) open_browser_window(filename) # restore for other backends diff --git a/src/init.jl b/src/init.jl index aa58c2bb9..a74c8f817 100644 --- a/src/init.jl +++ b/src/init.jl @@ -1,10 +1,10 @@ -using Scratch using REPL +import PlotlyJS -const _plotly_local_file_path = Ref{Union{Nothing,String}}(nothing) +const _plotly_local_file_path = PlotlyJS._js_path # use fixed version of Plotly instead of the latest one for stable dependency # see github.com/JuliaPlots/Plots.jl/pull/2779 -const _plotly_min_js_filename = "plotly-2.6.3.min.js" +const _plotly_min_js_filename = "plotly-$(PlotlyJS._js_version).min.js" const _use_local_dependencies = Ref(false) const _use_local_plotlyjs = Ref(false) @@ -23,10 +23,6 @@ end function _plots_plotly_defaults() if bool_env("PLOTS_HOST_DEPENDENCY_LOCAL", "false") - _plotly_local_file_path[] = - fn = joinpath(@get_scratch!("plotly"), _plotly_min_js_filename) - isfile(fn) || - Downloads.download("https://cdn.plot.ly/$(_plotly_min_js_filename)", fn) _use_local_plotlyjs[] = true end _use_local_dependencies[] = _use_local_plotlyjs[] diff --git a/test/test_misc.jl b/test/test_misc.jl index 9559b4fc7..eaa6ddf00 100644 --- a/test/test_misc.jl +++ b/test/test_misc.jl @@ -7,15 +7,12 @@ end @testset "Plotly standalone" begin - @test Plots._plotly_local_file_path[] ≡ nothing temp = Plots._use_local_dependencies[] withenv("PLOTS_HOST_DEPENDENCY_LOCAL" => true) do Plots._plots_plotly_defaults() - @test Plots._plotly_local_file_path[] isa String - @test isfile(Plots._plotly_local_file_path[]) + @test isfile(Plots._plotly_local_file_path) @test Plots._use_local_dependencies[] = true end - Plots._plotly_local_file_path[] = nothing Plots._use_local_dependencies[] = temp end From 4df9110f49881c94a22af9a1afb83d8e48717dd9 Mon Sep 17 00:00:00 2001 From: Panagiotis Georgakopoulos Date: Fri, 2 Feb 2024 19:14:29 +0200 Subject: [PATCH 2/4] feat: use artifacts instead of PlotlyJS --- Artifacts.toml | 6 ++++++ Project.toml | 1 + src/backends/plotly.jl | 2 +- src/init.jl | 10 +++++++--- 4 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 Artifacts.toml diff --git a/Artifacts.toml b/Artifacts.toml new file mode 100644 index 000000000..bd3da5966 --- /dev/null +++ b/Artifacts.toml @@ -0,0 +1,6 @@ +[plotly-dist] +git-tree-sha1 = "3039420613779f4cd3620456d5c7f0849e419e3d" + + [[plotly-dist.download]] + sha256 = "20c27644d7791d01058cc3a61c1cac351e313931994d464dc1f7796703d32312" + url = "https://github.com/plotly/plotly.js/archive/refs/tags/v2.6.3.tar.gz" diff --git a/Project.toml b/Project.toml index 920644df1..51e118193 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ author = ["Tom Breloff (@tbreloff)"] version = "1.39.0-dev" [deps] +Artifacts = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" Contour = "d38c429a-6771-53c6-b99e-75d170b6e991" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 4f0be045e..ebe133c39 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -1055,7 +1055,7 @@ html_body(plt::Plot{PlotlyBackend}) = plotly_html_body(plt) plotly_url() = if _use_local_dependencies[] - "file:///" * _plotly_local_file_path + "file:///" * joinpath(artifact("plotly-dist"), "plotly.js-2.6.3", "dist", "plotly.min.js") else "https://cdn.plot.ly/$_plotly_min_js_filename" end diff --git a/src/init.jl b/src/init.jl index 35893e29c..acf217f0e 100644 --- a/src/init.jl +++ b/src/init.jl @@ -1,10 +1,14 @@ using REPL -import PlotlyJS -const _plotly_local_file_path = PlotlyJS._js_path +# Local uses artifacts now. To update this, also update Artifacts.toml. +# using ArtifactUtils +# add_artifact!("Artifacts.toml", "plotly-dist", "https://github.com/plotly/plotly.js/archive/refs/tags/v2.6.3.tar.gz") +# # update with your desired version +const _plotly_version = "2.6.3" +const _plotly_local_file_path = joinpath(artifact("plotly-dist"), "plotly.js-2.6.3", "dist", "plotly.min.js") # use fixed version of Plotly instead of the latest one for stable dependency # see github.com/JuliaPlots/Plots.jl/pull/2779 -const _plotly_min_js_filename = "plotly-$(PlotlyJS._js_version).min.js" +const _plotly_min_js_filename = "plotly-2.6.3.min.js" const _use_local_dependencies = Ref(false) const _use_local_plotlyjs = Ref(false) From bfcc89186b4c02ed364172efcb4f920c91b0e31c Mon Sep 17 00:00:00 2001 From: Panagiotis Georgakopoulos Date: Fri, 2 Feb 2024 19:54:42 +0200 Subject: [PATCH 3/4] feat: base64 instead of file:/// --- src/backends/plotly.jl | 2 +- src/init.jl | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index ebe133c39..12f81ad60 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -1055,7 +1055,7 @@ html_body(plt::Plot{PlotlyBackend}) = plotly_html_body(plt) plotly_url() = if _use_local_dependencies[] - "file:///" * joinpath(artifact("plotly-dist"), "plotly.js-2.6.3", "dist", "plotly.min.js") + _plotly_data_url() else "https://cdn.plot.ly/$_plotly_min_js_filename" end diff --git a/src/init.jl b/src/init.jl index acf217f0e..8dcec166e 100644 --- a/src/init.jl +++ b/src/init.jl @@ -1,4 +1,5 @@ using REPL +import Base64 # Local uses artifacts now. To update this, also update Artifacts.toml. # using ArtifactUtils @@ -12,6 +13,13 @@ const _plotly_min_js_filename = "plotly-2.6.3.min.js" const _use_local_dependencies = Ref(false) const _use_local_plotlyjs = Ref(false) +const _plotly_data_url_cached = Ref{Union{Nothing,String}}(nothing) + +_plotly_data_url() = if isnothing(_plotly_data_url_cached[]) + _plotly_data_url_cached[] = "data:text/javascript;base64,$(Base64.base64encode(read(_plotly_local_file_path)))" +else + _plotly_data_url_cached[] +end _plots_defaults() = if isdefined(Main, :PLOTS_DEFAULTS) From aa9e0c09bedc030067fc3a37ab64c9b2f5ff3df7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A0=CE=B1=CE=BD=CE=B1=CE=B3=CE=B9=CF=8E=CF=84=CE=B7?= =?UTF-8?q?=CF=82=20=CE=93=CE=B5=CF=89=CF=81=CE=B3=CE=B1=CE=BA=CF=8C=CF=80?= =?UTF-8?q?=CE=BF=CF=85=CE=BB=CE=BF=CF=82?= Date: Mon, 5 Feb 2024 15:55:02 +0200 Subject: [PATCH 4/4] fix: correct Artifacts import Co-authored-by: Steve Kelly --- src/init.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/init.jl b/src/init.jl index 8dcec166e..80b5ad1b4 100644 --- a/src/init.jl +++ b/src/init.jl @@ -1,5 +1,6 @@ using REPL import Base64 +using Artifacts # Local uses artifacts now. To update this, also update Artifacts.toml. # using ArtifactUtils