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.

    Examples:
    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))})
st.write(df)

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')
st.write(s)

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)


8 Likes

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:

https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_text_buttons2

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