Skip to content

Commit

Permalink
enhance cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Nov 12, 2024
1 parent 9e88053 commit 5b611f8
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 22 deletions.
5 changes: 5 additions & 0 deletions PlotsBase/ext/FileIOExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ function _show_pdfbackends(io::IO, ::MIME"image/png", plt::Plot)

# now write from the file
write(io, read(open(pngfn), String))

# cleanup
rm("$fn.pdf")
rm("$fn.png")
nothing
end

# Possibly need to create another extension that has both pgfplotsx and showio
Expand Down
4 changes: 2 additions & 2 deletions PlotsBase/ext/GastonExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ for (mime, term) ∈ (
)
@eval function PlotsBase._show(io::IO, ::MIME{Symbol($mime)}, plt::Plot{GastonBackend})
term = String($term)
tmpfile = tempname() * ".$term"
if plt.o nothing
tmpfile = tempname() * ".$term"
ret = Gaston.save(;
saveopts = gaston_saveopts(plt),
handle = plt.o.handle,
Expand All @@ -192,8 +192,8 @@ for (mime, term) ∈ (
while !isfile(tmpfile)
end # avoid race condition with read in next line
write(io, read(tmpfile))
rm(tmpfile, force = true)
end
isfile(tmpfile) && rm(tmpfile, force = true)
end
nothing
end
Expand Down
2 changes: 0 additions & 2 deletions PlotsBase/src/PlotsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ include("users.jl")
n = length(_examples)
imports = sizehint!(Expr[], n)
examples = sizehint!(Expr[], 10n)
scratch_dir = mktempdir()
for i setdiff(1:n, _backend_skips[backend_name()], _animation_examples)
_examples[i].external && continue
(imp = _examples[i].imports) nothing || push!(imports, imp)
Expand All @@ -174,7 +173,6 @@ include("users.jl")
$func() = begin # evaluate each example in a local scope
$(_examples[i].exprs)
$i == 1 || return # trigger display only for one example
fn = joinpath(scratch_dir, tempname())
show(devnull, current())
nothing
end
Expand Down
28 changes: 17 additions & 11 deletions PlotsBase/src/animation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ function frame(anim::Animation, plt::P = current()) where {P<:AbstractPlot}
push!(anim.frames, filename)
end

giffn() = isijulia() ? "tmp.gif" : tempname() * ".gif"
movfn() = isijulia() ? "tmp.mov" : tempname() * ".mov"
mp4fn() = isijulia() ? "tmp.mp4" : tempname() * ".mp4"
webmfn() = isijulia() ? "tmp.webm" : tempname() * ".webm"
apngfn() = isijulia() ? "tmp.png" : tempname() * ".png"
anim_filename(ext, parent = nothing) = if isijulia()
"tmp"
else
tempname(parent nothing ? tempdir() : parent)
end * ext

giffn(parent = nothing) = anim_filename(".gif", parent)
movfn(parent = nothing) = anim_filename(".mov", parent)
mp4fn(parent = nothing) = anim_filename(".mp4", parent)
webmfn(parent = nothing) = anim_filename(".webm", parent)
apngfn(parent = nothing) = anim_filename(".png", parent)

mutable struct FrameIterator
itr
Expand Down Expand Up @@ -88,27 +94,27 @@ file_extension(fn) = Base.Filesystem.splitext(fn)[2][2:end]
gif(animation[, filename]; fps=20, loop=0, variable_palette=false, verbose=false, show_msg=true)
Creates an animated .gif-file from an `Animation` object.
"""
gif(anim::Animation, fn = giffn(); kw...) = buildanimation(anim, fn; kw...)
gif(anim::Animation, fn = giffn(anim.dir); kw...) = buildanimation(anim, fn; kw...)
"""
mov(animation[, filename]; fps=20, loop=0, verbose=false, show_msg=true)
Creates an .mov-file from an `Animation` object.
"""
mov(anim::Animation, fn = movfn(); kw...) = buildanimation(anim, fn, false; kw...)
mov(anim::Animation, fn = movfn(anim.dir); kw...) = buildanimation(anim, fn, false; kw...)
"""
mp4(animation[, filename]; fps=20, loop=0, verbose=false, show_msg=true)
Creates an .mp4-file from an `Animation` object.
"""
mp4(anim::Animation, fn = mp4fn(); kw...) = buildanimation(anim, fn, false; kw...)
mp4(anim::Animation, fn = mp4fn(anim.dir); kw...) = buildanimation(anim, fn, false; kw...)
"""
webm(animation[, filename]; fps=20, loop=0, verbose=false, show_msg=true)
Creates an .webm-file from an `Animation` object.
"""
webm(anim::Animation, fn = webmfn(); kw...) = buildanimation(anim, fn, false; kw...)
webm(anim::Animation, fn = webmfn(anim.dir); kw...) = buildanimation(anim, fn, false; kw...)
"""
apng(animation[, filename]; fps=20, loop=0, verbose=false, show_msg=true)
Creates an animated .apng-file from an `Animation` object.
"""
apng(anim::Animation, fn = apngfn(); kw...) = buildanimation(anim, fn, false; kw...)
apng(anim::Animation, fn = apngfn(anim.dir); kw...) = buildanimation(anim, fn, false; kw...)

ffmpeg_framerate(fps) = "$fps"
ffmpeg_framerate(fps::Rational) = "$(fps.num)/$(fps.den)"
Expand Down Expand Up @@ -169,7 +175,6 @@ function Base.show(io::IO, ::MIME"text/html", agif::AnimatedGif)
else
error("Cannot show animation with extension $ext: $agif")
end

write(io, html)
nothing
end
Expand All @@ -184,6 +189,7 @@ Base.showable(::MIME"image/png", agif::AnimatedGif) =
Base.show(io::IO, ::MIME"image/gif", agif::AnimatedGif) =
open(fio -> write(io, fio), agif.filename)


Base.show(io::IO, ::MIME"image/png", agif::AnimatedGif) =
open(fio -> write(io, fio), agif.filename)

Expand Down
6 changes: 4 additions & 2 deletions PlotsBase/src/web.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ function show_png_from_html(io::IO, plt::AbstractPlot)
html_to_png(html_fn, png_fn, w, h)

# now read that file data into io
pngdata = readall(png_fn)
write(io, pngdata)
write(io, readall(png_fn))
rm(html_fn)
rm(png_fn)
nothing
end
12 changes: 7 additions & 5 deletions src/Plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,19 @@ if PlotsBase.DEFAULT_BACKEND == "gr" # FIXME: Creating a new global in closed m
$func() = begin # evaluate each example in a local scope
$(PlotsBase._examples[i].exprs)
$i == 1 || return # trigger display only for one example
fn = joinpath(scratch_dir, tempname())
fn = joinpath(scratch_dir, tempname(scratch_dir))
pl = current()
show(devnull, pl)
# FIXME: pgfplotsx requires bug
backend_name() :pgfplotsx && return
if backend_name() :unicodeplots
savefig(pl, "$fn.txt")
return
end
showable(MIME"image/png"(), pl) && savefig(pl, "$fn.png")
showable(MIME"application/pdf"(), pl) && savefig(pl, "$fn.pdf")
if showable(MIME"image/png"(), pl)
savefig(pl, "$fn.png")
end
if showable(MIME"application/pdf"(), pl)
savefig(pl, "$fn.pdf")
end
if showable(MIME"image/svg+xml"(), pl)
show(PipeBuffer(), MIME"image/svg+xml"(), pl)
end
Expand Down

0 comments on commit 5b611f8

Please sign in to comment.