How to set file download function?

I want to add a file download function in my Streamlit app. The file is a csv file which contains some technical templates. So users can download the template by themselves within Streamlit apps. Is there any possibility to make this happen? Thank you.

You can encode things as a data link, this is what I’m using (copied from someone else, so thanks to whoever that was :slight_smile: ). Takes a dataframe as an input but you can do it with just text/other file you read in. It’s not a nice static serving, but this should work well enough.

def get_table_download_link(df):
    """Generates a link allowing the data in a given panda dataframe to be downloaded
    in:  dataframe
    out: href string
    csv = df.to_csv(index=False)
    b64 = base64.b64encode(
    ).decode()  # some strings <-> bytes conversions necessary here
    return f'<a href="data:file/csv;base64,{b64}" download="myfilename.csv">Download csv file</a>'

st.markdown(get_table_download_link(data), unsafe_allow_html=True)
1 Like

Thank you very much. lan_Calvert! :grin:

1 Like

So while this is a great solution, it doesn’t seem to work when my CSV file has over a million records. :frowning:
Any idea why? Thoughts on alternatives?