Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import matplotlib.pyplot as plt | |
| st.set_page_config(page_title="BMI Calculator", page_icon="βοΈ") | |
| st.title("βοΈ BMI Calculator") | |
| # Gender/age group selector | |
| category = st.selectbox("Select Category", ["Male", "Female", "Child"]) | |
| # Age input | |
| age = st.number_input("Enter your age (in years)", min_value=1, max_value=120, step=1) | |
| # Approximate average height/weight based on age and gender | |
| def get_avg_hw(age, group): | |
| if group == "Child": | |
| if age <= 2: | |
| return (85, 12) | |
| elif age <= 5: | |
| return (105, 17) | |
| elif age <= 10: | |
| return (130, 25) | |
| elif age <= 15: | |
| return (155, 45) | |
| else: | |
| return (160, 50) | |
| elif group == "Male": | |
| if age < 20: | |
| return (170, 60) | |
| else: | |
| return (175, 70) | |
| elif group == "Female": | |
| if age < 20: | |
| return (160, 55) | |
| else: | |
| return (162, 60) | |
| avg_height, avg_weight = get_avg_hw(age, category) | |
| st.info(f"π Average for {category}, Age {age}: Height β {avg_height} cm | Weight β {avg_weight} kg") | |
| # Height input unit toggle | |
| height_unit = st.radio("Choose height input unit", ["Centimeters (cm)", "Feet & Inches"]) | |
| # Get height based on unit | |
| if height_unit == "Centimeters (cm)": | |
| height_cm = st.number_input("Enter your height (in cm)", min_value=50.0, max_value=250.0, step=1.0) | |
| height_m = height_cm / 100 | |
| else: | |
| feet = st.number_input("Feet", min_value=1, max_value=8, step=1) | |
| inches = st.number_input("Inches", min_value=0, max_value=11, step=1) | |
| total_inches = (feet * 12) + inches | |
| height_m = total_inches * 0.0254 # convert inches to meters | |
| # Weight input | |
| weight_kg = st.number_input("Enter your weight (in kg)", min_value=10.0, max_value=300.0, step=0.5) | |
| # Calculate BMI | |
| if height_m > 0 and weight_kg > 0: | |
| bmi = round(weight_kg / (height_m ** 2), 2) | |
| # Determine category | |
| def bmi_category(bmi_val, cat): | |
| if cat == "Child": | |
| if bmi_val < 14: | |
| return "Underweight" | |
| elif bmi_val < 18: | |
| return "Healthy" | |
| elif bmi_val < 20: | |
| return "Overweight" | |
| else: | |
| return "Obese" | |
| else: | |
| if bmi_val < 18.5: | |
| return "Underweight" | |
| elif bmi_val < 24.9: | |
| return "Healthy" | |
| elif bmi_val < 29.9: | |
| return "Overweight" | |
| else: | |
| return "Obese" | |
| category_label = bmi_category(bmi, category) | |
| # Display result | |
| st.success(f"Your BMI is **{bmi}** ({category_label})") | |
| # Graphical representation | |
| st.subheader("π BMI Category Ranges") | |
| fig, ax = plt.subplots(figsize=(8, 2)) | |
| if category == "Child": | |
| thresholds = [14, 18, 20] | |
| labels = ["Underweight", "Healthy", "Overweight", "Obese"] | |
| else: | |
| thresholds = [18.5, 24.9, 29.9] | |
| labels = ["Underweight", "Healthy", "Overweight", "Obese"] | |
| colors = ['lightblue', 'lightgreen', 'orange', 'red'] | |
| ranges = [0] + thresholds + [40] | |
| for i in range(len(labels)): | |
| ax.barh(0, ranges[i+1] - ranges[i], left=ranges[i], color=colors[i], edgecolor='black', label=labels[i]) | |
| ax.axvline(bmi, color='black', linestyle='--', label=f"Your BMI: {bmi}") | |
| ax.set_xlim(0, 40) | |
| ax.set_yticks([]) | |
| ax.set_xlabel("BMI") | |
| ax.legend(loc="upper center", bbox_to_anchor=(0.5, 1.25), ncol=4) | |
| st.pyplot(fig) | |