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