Spaces:
Sleeping
Sleeping
| 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.") | |