Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use artifacts instead of PlotlyJS #4884

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Artifacts.toml
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 2 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,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"
Expand Down Expand Up @@ -75,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"
PyPlot = "2"
Expand All @@ -85,7 +85,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"
Expand Down
4 changes: 1 addition & 3 deletions ext/IJuliaExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/backends/plotly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/backends/web.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 14 additions & 6 deletions src/init.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
using Scratch
using REPL
import Base64
pankgeorg marked this conversation as resolved.
Show resolved Hide resolved

const _plotly_local_file_path = Ref{Union{Nothing,String}}(nothing)
# 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")

Check warning on line 9 in src/init.jl

View workflow job for this annotation

GitHub Actions / check

[JuliaFormatter] reported by reviewdog 🐶 Raw Output: src/init.jl:9:-const _plotly_local_file_path = joinpath(artifact("plotly-dist"), "plotly.js-2.6.3", "dist", "plotly.min.js") src/init.jl:9:+const _plotly_local_file_path = src/init.jl:10:+ 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-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[])

Check warning on line 18 in src/init.jl

View workflow job for this annotation

GitHub Actions / check

[JuliaFormatter] reported by reviewdog 🐶 Raw Output: src/init.jl:18:-_plotly_data_url() = if isnothing(_plotly_data_url_cached[]) src/init.jl:19:- _plotly_data_url_cached[] = "data:text/javascript;base64,$(Base64.base64encode(read(_plotly_local_file_path)))" src/init.jl:20:-else src/init.jl:21:- _plotly_data_url_cached[] src/init.jl:22:-end src/init.jl:19:+_plotly_data_url() = src/init.jl:20:+ if isnothing(_plotly_data_url_cached[]) src/init.jl:21:+ _plotly_data_url_cached[] = "data:text/javascript;base64,$(Base64.base64encode(read(_plotly_local_file_path)))" src/init.jl:22:+ else src/init.jl:23:+ _plotly_data_url_cached[] src/init.jl:24:+ end
_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)
Expand All @@ -23,10 +35,6 @@

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[]
Expand Down
5 changes: 1 addition & 4 deletions test/test_misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading