-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
69 lines (58 loc) · 2.27 KB
/
app.py
File metadata and controls
69 lines (58 loc) · 2.27 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from flask import Flask, request, jsonify, render_template
import tdee
app = Flask(__name__)
app.json.sort_keys = False
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
return render_template('index.html', activity_levels=tdee.Data.ACTIVITY_LEVELS.label.to_list())
payload = request.json
weight = payload.get('weight')
weight_unit = payload.get('weight_unit')
height = payload.get('height')
height_unit = payload.get('height_unit')
age = payload.get('age')
sex = payload.get('sex')
activityLevel = payload.get('activityLevel')
result = []
if not weight:
result.append({'Error(s)': 'Enter weight.'})
if not height:
result.append({'Error(s)': 'Enter height.'})
if not age:
result.append({'Error(s)': 'Enter age.'})
if result:
return result
params = dict(
weight=float(weight),
weight_unit=weight_unit,
height=float(height),
height_unit=height_unit,
age=float(age),
sex=sex,
activity_level=activityLevel,
)
scenarios_with_modified_params = {
'As entered': params,
'After 10 lb loss': tdee.change_weight(**params, weight_change=-10),
'After 20 lb loss': tdee.change_weight(**params, weight_change=-20),
'After 10 lb gain': tdee.change_weight(**params, weight_change=10),
'After 20 lb gain': tdee.change_weight(**params, weight_change=20),
'If 6 in taller': tdee.change_height(**params, height_change=6),
'If 6 in shorter': tdee.change_height(**params, height_change=-6),
'If 10y younger': tdee.change_age(**params, age_change=-10),
'If 10y older': tdee.change_age(**params, age_change=10),
'If other sex': tdee.change_sex(**params),
}
actual_bmr, actual_tdee = tdee.calculate_bmr_and_tdee(**params)
for scenario_label, modified_params in scenarios_with_modified_params.items():
scenario_bmr, scenario_tdee = tdee.calculate_bmr_and_tdee(**modified_params)
result.extend([{
'Scenario': scenario_label,
'BMR': scenario_bmr,
'TDEE': scenario_tdee,
'ΔTDEE': scenario_tdee - actual_tdee,
}])
return jsonify(result)
if __name__ == '__main__':
app.run()