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 .don't use st.button in code
- Assistant
You can achieve this by using the following code snippet in your Streamlit app:
import streamlit as st # Create a hyperlink in the markdown if st.markdown("[Download File](#)"): # Write the file content to a temporary file with open("temp_file.txt", "w") as file: file.write("Hello, World!") # Display a success message st.success("File downloaded successfully!")In the code above, we create a hyperlink in the markdown using
st.markdown("[Download File](#)"). When this link is clicked, we open a temporary file (temp_file.txtin this case) and write some content to it. Finally, we display a success message usingst.success("File downloaded successfully!").Please note that clicking on the link will not trigger an actual download prompt in the browser, but it will create a file locally on the server side. If you need to initiate a download in the browser, you may consider using JavaScript or a different approach.