Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating a CSV file online on Google App Engine

I am using Google App Engine (python), I want my users to be able to download a CSV file generated using some data from the datastore (but I don't want them to download the whole thing, as I re-order the columns and stuff).

I have to use the csv module, because there can be cells containing commas. But the problem that if I do that I will need to write a file, which is not allowed on Google App Engine

What I currently have is something like this:

    tmp = open("tmp.csv", 'w')
    writer = csv.writer(tmp)
    writer.writerow(["foo", "foo,bar", "bar"])

So I guess what I would want to do is either to handle cells with commas.. or to use the csv module without writing a file as this is not possible with GAE..

like image 294
Mohamed Khamis Avatar asked Jun 22 '12 21:06

Mohamed Khamis


3 Answers

I found a way to use the CSV module on GAE! Here it is:

self.response.headers['Content-Type'] = 'application/csv'
writer = csv.writer(self.response.out)

writer.writerow(["foo", "foo,bar", "bar"])

This way you don't need to write any files

like image 125
Mohamed Khamis Avatar answered Oct 16 '22 14:10

Mohamed Khamis


Here is a complete example of using the Python CSV module in GAE. I typically use it for creating a csv file from a gql query and prompting the user to save or open it.

import csv

class MyDownloadHandler(webapp2.RequestHandler):
  def get(self):

    q = ModelName.gql("WHERE foo = 'bar' ORDER BY date ASC")
    reqs = q.fetch(1000)

    self.response.headers['Content-Type'] = 'text/csv'
    self.response.headers['Content-Disposition'] = 'attachment; filename=studenttransreqs.csv'
    writer = csv.writer(self.response.out)

create row labels

    writer.writerow(['Date', 'Time','User' ])

iterate through query returning each instance as a row

    for req in reqs:
        writer.writerow([req.date,req.time,req.user])

Add the appropriate mapping so that when a link is clicked, the file dialog opens

('/mydownloadhandler',MyDownloadHandler),
like image 41
techkilljoy Avatar answered Oct 16 '22 12:10

techkilljoy


import StringIO

tmp = StringIO.StringIO()
writer = csv.writer(tmp)

writer.writerow(["foo", "foo,bar", "bar"])
contents = tmp.getvalue()

tmp.close()
print contents
like image 4
Maria Zverina Avatar answered Oct 16 '22 13:10

Maria Zverina