# Plot() output written to HTML file?

**URL:** <https://discourse.julialang.org/t/plot-output-written-to-html-file/31182>\
**Category:** Tooling\
**Tags:** question, plotting\
**Created:** [November 17, 2019, 4:19pm UTC](https://discourse.julialang.org/t/plot-output-written-to-html-file/31182 "2019-11-17T16:19:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pontus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pontus/32/10438_2.png) [@pontus](https://discourse.julialang.org/u/pontus)\
**Post date:** [November 17, 2019, 4:19pm UTC](https://discourse.julialang.org/t/plot-output-written-to-html-file/31182/1 "2019-11-17T16:19:02Z")

</div>

**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. 🤔  
For reference, here is writeup of using `BrowseTables.jl` to export tabular data to an html file:

> [@ANN: BrowseTables.jl](https://discourse.julialang.org/t/ann-browsetables-jl/18304/4):
>
> Interactive dataframe/table exploration using BrowseTables.jl and the reload node app. Have been using a static html server live-reloader app to view dataframe and table output in a browser via an html file written from Julia. Posted examples using PrettyTables.jl ([GitHub - ronisbr/PrettyTables.jl: Print data in formatted tables.](https://github.com/ronisbr/PrettyTables.jl)) with the REPL ([PrettyTables.jl now has initial support for HTML output - #15 by pontus](https://discourse.julialang.org/t/prettytables-jl-now-has-initial-support-for-html-output/30905/15)) and with Sublime Text ([Sublime Text 3: Worth a look! - #85 by pontus](https://discourse.julialang.org/t/sublime-text-3-worth-a-look/17142/85)). See the…

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

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [November 17, 2019, 10:34pm UTC](https://discourse.julialang.org/t/plot-output-written-to-html-file/31182/2 "2019-11-17T22:34:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 18, 2019, 7:51am UTC](https://discourse.julialang.org/t/plot-output-written-to-html-file/31182/3 "2019-11-18T07:51:07Z")

</div>

> **[GitHub - KristofferC/PGFPlotsX.jl: Plots in Julia using the PGFPlots LaTeX...](https://github.com/KristofferC/PGFPlotsX.jl)**
>
> Plots in Julia using the PGFPlots LaTeX package. Contribute to KristofferC/PGFPlotsX.jl development by creating an account on GitHub.

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

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [November 18, 2019, 12:11pm UTC](https://discourse.julialang.org/t/plot-output-written-to-html-file/31182/4 "2019-11-18T12:11:23Z")

</div>

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

```julia
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 ...

```

---

<div class="post-metadata">

**Author:** ![pontus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pontus/32/10438_2.png) [@pontus](https://discourse.julialang.org/u/pontus)\
**Post date:** [November 18, 2019, 3:37pm UTC](https://discourse.julialang.org/t/plot-output-written-to-html-file/31182/5 "2019-11-18T15:37:27Z")

</div>

> [@heliosdrm](#):
>
> Same for IJulia. Or I am missing your point?

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.

> [@Tamas\_Papp](#):
>
> can do SVG which works best in HTML, but it requires a working LaTeX environment.

Thank you, will try that this evening.

> [@pfitzseb](#):
>
> You can call `show` with a mime type, e.g.

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

```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=\"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, 😭 , but was resolved, see: [Resolved: Plot() issue in REPL w/ GR & Qt - Linux Arch](https://discourse.julialang.org/t/resolved-plot-issue-in-repl-w-linux-arch-qt/31237))

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [November 18, 2019, 3:54pm UTC](https://discourse.julialang.org/t/plot-output-written-to-html-file/31182/6 "2019-11-18T15:54:55Z")

</div>

> [@pontus](#):
>
> Thanks, trying this but not getting a file output? Looked at help for `sprint` but it’s not clear how to accomplish.

Try

```julia
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.

---

<div class="post-metadata">

**Author:** ![pontus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pontus/32/10438_2.png) [@pontus](https://discourse.julialang.org/u/pontus)\
**Post date:** [November 18, 2019, 5:25pm UTC](https://discourse.julialang.org/t/plot-output-written-to-html-file/31182/7 "2019-11-18T17:25:18Z")

</div>

Thank you @pfitzseb!! 👍 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](https://discourse.julialang.org/t/formatted-printing-of-arrays/3121) thread:

```julia
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:

```julia
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

```

 ![pfitzseb_soln_showFormatted3SignDig](https://global.discourse-cdn.com/julialang/original/3X/8/9/893d88351a842fa880eef985587f032d297c8a4b.png)
