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

substitute old savefig code with PlotlyKaleido #481

Merged
merged 6 commits into from
Feb 23, 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
1 change: 1 addition & 0 deletions .github/workflows/ci-master-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
timeout-minutes: 30
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
julia-version:
- '1.6'
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci-pr-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
timeout-minutes: 30
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
julia-version:
- '1.6'
Expand Down
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PlotlyJS"
uuid = "f0f68f2c-4968-5e81-91da-67840de0976a"
authors = ["Spencer Lyon <[email protected]>"]
version = "0.18.12"
version = "0.18.13"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand All @@ -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"
Expand All @@ -39,6 +40,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"
Expand Down
16 changes: 5 additions & 11 deletions src/PlotlyJS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
75 changes: 3 additions & 72 deletions src/kaleido.jl
Original file line number Diff line number Diff line change
@@ -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...)

Expand All @@ -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)
Expand Down Expand Up @@ -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",
Expand Down