Skip to content

Instantly share code, notes, and snippets.

@CVertex
Created December 18, 2018 07:03
Show Gist options
  • Save CVertex/d29e98fc1a5d4282c8248ddfc2f5d425 to your computer and use it in GitHub Desktop.
Save CVertex/d29e98fc1a5d4282c8248ddfc2f5d425 to your computer and use it in GitHub Desktop.
import csv
import io
class CsvDownloadHandler(handlers.BaseAjaxHandler):
# TODO, POST instead of GET to avoid XSSI prefixes
def get(self):
self.response.headers['Content-Type'] = 'application/csv'
self.response.headers["Content-Disposition"] = 'attachment; filename="%s"' % 'all-sessions.csv'
# Build a string buffer
output = io.BytesIO()
# Write the CSV into the string buffer
writer = csv.writer(output)
writer.writerow(['foo','foo,bar', 'bar'])
writer.writerow(['foo','foo,bar', 'bar'])
writer.writerow(['foo','foo,bar', 'bar'])
# Write to the output stream
self._RawWrite(output.getvalue())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment