diff --git a/Project.toml b/Project.toml index c69284b..f1c35c1 100644 --- a/Project.toml +++ b/Project.toml @@ -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" @@ -41,6 +42,7 @@ Pkg = "1.9" PlotlyBase = "0.8" PlotlyKaleido = "2" Reexport = "1" +ScopedValues = "1.2.1" Scratch = "1" TOML = "1" Unitful = "1" 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/src/PlutoPlotly.jl b/src/PlutoPlotly.jl index abdd424..c84856b 100644 --- a/src/PlutoPlotly.jl +++ b/src/PlutoPlotly.jl @@ -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) diff --git a/src/basics.jl b/src/basics.jl index 9955a57..eb06a9f 100644 --- a/src/basics.jl +++ b/src/basics.jl @@ -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 @@ -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 ## """ diff --git a/src/preprocess.jl b/src/preprocess.jl index 1f9801a..821ab27 100644 --- a/src/preprocess.jl +++ b/src/preprocess.jl @@ -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 #= diff --git a/src/show.jl b/src/show.jl index 50c7fec..f3de169 100644 --- a/src/show.jl +++ b/src/show.jl @@ -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 """