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

fix savefig ambiguity #16

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PlotlyKaleido"
uuid = "f2990250-8cf9-495f-b13a-cce12b45703c"
authors = ["Simon Christ <[email protected]>", "Spencer Lyon <[email protected]>"]
version = "2.2.1"
version = "2.2.2"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
56 changes: 36 additions & 20 deletions src/PlotlyKaleido.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,35 @@ 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...))

function start(;plotly_version = missing,
mathjax = missing, mathjax_version::VersionNumber = _mathjax_last_version,
kwargs...)
restart(; kwargs...) = (kill_kaleido(); start(; kwargs...))

function start(;
plotly_version = missing,
mathjax = missing,
mathjax_version::VersionNumber = _mathjax_last_version,
kwargs...,
)
is_running() && return
cmd = joinpath(Kaleido_jll.artifact_dir, "kaleido" * (Sys.iswindows() ? ".cmd" : ""))
basic_cmds = [cmd, "plotly"]
chromium_flags = ["--disable-gpu", Sys.isapple() ? "--single-process" : "--no-sandbox"]
extra_flags = if plotly_version === missing
(;
kwargs...
)
(; kwargs...)
else
# We create a plotlyjs flag pointing at the specified plotly version
(;
plotlyjs = "https://cdn.plot.ly/plotly-$(plotly_version).min.js",
kwargs...
)
(; plotlyjs = "https://cdn.plot.ly/plotly-$(plotly_version).min.js", kwargs...)
end
if !(mathjax === missing)
if mathjax_version > _mathjax_last_version
error("The given mathjax version ($(mathjax_version)) is greater than the last supported version ($(_mathjax_last_version)) of Kaleido.")
if mathjax_version > _mathjax_last_version
error(
"The given mathjax version ($(mathjax_version)) is greater than the last supported version ($(_mathjax_last_version)) of Kaleido.",
)
end
if mathjax isa Bool && mathjax
push!(chromium_flags, "--mathjax=$(_mathjax_url_path)/$(mathjax_version)/MathJax.js")
push!(
chromium_flags,
"--mathjax=$(_mathjax_url_path)/$(mathjax_version)/MathJax.js",
)
elseif mathjax isa String
# We expect the keyword argument to be a valid URL or similar, else error "Kaleido startup failed with code 1".
push!(chromium_flags, "--mathjax=$(mathjax)")
Expand All @@ -61,20 +64,21 @@ function start(;plotly_version = missing,
end
# Taken inspiration from https://github.com/plotly/Kaleido/blob/3b590b563385567f257db8ff27adae1adf77821f/repos/kaleido/py/kaleido/scopes/base.py#L116-L141
user_flags = String[]
for (k,v) in pairs(extra_flags)
for (k, v) in pairs(extra_flags)
flag_name = replace(string(k), "_" => "-")
if v isa Bool
v && push!(user_flags, "--$flag_name")
else
push!(user_flags, "--$flag_name=$v")
end
end
BIN = Cmd(vcat(basic_cmds, chromium_flags, user_flags))
BIN = Cmd(vcat(basic_cmds, chromium_flags, user_flags))

kstdin = Pipe()
kstdout = Pipe()
kstderr = Pipe()
kproc = run(pipeline(BIN, stdin=kstdin, stdout=kstdout, stderr=kstderr), wait=false)
kproc =
run(pipeline(BIN, stdin = kstdin, stdout = kstdout, stderr = kstderr), wait = false)

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

function savefig(io::IO, plot; height=500, width=700, scale=1, format="png")
payload = JSON.json((; height, width, scale, format, data=plot))
function savefig(io::IO, plot; height = 500, width = 700, scale = 1, format = "png")
payload = JSON.json((; height, width, scale, format, data = plot))
save_payload(io, payload, format)
end

function savefig(
io::IO,
plot::AbstractString;
height = 500,
width = 700,
scale = 1,
format = "png",
)
payload = "{\"width\":$width,\"height\":$height,\"scale\":$scale,\"data\": $plot}"
save_payload(io, payload, format)
end

Expand Down
22 changes: 19 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,36 @@ import PlotlyLight, EasyConfig, PlotlyJS
file = tempname() * ".$ext"
open(io -> PlotlyKaleido.save_payload(io, plt, ext), file, "w")
@test isfile(file)
@test filesize(file) > 0
rm(file)
end
end

@testset "Saving Base structures" begin
plt0 = Dict(:data => [Dict(:x => [0, 1, 2], :type => "scatter", :y => [1, 2, 3])])
for plt in (plt0, NamedTuple(plt0))
for ext in PlotlyKaleido.ALL_FORMATS
ext == "eps" && continue # TODO" Why does this work above but not here?
file = tempname() * ".$ext"
PlotlyKaleido.savefig(file, plt)
@test isfile(file)
@test filesize(file) > 0
rm(file)
end
end
end

@testset "Saving PlotlyJS & PlotlyLight" begin
for plt in [
PlotlyJS.plot(PlotlyJS.scatter(x=rand(10))),
PlotlyLight.Plot(EasyConfig.Config(x=rand(10)))
]
PlotlyJS.plot(PlotlyJS.scatter(x = rand(10))),
PlotlyLight.Plot(EasyConfig.Config(x = rand(10))),
]
for ext in PlotlyKaleido.ALL_FORMATS
ext == "eps" && continue # TODO" Why does this work above but not here?
file = tempname() * ".$ext"
@test PlotlyKaleido.savefig(plt, file) == file
@test isfile(file)
@test filesize(file) > 0
rm(file)
end
end
Expand Down
Loading