How to download file in streamlit

not directly, but there is a workaround. Its discussed in the following contribution: File Download Workaround added to awesome-streamlit.org.

I have created the following function for myself and use it frequently. It works fine for bigger files too, my files often have thousands of rows and so far I haven’t hit a limit yet.

st.markdown(get_table_download_link(df), unsafe_allow_html=True)
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(csv.encode()).decode()  # some strings <-> bytes conversions necessary here
    href = f'<a href="data:file/csv;base64,{b64}">Download csv file</a>'
15 Likes