Skip to content

Commit

Permalink
add function to ensure kaleido plotly version is the same used by the…
Browse files Browse the repository at this point in the history
… library

Can also be overidden with PLOTLY_VERSION
  • Loading branch information
disberd committed Aug 6, 2024
1 parent cac9291 commit 4425a4a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
33 changes: 30 additions & 3 deletions ext/PlotlyKaleidoExt.jl
Original file line number Diff line number Diff line change
@@ -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
20 changes: 15 additions & 5 deletions test/extensions.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
using PlutoPlotly
using PlutoPlotly: PLOTLY_VERSION
using Test
using ScopedValues

## PlotlyKaleido Extension ##
using PlotlyKaleido
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

Expand Down

0 comments on commit 4425a4a

Please sign in to comment.