Skip to content

Commit

Permalink
fix savefig ambiguity (#16)
Browse files Browse the repository at this point in the history
* fix savefig ambiguity

* my dear eps

* format
  • Loading branch information
BeastyBlacksmith authored Jan 30, 2024
1 parent 7ce21ff commit 592af20
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
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

2 comments on commit 592af20

@BeastyBlacksmith
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/99900

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.2.2 -m "<description of version>" 592af20ee6e35450f46059950f4e93eeea576199
git push origin v2.2.2

Please sign in to comment.