From 40281d0f1e3b4e4d712265ebb0bafb60af9436b2 Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Mon, 5 Feb 2024 11:04:25 +0100 Subject: [PATCH] Use data URL for local plotly asset (#4863) * Use data URL for local plotly asset * formatting * fix: reviewdog suggestion --------- Co-authored-by: Panagiotis Georgakopoulos --- 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 1c251c639..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:///" * _plotly_local_file_path[] + _plotly_data_url() else "https://cdn.plot.ly/$_plotly_min_js_filename" end diff --git a/src/init.jl b/src/init.jl index b962a843f..b7738e3f9 100644 --- a/src/init.jl +++ b/src/init.jl @@ -1,7 +1,15 @@ using Scratch using REPL +import Base64 const _plotly_local_file_path = Ref{Union{Nothing,String}}(nothing) +const _plotly_data_url_cached = Ref{Union{Nothing,String}}(nothing) +_plotly_data_url() = + if _plotly_data_url_cached[] === nothing + _plotly_data_url_cached[] = "data:text/javascript;base64,$(Base64.base64encode(read(_plotly_local_file_path)))" + else + _plotly_data_url_cached[] + end # 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"