-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
executable file
·78 lines (68 loc) · 2.73 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import serial
from time import sleep
from flask import Flask,jsonify
import xml.etree.ElementTree as ET
import json
app = Flask(__name__)
tree = ET.parse('containers.xml')
root = tree.getroot()
tree2 = ET.parse('consumption.xml')
root2 = tree2.getroot()
@app.route("/")
def index():
with open('weights.txt','r') as r:
a = r.readline()
a = a.split(" ")
json_data = []
for container in root.findall('container'):
content = container.find('content').text
iden = container.get('id')
json_data.append({"content": content, "left": a[int(iden)-1]})
return jsonify(json_data)
@app.route("/containers/")
def containers():
json_data = []
for container in root.findall('container'):
iden = container.get('id')
content = container.find('content').text
left = container.find('left').text
json_data.append({"id": iden,"content": content, "left": left})
return jsonify(json_data)
@app.route("/container/<iden>")
def container(iden):
json_data = []
for container in root.findall('.//container[@id="'+iden+'"]'):
iden = container.get('id')
content = container.find('content').text
left = container.find('left').text
saturated_fat = container.find('saturated_fat').text
polyunsaturated_fat = container.find('polyunsaturated_fat').text
monounsaturated_fat = container.find('monounsaturated_fat').text
cholestrol = container.find('cholestrol').text
sodium = container.find('sodium').text
potassium = container.find('potassium').text
carbohydrates = container.find('carbohydrates').text
dietary_fiber = container.find('dietary_fiber').text
sugars = container.find('sugars').text
protein = container.find('protein').text
vitamin_a = container.find('vitamin_a').text
vitamin_c = container.find('vitamin_c').text
calcium = container.find('calcium').text
iron = container.find('iron').text
json_data.append({"id": iden,"content": content, "left": left, "polyunsaturated_fat": polyunsaturated_fat, "monounsaturated_fat": monounsaturated_fat, "cholestrol": cholestrol, "sodium": sodium, "potassium": potassium, "carbohydrates": carbohydrates, "dietary_fiber": dietary_fiber, "sugars": sugars, "protein": protein, "vitamin_a": vitamin_a, "vitamin_c": vitamin_c, "calcium": calcium, "iron": iron})
return jsonify(json_data)
@app.route("/consumption_all/")
def consumption_all():
json_data = []
for container in root2.findall('.//container'):
for left in container:
json_data.append({"timestamp": left.get("at"), "left": left.text})
return jsonify(json_data)
@app.route("/consumption/<iden>")
def consumption(iden):
json_data = []
for container in root2.findall('.//container[@id="'+iden+'"]'):
for left in container:
json_data.append({"timestamp": left.get("at"), "left": left.text})
return jsonify(json_data)
app.run(host= '0.0.0.0')