-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.py
More file actions
50 lines (38 loc) · 1.36 KB
/
Copy pathapplication.py
File metadata and controls
50 lines (38 loc) · 1.36 KB
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
from flask import Flask, request, make_response, abort, json
from flask_cors import CORS
from service import Charts
app = Flask(__name__)
CORS(app)
@app.route("/")
def home():
return "Chart API. Use the endpoint /plot/line or /plot/pie"
@app.route('/plot/line', methods=['POST'])
def plot_api():
# json {"x": [n], "y": [n], "labels": [n]}
if not request.json or "x" not in request.json or "y" not in request.json or "labels" not in request.json:
abort(400)
x = request.json.get("x")
y = request.json.get("y")
labels = request.json.get("labels")
if len(x) != len(y) or len(x) != len(labels):
abort(400)
imgData = Charts.get_chart(x, y, labels)
response = make_response(imgData)
response.headers.set('Content-Type', 'image/png')
return response
@app.route('/plot/pie', methods=['POST'])
def plot_pie():
# json {"sizes": [n], "labels": [n]}
jsonData = request.get_json()
if not jsonData or "sizes" not in jsonData or "labels" not in jsonData:
abort(400)
sizes = jsonData.get("sizes")
labels = jsonData.get("labels")
if len(sizes) != len(labels):
abort(400)
imgData = Charts.get_pie_chart(sizes, labels)
response = make_response(imgData)
response.headers.set('Content-Type', 'image/png')
return response
if __name__ == "__main__":
app.run(port=8000, debug=True)