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

Use ScopedValues for flags to set #54

Merged
merged 7 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
PlotlyBase = "a03496cd-edff-5a9b-9e67-9cda94a718b5"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
ScopedValues = "7e506255-f358-4e82-b7e4-beb19740aa63"
Scratch = "6c6a2e73-6563-6170-7368-637461726353"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

Expand All @@ -41,6 +42,7 @@ Pkg = "1.9"
PlotlyBase = "0.8"
PlotlyKaleido = "2"
Reexport = "1"
ScopedValues = "1.2.1"
Scratch = "1"
TOML = "1"
Unitful = "1"
Expand Down
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
1 change: 1 addition & 0 deletions src/PlutoPlotly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using LaTeXStrings
using Markdown
using Downloads: download
using Artifacts
using ScopedValues
# This is similar to `@reexport` but does not exports undefined names and can
# also avoid exporting the module name
function re_export(m::Module; skip_modname = false)
Expand Down
10 changes: 7 additions & 3 deletions src/basics.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const ARTIFACT_VERSION = VersionNumber(read(joinpath(artifact"plotly-esm-min", "VERSION"), String))
const PLOTLY_VERSION = Ref(ARTIFACT_VERSION)
const DEFAULT_PLOTLY_VERSION = Ref(ARTIFACT_VERSION)
const PLOTLY_VERSION = ScopedValue{Union{Nothing, String, VersionNumber}}(nothing)
const DEFAULT_TEMPLATE = Ref(PlotlyBase.templates[PlotlyBase.templates.default])
const JS = HypertextLiteral.JavaScript

Expand Down Expand Up @@ -57,10 +58,13 @@ end
function change_plotly_version(v)
ver = VersionNumber(v)
maybe_add_plotly_local(ver)
PLOTLY_VERSION[] = ver
DEFAULT_PLOTLY_VERSION[] = ver
end

get_plotly_version() = PLOTLY_VERSION[]
function get_plotly_version()
v = @something PLOTLY_VERSION[] DEFAULT_PLOTLY_VERSION[]
return VersionNumber(v)
end

## Prepend Cell Selector ##
"""
Expand Down
7 changes: 2 additions & 5 deletions src/preprocess.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const SKIP_FLOAT32 = Ref(false)
const SKIP_FLOAT32 = ScopedValue(false)
skip_float32(f) = let
SKIP_FLOAT32[] = true
out = f()
SKIP_FLOAT32[] = false
out
with(f, SKIP_FLOAT32 => true)
end

#=
Expand Down
2 changes: 1 addition & 1 deletion src/show.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function _show(pp::PlutoPlot; script_id = "pluto-plotly-div", ver = PLOTLY_VERSION[])
function _show(pp::PlutoPlot; script_id = "pluto-plotly-div", ver = get_plotly_version())
@htl """
<script id=$(script_id)>
// We start by putting all the variable interpolation here at the beginning
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Kaleido_jll = "f7e6163d-2fa5-5f23-b69c-1db539e41963"
PlotlyKaleido = "f2990250-8cf9-495f-b13a-cce12b45703c"
Pluto = "c3e4b0f8-55cb-11ea-2926-15256bba5781"
ScopedValues = "7e506255-f358-4e82-b7e4-beb19740aa63"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
34 changes: 25 additions & 9 deletions test/basic_coverage.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Test
using PlutoPlotly
using PlutoPlotly: _preprocess, SKIP_FLOAT32, skip_float32, ARTIFACT_VERSION
using PlutoPlotly: _preprocess, SKIP_FLOAT32, skip_float32, ARTIFACT_VERSION, PLOTLY_VERSION
using PlutoPlotly.PlotlyBase: ColorScheme, Colors, Cycler, templates
using ScopedValues

@test SKIP_FLOAT32[] == false
@test skip_float32() do
Expand All @@ -10,8 +11,12 @@ end == true
@test SKIP_FLOAT32[] == false

@test force_pluto_mathjax_local() === false
force_pluto_mathjax_local(true)
@test force_pluto_mathjax_local() === true
try
force_pluto_mathjax_local(true)
@test force_pluto_mathjax_local() === true
finally
force_pluto_mathjax_local(false)
end

@test ColorScheme([Colors.RGB(0.0, 0.0, 0.0), Colors.RGB(1.0, 1.0, 1.0)],
"custom", "twotone, black and white") |> _preprocess == [(0.0, "rgb(0,0,0)"), (1.0, "rgb(255,255,255)")]
Expand All @@ -24,10 +29,13 @@ force_pluto_mathjax_local(true)

# Check that plotly is the default
@test default_plotly_template() == templates[templates.default]
@test default_plotly_template(:none) == Template()
@test default_plotly_template("seaborn") == templates[:seaborn]
@test_logs (:info, "The default plotly template is seaborn") default_plotly_template(;find_matching = true)

try
@test default_plotly_template(:none) == Template()
@test default_plotly_template("seaborn") == templates[:seaborn]
@test_logs (:info, "The default plotly template is seaborn") default_plotly_template(;find_matching = true)
finally
default_plotly_template(templates[templates.default])
end
let p = plot(rand(4))
@test get_image_options(p) == Dict{Symbol,Any}()
change_image_options!(p; height = 400)
Expand All @@ -38,5 +46,13 @@ end
@test plutoplotly_paste_receiver() isa PlutoPlotly.HypertextLiteral.Result

@test get_plotly_version() === ARTIFACT_VERSION
@test change_plotly_version("2.30") === VersionNumber("2.30.0")
@test get_plotly_version() === VersionNumber("2.30.0")
try
@test change_plotly_version("2.30") === VersionNumber("2.30.0")
@test get_plotly_version() === VersionNumber("2.30.0")
@test VersionNumber("2.33.0") === with(PLOTLY_VERSION => "2.33") do
get_plotly_version()
end
finally
# We put back the default version to be the ARTIFACT one. This is to avoid errors while repeating multiple times tests locally
change_plotly_version(ARTIFACT_VERSION)
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
Loading