Here's a download function that works for dataframes and txt

In case it helps anyone, the download function below is based on a previous solution and post: How to download file in streamlit

The code below extends that approach to work for dataframes and txt by adding a couple parameters.

import base64

import streamlit as st
import pandas as pd

def download_link(object_to_download, download_filename, download_link_text):
    Generates a link to download the given object_to_download.

    object_to_download (str, pd.DataFrame):  The object to be downloaded.
    download_filename (str): filename and extension of file. e.g. mydata.csv, some_txt_output.txt
    download_link_text (str): Text to display for download link.

    download_link(YOUR_DF, 'YOUR_DF.csv', 'Click here to download data!')
    download_link(YOUR_STRING, 'YOUR_STRING.txt', 'Click here to download your text!')

    if isinstance(object_to_download,pd.DataFrame):
        object_to_download = object_to_download.to_csv(index=False)

    # some strings <-> bytes conversions necessary here
    b64 = base64.b64encode(object_to_download.encode()).decode()

    return f'<a href="data:file/txt;base64,{b64}" download="{download_filename}">{download_link_text}</a>'

# Examples
df = pd.DataFrame({'x': list(range(10)), 'y': list(range(10))})

if st.button('Download Dataframe as CSV'):
    tmp_download_link = download_link(df, 'YOUR_DF.csv', 'Click here to download your data!')
    st.markdown(tmp_download_link, unsafe_allow_html=True)

s = st.text_input('Enter text here')

if st.button('Download input as a text file'):
    tmp_download_link = download_link(s, 'YOUR_INPUT.txt', 'Click here to download your text!')
    st.markdown(tmp_download_link, unsafe_allow_html=True)


Hey @Chad_Mitchell, great to see you over here on the forums. Welcome to the Streamlit community!

I haven’t had a chance to test this out, but from my quick skim, this does look like a useful functionality that everyone is always asking for :+1:

1 Like

Hey @randyzwitch, glad to be here! I have a list of workflow tools I’m building for my current company in Streamlit that I’ll be sharing when Streamlit for Teams is out (and GitHub this week), and this is on my list of projects to look into further. I’d like to generalize it to a larger set of file types and add auto download functionality on button click rather than returning a link. Thank you for checking into it.

1 Like

For your button question, I wonder if you could get away with something like this in a st.markdown with unsafe_html enabled:

1 Like

Interesting. Prima facie, that seems like it’ll work. I’ll look into it today. Cheers!

Hi, as an update, I wasn’t able to get this to work. I tried a few other options as well, but no luck.

This ended up being the approach that worked. Thank you again! A function to download (probably not) all files

1 Like

Really nice, but is there somehow that this could work with utf-8? My problem is that my csv export file all the letters ÅÄÖ becomes är Ã¥ ä ö

Possible solution:

Thank you so much for this.

1 Like

@Chad_Mitchell didn’t worked for me. I pass all the fuctions needed but when i press the button to download he returns to the main page of my streamlit app.

df4 is the dataframe i want to e downloaded
cg_result.csv is the filename and his extesion
Click here to download your data! is the text who gonna be displayed to donwload the link

btn_download = st.button("Click to Donwload the SpreedSheet")

        if btn_download:
            tmp_download_link = download_link(df4, 'Cg_result.csv', 'Click here to download your data!')
            st.markdown(tmp_download_link, unsafe_allow_html=True)

            s = st.text_input('Enter text here')

Hey @Chad_Mitchell , your code works for one of my data science projects. Thanks for sharing!

Starting with v0.88, we now have st.download_button natively built into Streamlit. Check out the release notes and demo: 0.88.0 Release Notes