From 4425a4a41d04d9de59533de0314bda2adfca0919 Mon Sep 17 00:00:00 2001 From: Alberto Mengali Date: Tue, 6 Aug 2024 17:03:15 +0200 Subject: [PATCH] add function to ensure kaleido plotly version is the same used by the library Can also be overidden with PLOTLY_VERSION --- ext/PlotlyKaleidoExt.jl | 33 ++++++++++++++++++++++++++++++--- test/extensions.jl | 20 +++++++++++++++----- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/ext/PlotlyKaleidoExt.jl b/ext/PlotlyKaleidoExt.jl index 59ecc0c..e926d58 100644 --- a/ext/PlotlyKaleidoExt.jl +++ b/ext/PlotlyKaleidoExt.jl @@ -1,8 +1,35 @@ module PlotlyKaleidoExt -using PlutoPlotly: PlutoPlot -using PlotlyKaleido: savefig, PlotlyKaleido +using PlutoPlotly: PlutoPlot, get_plotly_version +using PlotlyKaleido: savefig, PlotlyKaleido, restart, P, is_running -PlotlyKaleido.savefig(io::IO, p::PlutoPlot, args...; kwargs...) = savefig(io, p.Plot, args...; kwargs...) +function get_version_in_kaleido() + is_running() || return nothing + exec = P.proc.cmd.exec + flag_idx = findfirst(startswith("https://cdn.plot.ly"), exec) + isnothing(flag_idx) && return nothing + url = exec[flag_idx] + m = match(r"https://cdn.plot.ly/plotly-(\d+\.\d+\.\d+).min.js", url) + return first(m.captures) |> VersionNumber +end +function ensure_correct_version() + pkgversion(PlotlyKaleido) >= v"2.2.1" || return # If we can't change version, we just assume it's correct + current_version = get_plotly_version() + # We find the flags in the cmd used to start kaleido, if we have a specified + # version, that appears as a url of the corresponding version on the plotly + # CDN, see https://github.com/JuliaPlots/PlotlyKaleido.jl/pull/9 for more + # details + kaleido_plotly_version = get_version_in_kaleido() + if isnothing(kaleido_plotly_version) || kaleido_plotly_version != current_version + @info "(Re)Starting the kaleido process with plotly version $current_version" + restart(; plotly_version = current_version) # Process it not active, we simply start it with the correct version + return + end +end + +function PlotlyKaleido.savefig(io::IO, p::PlutoPlot, args...; kwargs...) + ensure_correct_version() + savefig(io, p.Plot, args...; kwargs...) +end end \ No newline at end of file diff --git a/test/extensions.jl b/test/extensions.jl index b7e921a..b52d723 100644 --- a/test/extensions.jl +++ b/test/extensions.jl @@ -1,5 +1,7 @@ using PlutoPlotly +using PlutoPlotly: PLOTLY_VERSION using Test +using ScopedValues ## PlotlyKaleido Extension ## using PlotlyKaleido @@ -7,12 +9,20 @@ if Sys.islinux() # We only test this in linux as the library fail in CI on Mac OS and Windows PlotlyKaleido.start() - mktempdir() do dir - cd() do - p = plot(rand(10,4)) - @test_nowarn savefig(p, "test_savefig.png") - @test isfile("test_savefig.png") + try + mktempdir() do dir + cd() do + p = plot(rand(10,4)) + @test_logs (:info, r"with plotly version 2.34.0") savefig(p, "test_savefig.png") + @test isfile("test_savefig.png") + @test_logs (:info, r"with plotly version 2.33.0") with(PLOTLY_VERSION => "2.33") do + savefig(p, "test_changeversion.png") + end + @test isfile("test_changeversion.png") + end end + finally + PlotlyKaleido.kill_kaleido() end end