Skip to content

Commit 62a4cd2

Browse files
authored
Merge pull request #18 from disberd/move_errors_savefig
2 parents d3b685a + 5a0c00f commit 62a4cd2

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/PlotlyKaleido.jl

+27-10
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@ const P = Pipes()
2020
const _mathjax_url_path = "https://cdnjs.cloudflare.com/ajax/libs/mathjax"
2121
const _mathjax_last_version = v"2.7.9"
2222

23+
function warn_and_kill(s::String)
24+
@warn "$s"
25+
kill_kaleido()
26+
return nothing
27+
end
28+
2329
kill_kaleido() = is_running() && (kill(P.proc); wait(P.proc))
2430

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

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

2935
# 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
30-
function readline_noblock(io)
36+
function readline_noblock(io; timeout = 10)
3137
msg = Channel{String}(1)
3238

3339
task = Task() do
@@ -39,7 +45,7 @@ function readline_noblock(io)
3945
end
4046

4147
interrupter = Task() do
42-
sleep(5)
48+
sleep(timeout)
4349
if !istaskdone(task)
4450
Base.throwto(task, InterruptException())
4551
end
@@ -48,18 +54,24 @@ function readline_noblock(io)
4854
schedule(interrupter)
4955
schedule(task)
5056
wait(task)
57+
kaleido_version = read(joinpath(Kaleido_jll.artifact_dir, "version"), String)
5158
out = take!(msg)
52-
out === "Stopped" && error("It looks like the kaleido process is hanging.
53-
If you are on windows this might be caused by known problems with Kaleido v0.2 on windows.
54-
You might want to try forcing a downgrade of the kaleido library to 0.1.
55-
Check the Package Readme at https://github.com/JuliaPlots/PlotlyKaleido.jl/tree/main#windows-note for more details")
59+
out === "Stopped" && warn_and_kill("It looks like the Kaleido process is not responding.
60+
The unresponsive process will be killed, but this means that you will not be able to save figures using `savefig`.
61+
62+
If you are on Windows this might be caused by known problems with Kaleido v0.2 on Windows (you are using version $(kaleido_version)).
63+
You might want to try forcing a downgrade of the Kaleido_jll library to 0.1.
64+
Check the Package Readme at https://github.com/JuliaPlots/PlotlyKaleido.jl/tree/main#windows-note for more details.
65+
66+
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`")
5667
return out
5768
end
5869

5970
function start(;
6071
plotly_version = missing,
6172
mathjax = missing,
6273
mathjax_version::VersionNumber = _mathjax_last_version,
74+
timeout = 10,
6375
kwargs...,
6476
)
6577
is_running() && return
@@ -125,10 +137,12 @@ function start(;
125137
P.stderr = kstderr
126138
P.proc = kproc
127139

128-
res = readline_noblock(P.stdout) # {"code": 0, "message": "Success", "result": null, "version": "0.2.1"}
129-
length(res) == 0 && error("Kaleido startup failed.")
130-
code = JSON.parse(res)["code"]
131-
code == 0 || error("Kaleido startup failed with code $code.")
140+
res = readline_noblock(P.stdout; timeout) # {"code": 0, "message": "Success", "result": null, "version": "0.2.1"}
141+
length(res) == 0 && warn_and_kill("Kaleido startup failed.")
142+
if is_running()
143+
code = JSON.parse(res)["code"]
144+
code == 0 || warn_and_kill("Kaleido startup failed with code $code.")
145+
end
132146
return
133147
end
134148

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

140154

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

144161
bytes = transcode(UInt8, payload)

0 commit comments

Comments
 (0)