would be interested to know myself. I could not get a download link working for something that was not an excel/text file. In my case I have a binary (midi) file and in both our cases this part
There is a good example in the download button offered as a work around. See:
The download_button() function is an extension of a workaround based on
the discussions covered in more detail at Awesome Streamlit.
Go to Gallery → Select the App Dropdown → Choose “File Download Workaround”
for more information.
When you use the code offered there. Check the last example to get this code:
--------------------------
# Select a file to download
# --------------------------
if st.checkbox('Select a file to download'):
st.write('~> Use if you want to test uploading / downloading a certain file.')
# Upload file for testing
folder_path = st.text_input('Enter directory: deafult .', '.')
filename = file_selector(folder_path=folder_path)
# Load selected file
with open(filename, 'rb') as f:
s = f.read()
download_button_str = download_button(s, filename, f'Click here to download {filename}')
st.markdown(download_button_str, unsafe_allow_html=True)
import base64
import shutil
def create_download_zip(zip_directory, zip_path, filename='foo.zip"):
"""
zip_directory (str): path to directory you want to zip
zip_path (str): where you want to save zip file
filename (str): download filename for user who download this
"""
shutil.make_archive(zip_path, 'zip', zip_directory)
with open(zip_path, 'rb') as f:
bytes = f.read()
b64 = base64.b64encode(bytes).decode()
href = f'<a href="data:file/zip;base64,{b64}" download=\'{filename}\'>\
download file \
</a>'
st.markdown(href, unsafe_allow_html=True)