How to download file in streamlit


def get_minio_link(buffer, filename, content_type, bucket_name):
    minio_client.put_object(
        bucket_name,
        filename,
        data=buffer,
        length=len(buffer.getvalue()),
        content_type=content_type,
    )
    download_url = minio_client.presigned_get_object(
                                       bucket_name,
                                       filename)
    return download_url

You can use it like this


csv_bytes = df.to_csv().encode("utf-8")

csv_buffer = BytesIO(csv_bytes)

filename = "mydf.csv"

content_type = "application/csv"

uri = get_minio_link(buffer=csv_buffer, filename=filename, content_type=content_type, bucket_name="my-bucket")

st.markdown(
    f"""
    <a href='{uri}' download>Click to Download {filename}</a>
    """,
    unsafe_allow_html=True,
)

Its untested but it should work if you feed a proper minio_client.

Psst. by default link will be available for 7 days. I would change expire to something less. https://docs.min.io/docs/python-client-api-reference.html#presigned_get_object

Hope it helps ! :slight_smile:

5 Likes