# Export dataframe to html file

**URL:** https://discourse.julialang.org/t/export-dataframe-to-html-file/91941
**Category:** New to Julia
**Tags:** dataframes, html, prettytables
**Created:** [December 21, 2022, 10:47am UTC](https://discourse.julialang.org/t/export-dataframe-to-html-file/91941 "2022-12-21T10:47:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)
#### Post date: [December 21, 2022, 10:47am UTC](https://discourse.julialang.org/t/export-dataframe-to-html-file/91941/1 "2022-12-21T10:47:34Z")

</div>

Hi there  
I have read a csv file into Julia, as a dataframe, with some columns of type string, some of type integer, and others of type float. I would like to save, or export or write this dataframe as an html file. What is the simplest procedure?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [December 21, 2022, 10:52am UTC](https://discourse.julialang.org/t/export-dataframe-to-html-file/91941/2 "2022-12-21T10:52:14Z")

</div>

PrettyTables has an [HTML backend](https://ronisbr.github.io/PrettyTables.jl/stable/man/html_backend/#HTML-back-end)

---

<div class="post-metadata">

### Author: ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)
#### Post date: [December 21, 2022, 12:57pm UTC](https://discourse.julialang.org/t/export-dataframe-to-html-file/91941/3 "2022-12-21T12:57:37Z")

</div>

I will have a look at this package. Thank you.

---

<div class="post-metadata">

### Author: ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)
#### Post date: [December 21, 2022, 7:56pm UTC](https://discourse.julialang.org/t/export-dataframe-to-html-file/91941/4 "2022-12-21T19:56:47Z")

</div>

I have this MWE.

```julia
using DataFrames
df = DataFrame(Name=["Andrew", "Barbara"],Id=[1,2],Exam_1=[3.5,7.0])
using PrettyTables
pt = pretty_table(df; backend = Val(:html), alignment=:c)

```

When run in a notebook, this gives me a really prettily formatted html output in a new cell, whereas when run in a plain Julia prompt it gives what I assume is the corresponding code for the html file. My problem now is how to save this pt object to an html file, preserving all the beautiful formatting. I have tried:

```julia
save("output.html", pt)

```

but it prints the error:

```julia
Error encountered while save FileIO.File{FileIO.DataFormat{:HTML}, String}("output.html").

Fatal error:
ERROR: ArgumentError: Argument does not support conversion to HTML.
...

```

How do I concretely export or save the a PrettyTable pt object formatted from the html back-end?

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [December 21, 2022, 8:31pm UTC](https://discourse.julialang.org/t/export-dataframe-to-html-file/91941/5 "2022-12-21T20:31:55Z")

</div>

There is an optional first argument to the `pretty_table` function which is the `io::IO` ([input/output](https://docs.julialang.org/en/v1/base/io-network/)) argument. (See the first couple paragraphs of the [pretty\_table documentation](https://ronisbr.github.io/PrettyTables.jl/dev/man/usage/).)

The following is untested but should work:

```julia
open("output.html", "w") do io
    pretty_table(io, df; backend = Val(:html), alignment=:c)
end

```

The [open](https://docs.julialang.org/en/v1/base/io-network/#Base.open) function with a `do` block is the safe way to open and close I/O streams in Julia. (It will close the stream even if the write fails with an error.)

---

<div class="post-metadata">

### Author: ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)
#### Post date: [December 21, 2022, 9:14pm UTC](https://discourse.julialang.org/t/export-dataframe-to-html-file/91941/6 "2022-12-21T21:14:36Z")

</div>

Thanks Nils and Nathan. Nathan’s chunk of code did work. Thanks again!

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [December 22, 2022, 8:08am UTC](https://discourse.julialang.org/t/export-dataframe-to-html-file/91941/7 "2022-12-22T08:08:01Z")

</div>

The same in plain DataFrames.jl:

```julia
julia> using DataFrames

julia> df = DataFrame(x=1:2, y=["a", "b"],z = [1.5, 2.5])
2×3 DataFrame
 Row │ x y z
     │ Int64 String Float64
─────┼────────────────────────
   1 │ 1 a 1.5
   2 │ 2 b 2.5

julia> open("output.html", "w") do io
           show(io, MIME("text/html"), df)
       end

```
