2018-07-26 20:29:16 +02:00
|
|
|
module WeavePlots
|
2020-03-17 02:57:36 +01:00
|
|
|
|
2020-05-10 06:35:54 +02:00
|
|
|
using Base64, ..Plots, ..Weave
|
2018-01-04 20:45:43 +01:00
|
|
|
|
2020-05-10 06:35:54 +02:00
|
|
|
|
|
|
|
# Pre-execute hooks to set the plot size for the chunk
|
2016-04-29 15:41:18 +02:00
|
|
|
function plots_set_size(chunk)
|
2020-05-08 16:39:17 +02:00
|
|
|
w = chunk.options[:fig_width] * chunk.options[:dpi]
|
|
|
|
h = chunk.options[:fig_height] * chunk.options[:dpi]
|
|
|
|
Plots.default(size = (w, h))
|
|
|
|
return chunk
|
2016-04-29 15:41:18 +02:00
|
|
|
end
|
|
|
|
|
2018-07-26 20:29:16 +02:00
|
|
|
Weave.push_preexecute_hook(plots_set_size)
|
2016-10-31 16:55:38 +01:00
|
|
|
|
2020-05-08 16:39:17 +02:00
|
|
|
# PNG or SVG is not working, output html
|
|
|
|
function Base.display(
|
|
|
|
report::Weave.Report,
|
|
|
|
m::MIME"image/svg+xml",
|
|
|
|
data::Plots.Plot{Plots.PlotlyBackend},
|
|
|
|
)#
|
|
|
|
# Remove extra spaces from start of line for pandoc
|
|
|
|
s = repr(MIME("text/html"), data)
|
|
|
|
splitted = split(s, "\n")
|
|
|
|
start = split(splitted[1], r"(?=<div)")
|
|
|
|
# script = lstrip(start[1]) # local
|
|
|
|
|
|
|
|
div = lstrip(start[2])
|
|
|
|
plot = join(map(lstrip, splitted[2:end]), "\n")
|
|
|
|
|
|
|
|
if report.first_plot
|
|
|
|
report.header_script *= "<script src=\"https://cdn.plot.ly/plotly-latest.min.js\"></script>"
|
|
|
|
report.first_plot = false
|
|
|
|
end
|
2016-10-31 16:55:38 +01:00
|
|
|
|
|
|
|
report.rich_output *= "\n" * div * "\n" * plot
|
|
|
|
end
|
2016-11-01 09:40:40 +01:00
|
|
|
|
2020-05-08 16:39:17 +02:00
|
|
|
function Base.display(
|
|
|
|
report::Weave.Report,
|
|
|
|
m::MIME"image/png",
|
|
|
|
data::Plots.Plot{Plots.PlotlyBackend},
|
|
|
|
)#
|
|
|
|
display(report, MIME("image/svg+xml"), data)
|
2016-11-02 08:26:10 +01:00
|
|
|
end
|
|
|
|
|
2020-05-08 16:39:17 +02:00
|
|
|
# PNG or SVG is not working, output html
|
|
|
|
function Base.display(
|
|
|
|
report::Weave.Report,
|
|
|
|
m::MIME"image/svg+xml",
|
|
|
|
plot::Plots.Plot{Plots.PlotlyJSBackend},
|
|
|
|
)
|
|
|
|
body = Plots.PlotlyJS.html_body(plot.o.plot)
|
2016-11-02 08:26:10 +01:00
|
|
|
|
2020-05-08 16:39:17 +02:00
|
|
|
if report.first_plot
|
|
|
|
report.header_script *= "<script src=\"https://cdn.plot.ly/plotly-latest.min.js\"></script>"
|
|
|
|
report.first_plot = false
|
|
|
|
end
|
2016-11-01 09:40:40 +01:00
|
|
|
|
2020-05-08 16:39:17 +02:00
|
|
|
report.rich_output *= "\n" * body
|
2016-11-01 09:40:40 +01:00
|
|
|
end
|
|
|
|
|
2020-05-08 16:39:17 +02:00
|
|
|
function Base.display(
|
|
|
|
report::Weave.Report,
|
|
|
|
m::MIME"image/png",
|
|
|
|
plot::Plots.Plot{Plots.PlotlyJSBackend},
|
|
|
|
)
|
|
|
|
display(report, MIME("image/svg+xml"), data)
|
2016-11-01 09:40:40 +01:00
|
|
|
end
|
2016-11-01 14:23:14 +01:00
|
|
|
|
|
|
|
"""Add saved figure name to results and return the name"""
|
2018-07-26 20:29:16 +02:00
|
|
|
function add_plots_figure(report::Weave.Report, plot::Plots.Plot, ext)
|
2020-05-08 16:39:17 +02:00
|
|
|
chunk = report.cur_chunk
|
|
|
|
full_name, rel_name = Weave.get_figname(report, chunk, ext = ext)
|
2016-11-01 14:23:14 +01:00
|
|
|
|
2020-05-08 16:39:17 +02:00
|
|
|
Plots.savefig(plot, full_name)
|
|
|
|
push!(report.figures, rel_name)
|
|
|
|
report.fignum += 1
|
|
|
|
return full_name
|
2016-11-01 14:23:14 +01:00
|
|
|
end
|
|
|
|
|
2018-07-26 20:29:16 +02:00
|
|
|
function Base.display(report::Weave.Report, m::MIME"application/pdf", plot::Plots.Plot)
|
2016-12-19 13:46:00 +01:00
|
|
|
add_plots_figure(report, plot, ".pdf")
|
2016-11-01 14:23:14 +01:00
|
|
|
end
|
|
|
|
|
2018-07-26 20:29:16 +02:00
|
|
|
function Base.display(report::Weave.Report, m::MIME"image/png", plot::Plots.Plot)
|
2016-12-19 13:46:00 +01:00
|
|
|
add_plots_figure(report, plot, ".png")
|
2016-11-01 14:23:14 +01:00
|
|
|
end
|
|
|
|
|
2018-07-26 20:29:16 +02:00
|
|
|
function Base.display(report::Weave.Report, m::MIME"image/svg+xml", plot::Plots.Plot)
|
2016-12-19 13:46:00 +01:00
|
|
|
add_plots_figure(report, plot, ".svg")
|
2016-11-03 13:10:35 +01:00
|
|
|
end
|
2018-01-04 20:45:43 +01:00
|
|
|
|
|
|
|
# write out html to view Animated gif
|
2018-07-26 20:29:16 +02:00
|
|
|
function Base.display(report::Weave.Report, ::MIME"text/html", agif::Plots.AnimatedGif)
|
2020-05-08 16:39:17 +02:00
|
|
|
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"
|
2018-01-04 20:45:43 +01:00
|
|
|
end
|
2018-07-26 20:29:16 +02:00
|
|
|
|
|
|
|
end
|