Skip to content

Commit

Permalink
fix savefig issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sglyon committed Jul 29, 2021
1 parent 195bd2d commit 179c724
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
15 changes: 10 additions & 5 deletions src/kaleido.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ function _start_kaleido_process()
nothing
end

savefig(p::SyncPlot; kwargs...) = savefig(p.plot; kwargs...)

function savefig(
p::Plot;
width::Union{Nothing,Int}=nothing,
Expand Down Expand Up @@ -119,6 +121,10 @@ function savefig(
end
end


@inline _get_Plot(p::Plot) = p
@inline _get_Plot(p::SyncPlot) = p.plot

"""
savefig(
io::IO,
Expand All @@ -136,21 +142,20 @@ image scale. `width` and `height` set the dimensions, in pixels. Defaults
are taken from `p.layout`, or supplied by plotly
"""
function savefig(io::IO,
p::Plot;
p::Union{SyncPlot,Plot};
width::Union{Nothing,Int}=nothing,
height::Union{Nothing,Int}=nothing,
scale::Union{Nothing,Real}=nothing,
format::String="png")


if format == "html"
return show(io, MIME("text/html"), p, include_mathjax="cdn", include_plotlyjs="cdn", full_html=true)
return show(io, MIME("text/html"), _get_Plot(p), include_mathjax="cdn", include_plotlyjs="cdn", full_html=true)
end

bytes = savefig(p, width=width, height=height, scale=scale, format=format)
write(io, bytes)
end


"""
savefig(
p::Plot, fn::AbstractString;
Expand All @@ -167,7 +172,7 @@ image scale. `width` and `height` set the dimensions, in pixels. Defaults
are taken from `p.layout`, or supplied by plotly
"""
function savefig(
p::Plot, fn::AbstractString;
p::Union{SyncPlot,Plot}, fn::AbstractString;
format::Union{Nothing,String}=nothing,
width::Union{Nothing,Int}=nothing,
height::Union{Nothing,Int}=nothing,
Expand Down
21 changes: 12 additions & 9 deletions test/kaleido.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
function myplot(fn)
function myplot(fn, func)
x = 0:0.1:2π
plt = Plot(scatter(x=x, y=sin.(x)))
plt = func(scatter(x=x, y=sin.(x)))
savefig(plt, fn)
end

@testset "kaleido" begin
for ext in [PlotlyBase.ALL_FORMATS..., "html"]
if ext === "eps"
continue
for func in [Plot, plot]
for ext in [PlotlyJS.ALL_FORMATS..., "html"]
if ext === "eps"
continue
end
fn = tempname() * "." * ext
@show func, fn
myplot(fn, func) == fn
@test isfile(fn)
rm(fn)
end
@show fn = tempname() * "." * ext
myplot(fn) == fn
@test isfile(fn)
rm(fn)
end
end

0 comments on commit 179c724

Please sign in to comment.