Skip to content

Commit 8a36dab

Browse files
authored
Call the kaleido binary directly (fixes permission errors on Windows and Julia 1.10) (#17)
* point to kaleido.exe directly on windows * try alternative * remove hanging stuff * remove `@test_nowarn` on start() * put bck test_nowarn on non-windows * warn about windows problem on README * testnowarn error * revert test_nowarn error * Throw an error with a suggestion fix if kaleido seems to hang * fix noblock function as bytesavailable does not work here
1 parent 592af20 commit 8a36dab

File tree

4 files changed

+68
-10
lines changed

4 files changed

+68
-10
lines changed

Project.toml

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ julia = "1.6"
1414
Kaleido_jll = "0.1, 0.2"
1515

1616
[extras]
17-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
18-
PlotlyLight = "ca7969ec-10b3-423e-8d99-40f33abb42bf"
1917
EasyConfig = "acab07b0-f158-46d4-8913-50acef6d41fe"
2018
PlotlyJS = "f0f68f2c-4968-5e81-91da-67840de0976a"
19+
PlotlyLight = "ca7969ec-10b3-423e-8d99-40f33abb42bf"
20+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
21+
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
2122

2223
[targets]
23-
test = ["Test", "PlotlyLight", "EasyConfig", "PlotlyJS"]
24+
test = ["Test", "PlotlyLight", "EasyConfig", "PlotlyJS", "Pkg"]

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,19 @@ PlotlyKaleido.kill_kaleido()
5252
To enable LaTeX (using MathJax v2) in plots, use the keyword argument `mathjax`:
5353
```julia
5454
PlotlyKaleido.start(mathjax=true) # start Kaleido server with MathJax enabled
55+
```
56+
57+
## Windows Note
58+
Many people one Windows have issues with the latest (0.2.1) version of the Kaleido library (see for example [discourse](https://discourse.julialang.org/t/plotlyjs-causes-errors-cant-figure-out-how-to-use-plotlylight-how-to-use-plotly-from-julia/108853/29), [this PR's comment](https://github.com/JuliaPlots/PlotlyKaleido.jl/pull/17#issuecomment-1969325440) and [this issue](https://github.com/plotly/Kaleido/issues/134) on the Kaleido repository).
59+
60+
Many people have succesfully fixed this problem on windows by downgrading the kaleido library to version 0.1.0 (see [the previously mentioned issue](https://github.com/plotly/Kaleido/issues/134)). If you experience issues with `PlotlyKaleido.start()` hanging on windows, you may want try adding `[email protected]` explicitly to your project environment to fix this. You can do so by either doing:
61+
```julia
62+
add Kaleido_jll@v0.1
63+
```
64+
inside the REPL package enviornment, or by calling the following code in the REPL directly:
65+
```julia
66+
begin
67+
import Pkg
68+
Pkg.add(; name = "Kaleido_jll", version = "0.1")
69+
end
5570
```

src/PlotlyKaleido.jl

+38-5
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,47 @@ is_running() = isdefined(P, :proc) && isopen(P.stdin) && process_running(P.proc)
2626

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

29+
# 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)
31+
msg = Channel{String}(1)
32+
33+
task = Task() do
34+
try
35+
put!(msg, readline(io))
36+
catch
37+
put!(msg, "Stopped")
38+
end
39+
end
40+
41+
interrupter = Task() do
42+
sleep(5)
43+
if !istaskdone(task)
44+
Base.throwto(task, InterruptException())
45+
end
46+
end
47+
48+
schedule(interrupter)
49+
schedule(task)
50+
wait(task)
51+
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")
56+
return out
57+
end
58+
2959
function start(;
3060
plotly_version = missing,
3161
mathjax = missing,
3262
mathjax_version::VersionNumber = _mathjax_last_version,
3363
kwargs...,
3464
)
3565
is_running() && return
36-
cmd = joinpath(Kaleido_jll.artifact_dir, "kaleido" * (Sys.iswindows() ? ".cmd" : ""))
37-
basic_cmds = [cmd, "plotly"]
66+
# The kaleido executable must be ran from the artifact directory
67+
BIN = Cmd(kaleido(); dir = Kaleido_jll.artifact_dir)
68+
# We push the mandatory plotly flag
69+
push!(BIN.exec, "plotly")
3870
chromium_flags = ["--disable-gpu", Sys.isapple() ? "--single-process" : "--no-sandbox"]
3971
extra_flags = if plotly_version === missing
4072
(; kwargs...)
@@ -72,12 +104,13 @@ function start(;
72104
push!(user_flags, "--$flag_name=$v")
73105
end
74106
end
75-
BIN = Cmd(vcat(basic_cmds, chromium_flags, user_flags))
107+
# We add the flags to the BIN
108+
append!(BIN.exec, chromium_flags, extra_flags)
76109

77110
kstdin = Pipe()
78111
kstdout = Pipe()
79112
kstderr = Pipe()
80-
kproc =
113+
kproc =
81114
run(pipeline(BIN, stdin = kstdin, stdout = kstdout, stderr = kstderr), wait = false)
82115

83116
process_running(kproc) || error("There was a problem starting up kaleido.")
@@ -92,7 +125,7 @@ function start(;
92125
P.stderr = kstderr
93126
P.proc = kproc
94127

95-
res = readline(P.stdout) # {"code": 0, "message": "Success", "result": null, "version": "0.2.1"}
128+
res = readline_noblock(P.stdout) # {"code": 0, "message": "Success", "result": null, "version": "0.2.1"}
96129
length(res) == 0 && error("Kaleido startup failed.")
97130
code = JSON.parse(res)["code"]
98131
code == 0 || error("Kaleido startup failed with code $code.")

test/runtests.jl

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
using Test
2+
import Pkg
3+
if Sys.iswindows()
4+
# Fix kaleido tests on windows due to [email protected] hanging
5+
Pkg.add(;name = "Kaleido_jll", version = "0.1")
6+
end
27
@test_nowarn @eval using PlotlyKaleido
38

4-
@testset "Start" begin
5-
@test_nowarn PlotlyKaleido.start()
9+
@testset "Start" begin
10+
if Sys.iswindows()
11+
PlotlyKaleido.start()
12+
else
13+
@test_nowarn PlotlyKaleido.start()
14+
end
615
@test PlotlyKaleido.is_running()
716
end
817

0 commit comments

Comments
 (0)