Skip to content

Commit 592af20

Browse files
fix savefig ambiguity (#16)
* fix savefig ambiguity * my dear eps * format
1 parent 7ce21ff commit 592af20

File tree

3 files changed

+56
-24
lines changed

3 files changed

+56
-24
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PlotlyKaleido"
22
uuid = "f2990250-8cf9-495f-b13a-cce12b45703c"
33
authors = ["Simon Christ <[email protected]>", "Spencer Lyon <[email protected]>"]
4-
version = "2.2.1"
4+
version = "2.2.2"
55

66
[deps]
77
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

src/PlotlyKaleido.jl

+36-20
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,35 @@ kill_kaleido() = is_running() && (kill(P.proc); wait(P.proc))
2424

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

27-
restart(;kwargs...) = (kill_kaleido(); start(;kwargs...))
28-
29-
function start(;plotly_version = missing,
30-
mathjax = missing, mathjax_version::VersionNumber = _mathjax_last_version,
31-
kwargs...)
27+
restart(; kwargs...) = (kill_kaleido(); start(; kwargs...))
28+
29+
function start(;
30+
plotly_version = missing,
31+
mathjax = missing,
32+
mathjax_version::VersionNumber = _mathjax_last_version,
33+
kwargs...,
34+
)
3235
is_running() && return
3336
cmd = joinpath(Kaleido_jll.artifact_dir, "kaleido" * (Sys.iswindows() ? ".cmd" : ""))
3437
basic_cmds = [cmd, "plotly"]
3538
chromium_flags = ["--disable-gpu", Sys.isapple() ? "--single-process" : "--no-sandbox"]
3639
extra_flags = if plotly_version === missing
37-
(;
38-
kwargs...
39-
)
40+
(; kwargs...)
4041
else
4142
# We create a plotlyjs flag pointing at the specified plotly version
42-
(;
43-
plotlyjs = "https://cdn.plot.ly/plotly-$(plotly_version).min.js",
44-
kwargs...
45-
)
43+
(; plotlyjs = "https://cdn.plot.ly/plotly-$(plotly_version).min.js", kwargs...)
4644
end
4745
if !(mathjax === missing)
48-
if mathjax_version > _mathjax_last_version
49-
error("The given mathjax version ($(mathjax_version)) is greater than the last supported version ($(_mathjax_last_version)) of Kaleido.")
46+
if mathjax_version > _mathjax_last_version
47+
error(
48+
"The given mathjax version ($(mathjax_version)) is greater than the last supported version ($(_mathjax_last_version)) of Kaleido.",
49+
)
5050
end
5151
if mathjax isa Bool && mathjax
52-
push!(chromium_flags, "--mathjax=$(_mathjax_url_path)/$(mathjax_version)/MathJax.js")
52+
push!(
53+
chromium_flags,
54+
"--mathjax=$(_mathjax_url_path)/$(mathjax_version)/MathJax.js",
55+
)
5356
elseif mathjax isa String
5457
# We expect the keyword argument to be a valid URL or similar, else error "Kaleido startup failed with code 1".
5558
push!(chromium_flags, "--mathjax=$(mathjax)")
@@ -61,20 +64,21 @@ function start(;plotly_version = missing,
6164
end
6265
# Taken inspiration from https://github.com/plotly/Kaleido/blob/3b590b563385567f257db8ff27adae1adf77821f/repos/kaleido/py/kaleido/scopes/base.py#L116-L141
6366
user_flags = String[]
64-
for (k,v) in pairs(extra_flags)
67+
for (k, v) in pairs(extra_flags)
6568
flag_name = replace(string(k), "_" => "-")
6669
if v isa Bool
6770
v && push!(user_flags, "--$flag_name")
6871
else
6972
push!(user_flags, "--$flag_name=$v")
7073
end
7174
end
72-
BIN = Cmd(vcat(basic_cmds, chromium_flags, user_flags))
75+
BIN = Cmd(vcat(basic_cmds, chromium_flags, user_flags))
7376

7477
kstdin = Pipe()
7578
kstdout = Pipe()
7679
kstderr = Pipe()
77-
kproc = run(pipeline(BIN, stdin=kstdin, stdout=kstdout, stderr=kstderr), wait=false)
80+
kproc =
81+
run(pipeline(BIN, stdin = kstdin, stdout = kstdout, stderr = kstderr), wait = false)
7882

7983
process_running(kproc) || error("There was a problem starting up kaleido.")
8084
close(kstdout.in)
@@ -121,8 +125,20 @@ function save_payload(io::IO, payload::AbstractString, format::AbstractString)
121125
write(io, bytes)
122126
end
123127

124-
function savefig(io::IO, plot; height=500, width=700, scale=1, format="png")
125-
payload = JSON.json((; height, width, scale, format, data=plot))
128+
function savefig(io::IO, plot; height = 500, width = 700, scale = 1, format = "png")
129+
payload = JSON.json((; height, width, scale, format, data = plot))
130+
save_payload(io, payload, format)
131+
end
132+
133+
function savefig(
134+
io::IO,
135+
plot::AbstractString;
136+
height = 500,
137+
width = 700,
138+
scale = 1,
139+
format = "png",
140+
)
141+
payload = "{\"width\":$width,\"height\":$height,\"scale\":$scale,\"data\": $plot}"
126142
save_payload(io, payload, format)
127143
end
128144

test/runtests.jl

+19-3
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,36 @@ import PlotlyLight, EasyConfig, PlotlyJS
1515
file = tempname() * ".$ext"
1616
open(io -> PlotlyKaleido.save_payload(io, plt, ext), file, "w")
1717
@test isfile(file)
18+
@test filesize(file) > 0
1819
rm(file)
1920
end
2021
end
2122

23+
@testset "Saving Base structures" begin
24+
plt0 = Dict(:data => [Dict(:x => [0, 1, 2], :type => "scatter", :y => [1, 2, 3])])
25+
for plt in (plt0, NamedTuple(plt0))
26+
for ext in PlotlyKaleido.ALL_FORMATS
27+
ext == "eps" && continue # TODO" Why does this work above but not here?
28+
file = tempname() * ".$ext"
29+
PlotlyKaleido.savefig(file, plt)
30+
@test isfile(file)
31+
@test filesize(file) > 0
32+
rm(file)
33+
end
34+
end
35+
end
36+
2237
@testset "Saving PlotlyJS & PlotlyLight" begin
2338
for plt in [
24-
PlotlyJS.plot(PlotlyJS.scatter(x=rand(10))),
25-
PlotlyLight.Plot(EasyConfig.Config(x=rand(10)))
26-
]
39+
PlotlyJS.plot(PlotlyJS.scatter(x = rand(10))),
40+
PlotlyLight.Plot(EasyConfig.Config(x = rand(10))),
41+
]
2742
for ext in PlotlyKaleido.ALL_FORMATS
2843
ext == "eps" && continue # TODO" Why does this work above but not here?
2944
file = tempname() * ".$ext"
3045
@test PlotlyKaleido.savefig(plt, file) == file
3146
@test isfile(file)
47+
@test filesize(file) > 0
3248
rm(file)
3349
end
3450
end

0 commit comments

Comments
 (0)