How can i download a numpy array as a csv using Streamlit download button?

okay so i have a NumPy array that i need to download as a CSV ( without passing through pandas due to memory limitations) and i was wondering if it’s possible to somehow use the
numpy savetxt function to achieve this

Here’s one example:

import io
import numpy as np
import streamlit as st

# Create a NumPy array
arr = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])

# Create an in-memory buffer
with io.BytesIO() as buffer:
    # Write array to buffer
    np.savetxt(buffer, arr, delimiter=",")
    st.download_button(
        label="Download array as CSV",
        data = buffer, # Download buffer
        file_name = 'array.csv',
        mime='text/csv'
    ) 
2 Likes