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

Make all errors in start warnings, actually throw when calling save_payload #18

Merged
merged 8 commits into from
Mar 5, 2024
Merged
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
37 changes: 27 additions & 10 deletions src/PlotlyKaleido.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ const P = Pipes()
const _mathjax_url_path = "https://cdnjs.cloudflare.com/ajax/libs/mathjax"
const _mathjax_last_version = v"2.7.9"

function warn_and_kill(s::String)
@warn "$s"
kill_kaleido()
return nothing
end

kill_kaleido() = is_running() && (kill(P.proc); wait(P.proc))

is_running() = isdefined(P, :proc) && isopen(P.stdin) && process_running(P.proc)

restart(; kwargs...) = (kill_kaleido(); start(; kwargs...))

# The content of this function is inspired from https://discourse.julialang.org/t/readline-with-default-value-if-no-input-after-timeout/100388/2?u=disberd
function readline_noblock(io)
function readline_noblock(io; timeout = 10)
msg = Channel{String}(1)

task = Task() do
Expand All @@ -39,7 +45,7 @@ function readline_noblock(io)
end

interrupter = Task() do
sleep(5)
sleep(timeout)
if !istaskdone(task)
Base.throwto(task, InterruptException())
end
Expand All @@ -48,18 +54,24 @@ function readline_noblock(io)
schedule(interrupter)
schedule(task)
wait(task)
kaleido_version = read(joinpath(Kaleido_jll.artifact_dir, "version"), String)
out = take!(msg)
out === "Stopped" && error("It looks like the kaleido process is hanging.
If you are on windows this might be caused by known problems with Kaleido v0.2 on windows.
You might want to try forcing a downgrade of the kaleido library to 0.1.
Check the Package Readme at https://github.com/JuliaPlots/PlotlyKaleido.jl/tree/main#windows-note for more details")
out === "Stopped" && warn_and_kill("It looks like the Kaleido process is not responding.
The unresponsive process will be killed, but this means that you will not be able to save figures using `savefig`.

If you are on Windows this might be caused by known problems with Kaleido v0.2 on Windows (you are using version $(kaleido_version)).
You might want to try forcing a downgrade of the Kaleido_jll library to 0.1.
Check the Package Readme at https://github.com/JuliaPlots/PlotlyKaleido.jl/tree/main#windows-note for more details.

If you think this is not your case, you might try using a longer timeout to check if the process is not responding (defaults to 10 seconds) by passing the desired value in seconds using the `timeout` kwarg when calling `PlotlyKaleido.start` or `PlotlyKaleido.restart`")
return out
end

function start(;
plotly_version = missing,
mathjax = missing,
mathjax_version::VersionNumber = _mathjax_last_version,
timeout = 10,
kwargs...,
)
is_running() && return
Expand Down Expand Up @@ -125,10 +137,12 @@ function start(;
P.stderr = kstderr
P.proc = kproc

res = readline_noblock(P.stdout) # {"code": 0, "message": "Success", "result": null, "version": "0.2.1"}
length(res) == 0 && error("Kaleido startup failed.")
code = JSON.parse(res)["code"]
code == 0 || error("Kaleido startup failed with code $code.")
res = readline_noblock(P.stdout; timeout) # {"code": 0, "message": "Success", "result": null, "version": "0.2.1"}
length(res) == 0 && warn_and_kill("Kaleido startup failed.")
if is_running()
code = JSON.parse(res)["code"]
code == 0 || warn_and_kill("Kaleido startup failed with code $code.")
end
return
end

Expand All @@ -139,6 +153,9 @@ const TEXT_FORMATS = ["svg", "json", "eps"]


function save_payload(io::IO, payload::AbstractString, format::AbstractString)
is_running() || error("It looks like the Kaleido process is not running, so you can not save plotly figures.
Remember to start the process before using `savefig` by calling `PlotlyKaleido.start()`.
If the process was killed due to an error during initialization, you will receive a warning when the `PlotlyKaleido.start` function is executing")
format in ALL_FORMATS || error("Unknown format $format. Expected one of $ALL_FORMATS")

bytes = transcode(UInt8, payload)
Expand Down
Loading