# Download CSV from DataFrame in Pluto

**URL:** <https://discourse.julialang.org/t/download-csv-from-dataframe-in-pluto/94560>\
**Category:** Pluto\
**Tags:** dataframes, csv\
**Created:** [February 13, 2023, 1:49pm UTC](https://discourse.julialang.org/t/download-csv-from-dataframe-in-pluto/94560 "2023-02-13T13:49:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vzion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vzion/32/44768_2.png) [@vzion](https://discourse.julialang.org/u/vzion)\
**Post date:** [February 13, 2023, 1:49pm UTC](https://discourse.julialang.org/t/download-csv-from-dataframe-in-pluto/94560/1 "2023-02-13T13:49:02Z")

</div>

Hello,

Is there a better way to generate a downloadable CSV file from a dataframe in Pluto/PlutoUI than using **CSV.write** with **take!** like this?

```julia
df = DataFrame(a=[1,2],b=["a","b"])
DownloadButton(take!(CSV.write(IOBuffer(),df;delim=';')), "data.csv")

```

---

<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:** [February 13, 2023, 2:09pm UTC](https://discourse.julialang.org/t/download-csv-from-dataframe-in-pluto/94560/2 "2023-02-13T14:09:41Z")

</div>

How do you define “better”? If you mean _not using CSV.jl_ then you can do:

```julia
show(io, MIME("text/csv"), df)

```

but it will be less flexible than `CSV.write` regarding options.

---

<div class="post-metadata">

**Author:** ![vzion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vzion/32/44768_2.png) [@vzion](https://discourse.julialang.org/u/vzion)\
**Post date:** [February 13, 2023, 2:34pm UTC](https://discourse.julialang.org/t/download-csv-from-dataframe-in-pluto/94560/3 "2023-02-13T14:34:59Z")

</div>

Sorry, I meant simpler, like maybe using a single method to get a “CSV string” from a dataframe.

Something like this :

```julia
DownloadButton(CSV.write_to_string(df;delim=';'), "data.csv")

```

In the PlutoUI DownloadButton documentation, I found the example for Json generation quite nice :

```julia
DownloadButton(JSON.json(Dict("name" => "merlijn", "can_cook" => true)), "staff.json")

```

---

<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:** [February 16, 2023, 7:44am UTC](https://discourse.julialang.org/t/download-csv-from-dataframe-in-pluto/94560/4 "2023-02-16T07:44:00Z")

</div>

Does this do what you want?

```julia
julia> using CSV

julia> using DataFrames

julia> df = DataFrame(a=1:2, b=3:4)
2×2 DataFrame
 Row │ a b
     │ Int64 Int64
─────┼──────────────
   1 │ 1 3
   2 │ 2 4

julia> sprint(CSV.write, df)
"a,b\n1,3\n2,4\n"

```

---

<div class="post-metadata">

**Author:** ![vzion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vzion/32/44768_2.png) [@vzion](https://discourse.julialang.org/u/vzion)\
**Post date:** [February 16, 2023, 4:32pm UTC](https://discourse.julialang.org/t/download-csv-from-dataframe-in-pluto/94560/5 "2023-02-16T16:32:07Z")

</div>

Yes, thank you, that’s a good simple solution.

```julia
DownloadButton(sprint(CSV.write, df), "data.csv")

```

Otherwise, it’s about the same size as the initial solution when additional parameters are needed :

```julia
# with sprint
DownloadButton(sprint((i,d) -> CSV.write(i,d;delim=';'),df), "data.csv")
# with take!
DownloadButton(take!(CSV.write(IOBuffer(),df;delim=';')), "data.csv")

```
