Reputation: 9081
I am trying to return an excel file from Swagger API. Built that using Flask with a Swagger wrapper with Flasgger. Here's the code -
@app.route('/cluster', methods=['POST'])
def index():
"""
This API will help you generate clusters based on keywords present in unstructured text
Call this api passing the following parameters -
Dataset Path - <hostname>\\<path to dataset>
Column Name based on which clustering needs to be done
Number of Clusters
Sample URL: http://localhost:8180/cluster/clusters.csv?dataset=\\\\W1400368\\c$\\Users\\VK046010\\Documents\\Python%20Scripts\\RevCycle_PatientAcc.csv&ext=csv&col=SR_SUM_TXT&no_of_clusters=100
---
tags:
- Clustering API
parameters:
- name: dataset
in: formData
type: file
required: true
description: The fully qualified path of the dataset without the extension.
- name: col
in: query
type: string
required: true
description: The column name on which the clustering needs to be done
- name: no_of_clusters
in: query
type: integer
required: true
description: The number of clusters
"""
global data
data = data.fillna('NULL')
output = StringIO.StringIO()
data.to_csv(output,index=False)
resp = Response(output.getvalue(), mimetype="text/csv")
resp.headers["Accept"] = "text/csv"
resp.headers['Access-Control-Allow-Origin'] = '*'
resp.headers["Content-Disposition"] = "attachment; filename=clusters.csv"
return resp
This returns a downloadable link which I have to rename to csv to make it work.
Question: I am not being able to do this for excel files. No matter how I do it, once I download and rename, excel says the file is corrupt and that's that.
I tried pyexcel and pandas excel writer, didn't work out. Please help!
Upvotes: 1
Views: 2136
Reputation: 11
Try to use flasgger to download excel. You can change the response type to "application/octet-stream" to resolve it.
Upvotes: 1
Reputation: 437
Did you try to change the mimetype?
I think that the traditional mimetype for excel is application/vnd.ms-excel
You could find more details on microsoft files mimetype here: What is a correct mime type for docx, pptx etc?
Upvotes: 0