Skip to content

Instantly share code, notes, and snippets.

@EliAndrewC
Created June 29, 2015 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EliAndrewC/08236994c712e44c4b95 to your computer and use it in GitHub Desktop.
Save EliAndrewC/08236994c712e44c4b95 to your computer and use it in GitHub Desktop.
# Current version:
def csv_file(func):
@wraps(func)
def csvout(self, session):
cherrypy.response.headers['Content-Type'] = 'application/csv'
cherrypy.response.headers['Content-Disposition'] = 'attachment; filename=' + func.__name__ + '.csv'
writer = StringIO()
func(self, csv.writer(writer), session)
return writer.getvalue().encode('utf-8')
return csvout
# Version that supports query parameters:
def csv_file(func):
@wraps(func)
def csvout(self, session, **params): # <- note that we added **params
cherrypy.response.headers['Content-Type'] = 'application/csv'
cherrypy.response.headers['Content-Disposition'] = 'attachment; filename=' + func.__name__ + '.csv'
writer = StringIO()
func(self, csv.writer(writer), session, **params) # <- and we also added **params here
return writer.getvalue().encode('utf-8')
return csvout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment