Csv download improvements

* name + extension for generated csv and json files

* write csv index where data is meaningful
This commit is contained in:
andrewhn
2016-04-07 01:22:49 +10:00
committed by Maxime Beauchemin
parent 345727635e
commit 65e72d0d07
2 changed files with 13 additions and 1 deletions

View File

@@ -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)
@@ -477,6 +486,7 @@ class Caravel(BaseView):
resp = Response(
payload,
status=status,
headers=generate_download_headers("json"),
mimetype="application/json")
return resp
elif request.args.get("csv") == "true":
@@ -485,6 +495,7 @@ class Caravel(BaseView):
return Response(
payload,
status=status,
headers=generate_download_headers("csv"),
mimetype="application/csv")
else:
if request.args.get("standalone") == "true":

View File

@@ -257,7 +257,8 @@ class BaseViz(object):
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 []