i'm use Python Klein for my small API. Can return csv as response, but is this possible to return Excel file? Flask has this functional but what about Klein?
Task:
Have Excel file on hard drive and want return it to as attachment, so user can download it via API call.
Example: With csv file all is working good like
@app.route('/download/', branch=True, methods=['GET'])
def download(request):
request.setHeader("Access-Control-Allow-Origin", "*")
request.setHeader('Content-Type', 'text/csv')
request.setHeader('Content-Disposition', 'attachment; filename=test.csv')
file = open('test.csv', "r")
csv_data = file.read()
return csv_data
But how to be with an Excel file?
Found solution:
from twisted.web.static import File
from klein import Klein
app = Klein()
@app.route('/', branch=True)
def pg_index(request):
request.setHeader("Access-Control-Allow-Origin", "*")
request.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
request.setHeader('Content-Disposition', 'attachment; filename=test.xlsx'.format(list_id))
return File('./test.xlsx')