How to add a download excel/csv function to a button?

I have this button below.
And a data frame named ‘df_download’.
How can I function this button to download the ‘df_download’ as excel/csv file?

download=st.button('Download Excel File')
 if download:
  'Download Started!'
  liste= ['A','B','C']
  df_download= pd.DataFrame(liste)
  df_download.columns=['Title']
  df_download

#How can I download df_download?

@Chad_Mitchell created something similar to this idea:

2 Likes

@randyzwitch thank you for the inspiration.

I manage to solve it like :

if download:
  'Download Started!'
  liste= ['A','B','C']
  df_download= pd.DataFrame(liste)
  df_download.columns=['Title']
  df_download
  csv = df_download.to_csv(index=False)
  b64 = base64.b64encode(csv.encode()).decode()  # some strings
  linko= f'<a href="data:file/csv;base64,{b64}" download="myfilename.csv">Download csv file</a>'
  st.markdown(linko, unsafe_allow_html=True)