Embed Plots animated gifs to html output. Closes #105

pull/117/head
Matti Pastell 2018-01-04 21:45:43 +02:00
parent 742f00bf6a
commit 48327bd570
2 changed files with 22 additions and 0 deletions

View File

@ -1,5 +1,6 @@
import Plots
"""Pre-execute hooks to set the plot size for the chunk """
function plots_set_size(chunk)
w = chunk.options[:fig_width] * chunk.options[:dpi]
@ -73,3 +74,22 @@ end
function Base.display(report::Report, m::MIME"image/svg+xml", plot::Plots.Plot)
add_plots_figure(report, plot, ".svg")
end
# write out html to view Animated gif
function Base.display(report::Report, ::MIME"text/html", agif::Plots.AnimatedGif)
ext = agif.filename[end-2:end]
res = ""
if ext == "gif"
img = stringmime(MIME("image/gif"), read(agif.filename))
res = "<img src=\"data:image/gif;base64,$img\" />"
elseif ext in ("mov", "mp4")
#Uncomment to embed mp4, make global or chunk option?
#img = stringmime(MIME("video/$ext"), read(agif.filename))
#res = "<video controls><source src=\"data:video/$(ext);base64,$img\" type=\"video/$ext\"></video>"
res = "<video controls><source src=\"$(relpath(agif.filename))\" type=\"video/$ext\"></video>"
else
error("Cannot show animation with extension $ext: $agif")
end
report.rich_output *= "\n" * res * "\n"
end

View File

@ -153,6 +153,8 @@ function img2base64(fig, cwd)
return "data:image/png;base64," * stringmime(MIME("image/png"), raw)
elseif ext == ".svg"
return "data:image/svg+xml;base64," * stringmime(MIME("image/svg+xml"), raw)
elseif ext == ".gif"
return "data:image/gif;base64," * stringmime(MIME("image/gif"), raw)
else
return(fig)
end