Module to download csv file - Is there anything better than this "hack"?

Hi guys,

I’ve used this ‘hack’ for a few months now.

csv = df.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode()
st.markdown('### **⬇️ Download output CSV File **')
href = f'<a href="data:file/csv;base64,{b64}">Download CSV File</a> (right-click and save as ".csv")'
st.markdown(href, unsafe_allow_html=True)

While it does the job, I was wondering whether there was anything better today (official or non-official)?

Ideally, I’d like the user to be able to click on the link without having to manually type the file extension (in this case “csv”) - Is there a way to do this currently?

Thanks in advance :slight_smile:

2 Likes

Hey Charly -

Right now, there isn’t a widget for doing this, but you can avoid having to make the user do anything using the code in this post:

2 Likes

It works great, thanks Randy!

1 Like