From 75eacf4542dc12d7b05242b550f250e6039929bf Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Fri, 23 Feb 2024 14:50:53 +0100 Subject: [PATCH] substitute old savefig code with PlotlyKaleido (#481) * substitute old code with PlotlyKaleido * fail slow * dont spawn task * neither wait * invert logic * bump version [skip ci] --- .github/workflows/ci-master-workflow.yml | 1 + .github/workflows/ci-pr-workflow.yml | 1 + Project.toml | 4 +- src/PlotlyJS.jl | 16 ++--- src/kaleido.jl | 75 +----------------------- 5 files changed, 13 insertions(+), 84 deletions(-) diff --git a/.github/workflows/ci-master-workflow.yml b/.github/workflows/ci-master-workflow.yml index 90a010e3..eff4d1c2 100644 --- a/.github/workflows/ci-master-workflow.yml +++ b/.github/workflows/ci-master-workflow.yml @@ -11,6 +11,7 @@ jobs: timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: + fail-fast: false matrix: julia-version: - '1.6' diff --git a/.github/workflows/ci-pr-workflow.yml b/.github/workflows/ci-pr-workflow.yml index 777ff8e6..dcf2cec7 100644 --- a/.github/workflows/ci-pr-workflow.yml +++ b/.github/workflows/ci-pr-workflow.yml @@ -10,6 +10,7 @@ jobs: timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: + fail-fast: false matrix: julia-version: - '1.6' diff --git a/Project.toml b/Project.toml index a477e0b0..e74e8cc2 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "PlotlyJS" uuid = "f0f68f2c-4968-5e81-91da-67840de0976a" authors = ["Spencer Lyon "] -version = "0.18.12" +version = "0.18.13" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" @@ -13,6 +13,7 @@ Kaleido_jll = "f7e6163d-2fa5-5f23-b69c-1db539e41963" Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" PlotlyBase = "a03496cd-edff-5a9b-9e67-9cda94a718b5" +PlotlyKaleido = "f2990250-8cf9-495f-b13a-cce12b45703c" REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" Requires = "ae029012-a4dd-5104-9daa-d747884805df" @@ -40,6 +41,7 @@ JSExpr = "0.5, 1" JSON = "0.20, 0.21" JSON3 = "1" PlotlyBase = "0.8.15" +PlotlyKaleido = "2" Reexport = "0.2, 1" Requires = "1.0" WebIO = "0.8" diff --git a/src/PlotlyJS.jl b/src/PlotlyJS.jl index 0d3b2521..3f60e314 100644 --- a/src/PlotlyJS.jl +++ b/src/PlotlyJS.jl @@ -3,6 +3,7 @@ module PlotlyJS using Base64 using Reexport @reexport using PlotlyBase +using PlotlyKaleido: PlotlyKaleido using JSON using REPL, Pkg, Pkg.Artifacts, DelimitedFiles # stdlib @@ -98,7 +99,10 @@ function __init__() @warn("Warnings were generated during the last build of PlotlyJS: please check the build log at $_build_log") end - kaleido_task = Base.Threads.@spawn _start_kaleido_process() + if ccall(:jl_generating_output, Cint, ()) != 1 + # ensure precompilation of packages depending on PlotlyJS finishes + PlotlyKaleido.start() + end if !isfile(_js_path) @info("plotly.js javascript libary not found -- downloading now") @@ -144,16 +148,6 @@ function __init__() end end end - - wait(kaleido_task) - - if ccall(:jl_generating_output, Cint, ()) == 1 - # ensure precompilation of packages depending on PlotlyJS finishes - if isdefined(P, :proc) - close(P.stdin) - wait(P.proc) - end - end end # for methods that update the layout, first apply to the plot, then let plotly.js diff --git a/src/kaleido.jl b/src/kaleido.jl index c01900bd..67fc2888 100644 --- a/src/kaleido.jl +++ b/src/kaleido.jl @@ -1,72 +1,4 @@ -using Kaleido_jll - -mutable struct Pipes - stdin::Pipe - stdout::Pipe - stderr::Pipe - proc::Base.Process - Pipes() = new() -end - -const P = Pipes() - -const ALL_FORMATS = ["png", "jpeg", "webp", "svg", "pdf", "eps", "json"] -const TEXT_FORMATS = ["svg", "json", "eps"] - -function _restart_kaleido_process() - if isdefined(P, :proc) && process_running(P.proc) - kill(P.proc) - end - _start_kaleido_process() -end - - -function _start_kaleido_process() - global P - try - BIN = let - art = Kaleido_jll.artifact_dir - cmd = if Sys.islinux() || Sys.isapple() - joinpath(art, "kaleido") - else - # Windows - joinpath(art, "kaleido.cmd") - end - no_sandbox = "--no-sandbox" - Sys.isapple() ? `$(cmd) plotly --disable-gpu --single-process` : `$(cmd) plotly --disable-gpu $(no_sandbox)` - end - kstdin = Pipe() - kstdout = Pipe() - kstderr = Pipe() - kproc = run(pipeline(BIN, - stdin=kstdin, stdout=kstdout, stderr=kstderr), - wait=false) - process_running(kproc) || error("There was a problem startink up kaleido.") - close(kstdout.in) - close(kstderr.in) - close(kstdin.out) - Base.start_reading(kstderr.out) - P.stdin = kstdin - P.stdout = kstdout - P.stderr = kstderr - P.proc = kproc - - # read startup message and check for errors - res = readline(P.stdout) - if length(res) == 0 - error("Could not start Kaleido process") - end - - js = JSON.parse(res) - if get(js, "code", 0) != 0 - error("Could not start Kaleido process") - end - catch e - @warn "Kaleido is not available on this system. Julia will be unable to save images of any plots." - @warn "$e" - end - nothing -end +using PlotlyKaleido: kill, is_running, start, restart, ALL_FORMATS, TEXT_FORMATS savefig(p::SyncPlot; kwargs...) = savefig(p.plot; kwargs...) @@ -92,7 +24,7 @@ function savefig( ) _ensure_kaleido_running() - + P = PlotlyKaleido.P # convert payload to vector of bytes bytes = transcode(UInt8, JSON.json(payload)) write(P.stdin, bytes) @@ -189,8 +121,7 @@ function savefig( return fn end -_kaleido_running() = isdefined(P, :stdin) && isopen(P.stdin) && process_running(P.proc) -_ensure_kaleido_running() = !_kaleido_running() && _restart_kaleido_process() +_ensure_kaleido_running() = !is_running() && restart() const _KALEIDO_MIMES = Dict( "application/pdf" => "pdf",