Plot() output written to HTML file?

Any quick suggestions for existing packages or a workflow for including output of a Plot() in Julia html output written to a file??
Looking at a Weave or Literate+Documenter workflow as one option. :thinking:
For reference, here is writeup of using BrowseTables.jl to export tabular data to an html file:

Would like to do something similar, but exporting a plot versus tabular data.
Ideally a one-liner that runs quickly (for interactive use).

2 Likes

You already mentioned Weave and Documenter. What do you need that they don’t provide?

If it’s brevity (you comment on “ideally a one-liner”), the burden required by Weave is not that much. You only need the jmd file with a chunk containing the code to do the plot, and run weave in one line (or one click if you use Juno). Same for IJulia. Or I am missing your point?

Besides, most plotting packages have a savefig or similar function to export the plot. Some accept html output, e.g. plotly(js) --which may be used as backend of Plots.jl—. And most accept svg output, which can be embedded in html as is.

2 Likes

can do SVG which works best in HTML, but it requires a working LaTeX environment.

1 Like

You can call show with a mime type, e.g.

julia> using Plots

julia> p = plot(1:10, rand(10))

julia> sprint(show, "text/html", p)
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\" viewBox=\"0 0 2400 1600\">\n<defs>\n  <clipPath id=\"clip2800\">\n ...
3 Likes

Perhaps, just want to send a plot to index.html in current working directory.
Not sure how to do this with “embedding” (“…accept svg output, which can be embedded in html as is”.). Still working on a process that works for Weave/Documenter, so can’t answer your question quite yet.

Thank you, will try that this evening.

Thanks, trying this but not getting a file output? Looked at help for sprint but it’s not clear how to accomplish.

sprint(show, "text/html", p) 
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\" viewBox=\"0 0 2400 1600\">\n<defs>\n  <clipPath id=\"clip1600\">\n    <rect x=\"0\" y=\"0\" width=\"2400\" height=\"1600\"/>\n  ...

Tried to paste the full output, with and without the quotes, into an index.html file, but that doesn’t work (just blank output in browser save for "\n\n\n \n \n\n\n"

(Had an issue with GR backend for plots in REPL, :sob: , but was resolved, see: Resolved: Plot() issue in REPL w/ GR & Qt - Linux Arch)

Try

julia> open("index.html", "w") do io
         show(io, "text/html", p)
       end

show writes a representation of p to io and sprint basically calls the function with an IOBuffer and gives you a string back. So you could just copy (or write) the content of that string into a file, but the above is a bit simpler.

4 Likes

Thank you @pfitzseb!! :+1: This is working nicely for the plot. Requires no major dependencies and is quite straightforward. Let me play with a few other options and will mark this as solution if nothing simpler is found.

I know including the plot data itself wasn’t part of the requirement, but trying to do that and get it to look clean. BrowseTables.jl doesn’t seem to work within the loop ( HTMLTable(data) produces blank output).
Found a formatting solution in the Formatted printing of arrays thread:

Base.show(io::IO, f::Float64) = @printf io "%1.3f" f

and "%1.4f" for 4 sign digits, etc. Looking at DataFrames as well, as the show there can control scope of which rows/columns to output. Here is code and output at this point:

using Plots ; gr() 
Base.show(io::IO, f::Float64) = @printf io "%1.3f" f
data = rand(14)
p=Plots.plot(1:14, data)
open("index.html", "w") do io
  show(io, data)
  show(io, "text/html", p)
end

using DataFrames
df=rand(11)
p=Plots.plot(1:11, df)
open("index.html", "w") do io
  show(io, df)
  show(io, "text/html", p)
end

2 Likes