Skip to content

Commit

Permalink
add bmi calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
ananya173147 committed Nov 17, 2023
1 parent 1cd3d5c commit a025458
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
27 changes: 27 additions & 0 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,33 @@ def friends():
myFriendsList=myFriendsList)


@app.route('/bmi_calc', methods=['GET', 'POST'])
def bmi_calci():
bmi = ''
bmi_category = ''

if request.method == 'POST' and 'weight' in request.form:
weight = float(request.form.get('weight'))
height = float(request.form.get('height'))
bmi = calc_bmi(weight, height)
bmi_category = get_bmi_category(bmi)

return render_template("bmi_cal.html", bmi=bmi, bmi_category=bmi_category)

def calc_bmi(weight, height):
return round((weight / ((height / 100) ** 2)), 2)

def get_bmi_category(bmi):
if bmi < 18.5:
return 'Underweight'
elif bmi < 24.9:
return 'Normal Weight'
elif bmi < 29.9:
return 'Overweight'
else:
return 'Obese'


@app.route("/send_email", methods=['GET', 'POST'])
def send_email():
# ############################
Expand Down
80 changes: 80 additions & 0 deletions templates/bmi_cal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{% extends "layout.html" %}

{% block content %}
<div class="container">
<h1>Your Health, Your BMI</h1>
<div class="main-content">
<p>
Welcome to our BMI Calculator! Body Mass Index (BMI) is a helpful measure
to understand how your weight relates to your height. It's a simple tool
that can give you insights into your health.

To discover your BMI, input your weight (in kilograms) and height (in
centimeters) below:
</p>

<div class="content-section">
<form class="pure-form" method="POST" action="/bmi_calc" id="bmi-form">
<label for="weightInput">Weight in kilograms:</label><br />
<input type="text" name="weight" id="weightInput" placeholder="Enter your weight" required /><br />

<label for="heightInput">Height in centimeters:</label><br />
<input type="text" name="height" id="heightInput" placeholder="Enter your height" required /><br />
<br>
<button type="button" class="pure-button pure-button-primary" onclick="calculateBMI()">
Calculate BMI
</button>
</form>
</div>

<div id="resultSection" style="display: none;">
<p style="text-align: center" class="display-4">
Your BMI is <span id="bmiResult"></span>.
</p>
<p style="text-align: center" class="display-6">You fall into the category of <span id="bmiCategory"></span></p>
</div>
</div>
</div>

<script>
function calculateBMI() {
var weight = document.getElementById("weightInput").value;
var height = document.getElementById("heightInput").value;

// Validate inputs
if (!isNumeric(weight) || !isNumeric(height)) {
alert("Please enter valid numeric values for weight and height.");
return;
}

// Perform BMI calculation here and set the results
var bmi = calculateBMIValue(weight, height);
var bmiCategory = determineBMICategory(bmi);

// Display the result section
document.getElementById("bmiResult").innerText = bmi;
document.getElementById("bmiCategory").innerText = bmiCategory;
document.getElementById("resultSection").style.display = "block";
}

function isNumeric(value) {
return !isNaN(parseFloat(value)) && isFinite(value);
}

function calculateBMIValue(weight, height) {
return (weight / (height / 100) ** 2).toFixed(2);
}

function determineBMICategory(bmi) {
if (bmi < 18.5) {
return "Underweight";
} else if (bmi < 24.9) {
return "Normal Weight";
} else if (bmi < 29.9) {
return "Overweight";
} else {
return "Obese";
}
}
</script>
{% endblock content %}
1 change: 1 addition & 0 deletions templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<a class="nav-item nav-link" href="{{ url_for('calories') }}">Enter Calories</a>
<a class="nav-item nav-link" href="/history">Your plan</a>
<a class="nav-item nav-link" href="/workout">Burnout</a>
<a class="nav-item nav-link" href="{{url_for('bmi_calci')}}">BMI Calculator</a>
<a class="nav-item nav-link" href="{{url_for('friends')}}">Friends</a>
<a class="nav-item nav-link btn btn-link" href="{{ url_for('user_profile') }}">Profile</button>
</div>
Expand Down

0 comments on commit a025458

Please sign in to comment.