Skip to content

Instantly share code, notes, and snippets.

@jaf7
Forked from jerel/stream_csv.py
Created July 19, 2019 00:43
Show Gist options
  • Save jaf7/c08aa4d4844de7f107c3318e0c09e58c to your computer and use it in GitHub Desktop.
Save jaf7/c08aa4d4844de7f107c3318e0c09e58c to your computer and use it in GitHub Desktop.
Easily export a massive dataset from Django to the browser as a streaming CSV file
import csv
from StringIO import StringIO
from django.http import StreamingHttpResponse
class StreamCSV(object):
def __init__(self):
self._buffer = StringIO()
self._writer = csv.writer(self._buffer)
def writerow(self, row):
""" Stream csv data straight to the connection instead of holding it in memory """
self._writer.writerow(row)
self._buffer.seek(0)
data = self._buffer.read()
self._buffer.seek(0)
self._buffer.truncate()
return data
# paginate your query so that data is fetched from the DB in chunks, loop over each chunk and yield
def export(stream):
yield stream.writerow(['column a', 'column b'])
for page in some_paginator_object:
for item in page.object_list:
yield stream.writerow([item.a, item.b])
stream = StreamCSV()
generator = export(stream)
response = StreamingHttpResponse(generator, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format(download_name)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment