in streamlit app is running on browser and when i click on some link from st.markdown ,…
- User
in streamlit app is running on browser and when i click on some link from st.markdown , i want to download a file locally in my system
- Assistant
To download a file locally in the user's system in a Streamlit app, you can use the following code snippet:
import streamlit as st import base64 # Create a function to download a file def download_file(file_path, file_name): with open(file_path, "rb") as file: file_bytes = file.read() b64 = base64.b64encode(file_bytes).decode() href = f'<a href="data:file/csv;base64,{b64}" download="{file_name}">Click here to download {file_name}</a>' st.markdown(href, unsafe_allow_html=True) # Add a link in the Streamlit app to trigger file download if st.button("Download File"): file_path = "path/to/your/file" file_name = "your_file.csv" download_file(file_path, file_name)In the above code snippet, we first create a function
download_filethat reads the content of the file specified byfile_path, encodes it in base64 format, and generates a download link using the file name provided. We then use this function to trigger the download when the user clicks on a button in the Streamlit app.You can modify the
file_pathandfile_namevariables as per your requirements. This code will allow users to download the specified file when they click on the download link in the Streamlit app running in their browser.