File size: 2,703 Bytes
6332c45
9241333
6332c45
 
 
9241333
 
 
 
1bf2768
6332c45
 
 
 
 
1bf2768
9241333
6332c45
9241333
6332c45
1bf2768
9241333
 
 
 
 
 
 
 
 
 
 
 
1bf2768
 
 
 
 
 
 
 
 
 
 
 
 
9241333
1bf2768
9241333
 
1bf2768
 
 
 
 
 
 
 
 
 
 
 
9241333
 
 
1bf2768
9241333
 
 
6332c45
9241333
 
 
 
 
 
 
 
 
1bf2768
9241333
 
 
 
 
 
1bf2768
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import streamlit as st
from fpdf import FPDF

st.title("๐Ÿ‹๏ธโ€โ™‚๏ธ BMI Calculator")

# User Info
name = st.text_input("Enter your name")
user_id = st.text_input("Enter your User ID")

# Height and Weight Inputs
st.subheader("Enter your height:")
feet = st.number_input("Feet", min_value=0, value=5)
inches = st.number_input("Inches", min_value=0, max_value=11, value=7)
weight = st.number_input("Enter your weight (kg)", min_value=0.0, value=70.0)

# BMI Calculation Logic
def calculate_bmi(feet, inches, weight):
    total_inches = (feet * 12) + inches
    height_m = total_inches * 0.0254
    if height_m > 0:
        return weight / (height_m ** 2)
    return None

def get_bmi_category(bmi):
    if bmi < 18.5:
        return "Underweight"
    elif 18.5 <= bmi < 24.9:
        return "Normal weight"
    elif 25 <= bmi < 29.9:
        return "Overweight"
    else:
        return "Obese"

# PDF Generator
class PDF(FPDF):
    def header(self):
        self.set_font("Arial", 'B', 16)
        self.cell(0, 10, "BMI Report", ln=True, align='C')
        self.ln(10)

    def styled_cell(self, label, value):
        self.set_font("Arial", 'B', 12)
        self.cell(50, 10, label, border=0)
        self.set_font("Arial", '', 12)
        self.cell(100, 10, value, ln=True, border=0)

def generate_pdf(name, user_id, height_ft, height_in, weight, bmi, category):
    pdf = PDF()
    pdf.add_page()

    pdf.set_fill_color(240, 240, 240)
    pdf.set_draw_color(180, 180, 180)
    pdf.set_line_width(0.5)
    pdf.rect(10, 30, 190, 100)

    pdf.set_xy(15, 35)
    pdf.styled_cell("Name:", name)
    pdf.styled_cell("User ID:", user_id)
    pdf.styled_cell("Height:", f"{height_ft} ft {height_in} in")
    pdf.styled_cell("Weight:", f"{weight} kg")
    pdf.styled_cell("BMI:", f"{bmi:.2f}")
    pdf.styled_cell("Category:", category)

    return pdf.output(dest='S').encode('latin1')

# BMI Calculation + PDF Download
if st.button("Calculate BMI"):
    if not name or not user_id:
        st.error("Please enter your name and User ID.")
    else:
        bmi = calculate_bmi(feet, inches, weight)
        if bmi:
            category = get_bmi_category(bmi)
            st.success(f"Hello {name} ๐Ÿ‘‹")
            st.success(f"Your BMI is: {bmi:.2f}")
            st.info(f"Category: {category}")

            pdf_bytes = generate_pdf(name, user_id, feet, inches, weight, bmi, category)
            st.download_button(
                label="๐Ÿ“„ Download Styled BMI Report (PDF)",
                data=pdf_bytes,
                file_name=f"{name}_BMI_Report.pdf",
                mime="application/pdf"
            )
        else:
            st.error("Invalid height. Please check your input.")