BMI_CONVERTER / app.py
amnashoukat's picture
Create app.py
ec7f5d1 verified
import streamlit as st
from fpdf import FPDF
import base64
def calculate_bmi(height_ft, height_in, weight_kg):
total_height_m = (height_ft * 0.3048) + (height_in * 0.0254)
if total_height_m > 0:
bmi = weight_kg / (total_height_m ** 2)
return round(bmi, 2)
else:
return None
def create_pdf(user_name, bmi):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", 'B', 16)
pdf.cell(0, 10, "App by Amna Shoukat", ln=True, align="C")
pdf.ln(10)
pdf.set_font("Arial", '', 14)
pdf.cell(0, 10, f"User Name: {user_name}", ln=True)
pdf.cell(0, 10, f"Your BMI is: {bmi}", ln=True)
return pdf
def download_pdf(pdf):
pdf_output = pdf.output(dest='S').encode('latin1')
b64 = base64.b64encode(pdf_output).decode()
href = f'<a href="data:application/octet-stream;base64,{b64}" download="bmi_report.pdf">Download BMI Report</a>'
st.markdown(href, unsafe_allow_html=True)
def main():
st.title("BMI Converter")
user_name = st.text_input("Enter your name")
height_ft = st.number_input("Enter height (feet)", min_value=0, format="%d")
height_in = st.number_input("Enter height (inches)", min_value=0, format="%d")
weight_kg = st.number_input("Enter weight (kg)", min_value=0.0, format="%f")
if st.button("Calculate BMI"):
bmi = calculate_bmi(height_ft, height_in, weight_kg)
if bmi:
st.success(f"Hello {user_name}, your BMI is: {bmi}")
pdf = create_pdf(user_name, bmi)
download_pdf(pdf)
else:
st.error("Please enter valid height.")
if __name__ == "__main__":
main()