I’m trying to return a CSV from an action in my webapp, and give the user a prompt to download the file or open it from a spreadsheet app. I can get the CSV to spit out onto the screen, but how do I change the type of the file so that the browser recognizes that this isn’t supposed to be displayed as HTML? Can I use the csv module for this?
import csv def results_csv(self): data = ['895', '898', '897'] return data
To tell the browser the type of content you’re giving it, you need to set the
Content-typeheader to ‘text/csv’. In your Pylons function, the following should do the job:response.headers['Content-type'] = 'text/csv'