Spaces:
Sleeping
Sleeping
Create bmi-cal.py
Browse files- bmi-cal.py +48 -0
bmi-cal.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
|
| 6 |
+
# Function to calculate BMI
|
| 7 |
+
def calculate_bmi(weight, height):
|
| 8 |
+
return weight / ((height / 100) ** 2)
|
| 9 |
+
|
| 10 |
+
# Function to get BMI category
|
| 11 |
+
def get_bmi_category(bmi):
|
| 12 |
+
if bmi < 16:
|
| 13 |
+
return "Severe Thinness", "#FF0000"
|
| 14 |
+
elif 16 <= bmi < 17:
|
| 15 |
+
return "Moderate Thinness", "#FF4500"
|
| 16 |
+
elif 17 <= bmi < 18.5:
|
| 17 |
+
return "Mild Thinness", "#FF8C00"
|
| 18 |
+
elif 18.5 <= bmi < 25:
|
| 19 |
+
return "Normal", "#008000"
|
| 20 |
+
elif 25 <= bmi < 30:
|
| 21 |
+
return "Overweight", "#FFFF00"
|
| 22 |
+
elif 30 <= bmi < 35:
|
| 23 |
+
return "Obese Class I", "#FFA500"
|
| 24 |
+
elif 35 <= bmi < 40:
|
| 25 |
+
return "Obese Class II", "#FF6347"
|
| 26 |
+
else:
|
| 27 |
+
return "Obese Class III", "#FF0000"
|
| 28 |
+
|
| 29 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 30 |
+
def bmi_calculator():
|
| 31 |
+
bmi = None
|
| 32 |
+
category = None
|
| 33 |
+
color = None
|
| 34 |
+
|
| 35 |
+
if request.method == 'POST':
|
| 36 |
+
height = float(request.form.get('height', 0))
|
| 37 |
+
weight = float(request.form.get('weight', 0))
|
| 38 |
+
|
| 39 |
+
if height > 0:
|
| 40 |
+
bmi = calculate_bmi(weight, height)
|
| 41 |
+
category, color = get_bmi_category(bmi)
|
| 42 |
+
else:
|
| 43 |
+
error = "Height must be greater than zero."
|
| 44 |
+
|
| 45 |
+
return render_template('bmi_calculator.html', bmi=bmi, category=category, color=color)
|
| 46 |
+
|
| 47 |
+
if __name__ == '__main__':
|
| 48 |
+
app.run(host='0.0.0.0', port=5000, debug=True)
|