Question

So I found a lot of good info on here from how to download data from GAE to a csv.. where I'm stuck is how to export multiple csv files

Summary: I have a webapp that takes multiple inputs and for each input, it sends data to the datastore and then writes from the datastore to the csv. Sample code is below:

class RunScript(webapp2.RequestHandler):
def post(self): 
    listOfTickers = self.request.get('Stocks').split(", ")
    for i in listOfTickers:
        self.main(cgi.escape(i))

main method calls other functions to get the data and ends up writing to csv via the code below:

item_query=Item.query().order(Item.keyOfItem).fetch()
    self.response.headers['Content-Type'] = 'text/csv'
    self.response.headers['Content-Disposition'] = 'attachment; filename=file.csv'
    writer = csv.writer(self.response.out)
    for a in item_query:
        rowToWrite=a.row+a.value
        writer.writerow(rowToWrite)
        #empty datastore as data is drawn out
        a.key.delete()

at this point, the sub function ends and we go back to the for loop in the post() function. I thought the sub function ending would trigger GAE to download the first csv and then download the second csv after it goes through the code again. However, what happens is I get one csv at the end with both sets of data in it.

Any thoughts as to why this is happening? Any help is appreciated!

Was it helpful?

Solution

You keep writing data to the same response, i.e. to the same file. A response cannot return multiple files.

You should create several files, archive them together into a single file, and then return this archive.

OTHER TIPS

To create a response that has multiple separate pieces of data in it, you should look into multipart content type. This is usually used in request to send multipart form data, but it can also be used in response.

There is some support in browsers for multipart response: Browser support of multipart responses

There is also a mpAjax javascript library that can help you parse the multipart response. You might want to test it.

As an alternative yo can also write the cvs's to a single zip archive.

Or you can write you csv files to cloudstorage and use cloudstorage to download the files if you need them.

See this gist for using cloudstorage. It contains an example to create a zip archive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top