Skip to content

Commit

Permalink
show avg bmi by country and gender
Browse files Browse the repository at this point in the history
  • Loading branch information
ananya173147 committed Nov 25, 2023
1 parent 1a84b6d commit 0c038c3
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
39 changes: 38 additions & 1 deletion application.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# from apps import App
from flask import json
# from utilities import Utilities
from flask import render_template, session, url_for, flash, redirect, request, Flask
from flask import render_template, session, url_for, flash, redirect, request, Flask, jsonify
from flask_mail import Mail
from flask_pymongo import PyMongo
from tabulate import tabulate
Expand Down Expand Up @@ -972,6 +972,43 @@ def hrx():
return redirect(url_for('dashboard'))
return render_template('hrx.html', title='HRX', form=form)

@app.route('/get_countries', methods=['GET'])
def get_countries():
'''
Populate countries in the bmi_cal.html dropdown from mongodb
'''
countries = [country['country_name'] for country in mongo.db.obesity.find({}, {'country_name': 1, '_id': 0})]
return jsonify({'countries': countries})

@app.route('/get_average_bmi', methods=['GET'])
def get_average_bmi():
'''
Fetch the average bmi values from database
'''
country = request.args.get('country')
gender = request.args.get('gender')

bmi_data_for_country = list(mongo.db.obesity.find({'country_name': country}))

# Get the keys for both sexes, male, and female
gender_keys = ['both_sexes', 'male', 'female']

# Dictionary to store average BMI values
average_bmi_data = {}

# Calculate average BMI for both sexes
both_sexes_data = [entry[key] for entry in bmi_data_for_country for key in gender_keys if key in entry]
total_both_sexes = sum(both_sexes_data)
average_bmi_data['both_sexes'] = total_both_sexes / len(both_sexes_data)

# Calculate average BMI for the selected gender
if gender in gender_keys:
selected_gender_data = [entry[gender] for entry in bmi_data_for_country if gender in entry]
total_selected_gender = sum(selected_gender_data)
average_bmi_data[gender] = total_selected_gender / len(selected_gender_data)
else:
return jsonify({'error': 'Invalid gender parameter'}), 400
return jsonify(average_bmi_data)

# @app.route("/ajaxdashboard", methods=['POST'])
# def ajaxdashboard():
Expand Down
54 changes: 51 additions & 3 deletions templates/bmi_cal.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ <h1>BMI Calculator</h1>

<div class="gender">
<label class="container">
<input type="radio" name="gender" id="male"><p class="text">Male</p>
<input type="radio" name="gender" id="male" value="male"><p class="text">Male</p>
<span class="checkmark"></span>
</label>

<label class="container">
<input type="radio" name="gender" id="female"><p class="text">Female</p>
<input type="radio" name="gender" id="female" value="female"><p class="text">Female</p>
<span class="checkmark"></span>
</label>
</div>
Expand Down Expand Up @@ -68,7 +68,14 @@ <h1>BMI Calculator</h1>
<select id="countrySelect" name="country">
<option value="" disabled selected>Select a country</option>
</select>
</div>
</div>

<div id="averageBMISection" class="result" style="display: none;">
<p><br>Average BMI for <span id="selectedCountry"></span>:</p>
<div id="averageBothSexesValue">0.00</div>
<p>Average BMI for <span id="selectedGender"></span>:</p>
<div id="averageSelectedGenderValue">0.00</div>
</div>

</div>
</div>
Expand All @@ -88,6 +95,42 @@ <h1>BMI Calculator</h1>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>

<script>
function displayAverageBMI(averageBMIBothSexes, averageBMISelectedGender) {
var averageBMISection = $('#averageBMISection');
var selectedCountry = $('#selectedCountry');
var averageBothSexesValue = $('#averageBothSexesValue');
var selectedGender = $('#selectedGender');
var averageSelectedGenderValue = $('#averageSelectedGenderValue');

averageBMISection.show();
selectedCountry.text($('#countrySelect option:selected').text());
averageBothSexesValue.text(averageBMIBothSexes.toFixed(2));
selectedGender.text($('input[name="gender"]:checked').siblings('.text').text());
averageSelectedGenderValue.text(averageBMISelectedGender.toFixed(2));
}

function fetchAndDisplayAverageBMI() {
var selectedCountry = $('#countrySelect').val();
var selectedGender = $('input[name="gender"]:checked').val();

if (selectedCountry && selectedGender) {
$.ajax({
url: '/get_average_bmi',
type: 'GET',
data: {
country: selectedCountry,
gender: selectedGender
},
dataType: 'json',
success: function (data) {
displayAverageBMI(data.both_sexes, data[selectedGender]);
},
error: function (error) {
console.error('Error fetching average BMI:', error);
}
});
}
}

function fetchCountries() {
$.ajax({
Expand All @@ -113,6 +156,11 @@ <h1>BMI Calculator</h1>

$(document).ready(function () {
fetchCountries();
fetchAndDisplayAverageBMI();

$('#countrySelect, input[name="gender"]').change(function () {
fetchAndDisplayAverageBMI();
});
});

function calculateBMI() {
Expand Down

0 comments on commit 0c038c3

Please sign in to comment.