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)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.