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?
df = DataFrame(a=[1,2],b=["a","b"])
DownloadButton(take!(CSV.write(IOBuffer(),df;delim=';')), "data.csv")
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?
df = DataFrame(a=[1,2],b=["a","b"])
DownloadButton(take!(CSV.write(IOBuffer(),df;delim=';')), "data.csv")
How do you define βbetterβ? If you mean not using CSV.jl then you can do:
show(io, MIME("text/csv"), df)
but it will be less flexible than CSV.write
regarding options.
Sorry, I meant simpler, like maybe using a single method to get a βCSV stringβ from a dataframe.
Something like this :
DownloadButton(CSV.write_to_string(df;delim=';'), "data.csv")
In the PlutoUI DownloadButton documentation, I found the example for Json generation quite nice :
DownloadButton(JSON.json(Dict("name" => "merlijn", "can_cook" => true)), "staff.json")
Does this do what you want?
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"
Yes, thank you, thatβs a good simple solution.
DownloadButton(sprint(CSV.write, df), "data.csv")
Otherwise, itβs about the same size as the initial solution when additional parameters are needed :
# 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")