Skip to content

Commit

Permalink
substitute old savefig code with PlotlyKaleido (#481)
Browse files Browse the repository at this point in the history
* substitute old code with PlotlyKaleido

* fail slow

* dont spawn task

* neither wait

* invert logic

* bump version [skip ci]
  • Loading branch information
BeastyBlacksmith authored Feb 23, 2024
1 parent b324e55 commit 75eacf4
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 84 deletions.
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 Down Expand Up @@ -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"
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

2 comments on commit 75eacf4

@BeastyBlacksmith
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/101538

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.18.13 -m "<description of version>" 75eacf4542dc12d7b05242b550f250e6039929bf
git push origin v0.18.13

Please sign in to comment.