-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
77 lines (69 loc) · 2.19 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from flask import Flask
import pandas as pd
import hei_loader
import json
app = Flask(__name__)
# This is an example of what score_from_hhid will return after build_json_response
# {
# "hhid": "123",
# "hh_score": 2,
# "items": [{
# "description": "first food",
# "score": 1
# },
# {
# "description": "second food",
# "score": 1
# }
# ]
# }
def build_json_response(df):
response = {}
response['hhnum'] = df['hhnum'].iloc[0].item()
response['hhscore'] = df['hei_score'].sum().item()
items = []
for idx,row in df.iterrows():
mydict = {}
mydict['description'] = row['usdadescmain']
mydict['hei_score'] = row['hei_score']
items.append(mydict)
response['items'] = items
return json.dumps(response)
# returns all info for a household based on householdID. Grabbing random when we loading nutrifhir frontend
@app.route('/score_from_hhnum', methods=['GET'])
def hhid_to_score():
df = hei_loader.get_household_df()
json_response = build_json_response(df)
return json_response
@app.route('/test/<mystr>', methods=['GET'])
def tester(mystr):
return "string is {}".format(mystr)
# takes in UPCcode, returns HEI score.
@app.route('/score_from_upc/<upc>', methods=['GET'])
def get_score_from_upc(upc):
results = {}
upc_df = hei_loader.get_upc_df(upc)
if upc_df.empty:
results['usdadescmain'] = 'invalid food code'
results['hei_score'] = 0
else:
row = upc_df.iloc[0].to_dict()
results['usdadescmain'] = row['usdadescmain']
results['hei_score'] = row['hei_score']
return json.dumps(results)
# takes in foodcode returns HEI score
@app.route('/score_from_foodcode/<foodcode>', methods=['GET'])
def get_score_from_foodcode(foodcode):
results = {}
food_df = hei_loader.get_foodcode_df(float(foodcode))
if food_df.empty:
results['usdadescmain'] = 'invalid food code'
results['hei_score'] = 0
else:
row = food_df.iloc[0].to_dict()
results['usdadescmain'] = row['usdadescmain']
results['hei_score'] = row['hei_score']
return json.dumps(results)
# run the program
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80, debug=True)