Skip to content

Commit

Permalink
Csv download improvements
Browse files Browse the repository at this point in the history
* name + extension for generated csv and json files

* write csv index where data is meaningful
  • Loading branch information
andrewhn authored and mistercrunch committed Apr 6, 2016
1 parent 3457276 commit 65e72d0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 11 additions & 0 deletions caravel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ def validate_json(form, field): # noqa
raise ValidationError("json isn't valid")


def generate_download_headers(extension):
filename = datetime.now().strftime("%Y%m%d_%H%M%S")
content_disp = "attachment; filename={}.{}".format(filename, extension)
headers = {
"Content-Disposition": content_disp,
}
return headers


class DeleteMixin(object):
@action(
"muldelete", "Delete", "Delete all Really?", "fa-trash", single=False)
Expand Down Expand Up @@ -477,6 +486,7 @@ def explore(self, datasource_type, datasource_id):
resp = Response(
payload,
status=status,
headers=generate_download_headers("json"),
mimetype="application/json")
return resp
elif request.args.get("csv") == "true":
Expand All @@ -485,6 +495,7 @@ def explore(self, datasource_type, datasource_id):
return Response(
payload,
status=status,
headers=generate_download_headers("csv"),
mimetype="application/csv")
else:
if request.args.get("standalone") == "true":
Expand Down
3 changes: 2 additions & 1 deletion caravel/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ def get_json(self):

def get_csv(self):
df = self.get_df()
return df.to_csv(index=False)
include_index = not isinstance(df.index, pd.RangeIndex)
return df.to_csv(index=include_index)

def get_data(self):
return []
Expand Down

0 comments on commit 65e72d0

Please sign in to comment.