-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
55 lines (42 loc) · 1.6 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import pandas as pd
from flask import Flask, jsonify, request, make_response
from openpyxl import Workbook
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl.writer.excel import save_virtual_workbook
app = Flask(__name__)
@app.route("/")
def index():
return jsonify({
'message': 'Welcome to json-excel! Head over to https://github.com/RobinCheptileh/json-excel for documentation.'
})
@app.route("/api/v1/to-spreadsheet", methods=['POST'])
def to_spreadsheet():
if not request.json or 'rows' not in request.json or not isinstance(request.json['rows'], list):
return jsonify({
'message': 'Bad Request'
}), 400
try:
data = request.json
header = 'header' in data
data_frame = pd.DataFrame(data['rows'], columns=data['header']) if header else pd.DataFrame(data['rows'])
print(data_frame)
wb = Workbook()
ws = wb.active
for r in dataframe_to_rows(data_frame, index=False, header=header):
ws.append(r)
raw_data = save_virtual_workbook(wb)
response = make_response(raw_data)
response.headers['Content-Type'] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
response.headers['Content-Disposition'] = "inline; filename=spreadsheet.xlsx"
return response
except ValueError as error:
return jsonify({
'message': str(error)
}), 400
@app.route("/api/v1/to-json", methods=['POST'])
def to_json():
return jsonify({
'message': 'Coming Soon!'
})
if __name__ == "__main__":
app.run(debug=True)