pec / app.py
TANVEERMAKHDOOM's picture
Update app.py
ef12e47 verified
import streamlit as st
from fpdf import FPDF
# Set page config
st.set_page_config(page_title="BMI Calculator", page_icon="βš–οΈ", layout="centered")
# Apply custom CSS
st.markdown("""
<style>
.main {
background-color: #f0f2f6;
}
.stApp {
font-family: 'Arial', sans-serif;
}
.card {
background-color: white;
padding: 2rem;
border-radius: 1rem;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
</style>
""", unsafe_allow_html=True)
# Title
st.markdown("<h1 style='text-align: center; color: #4CAF50;'>βš–οΈ BMI Calculator</h1>", unsafe_allow_html=True)
st.markdown("<h4 style='text-align: center;'>Calculate your Body Mass Index easily</h4>", unsafe_allow_html=True)
st.markdown("---")
# Card container
with st.container():
st.markdown("<div class='card'>", unsafe_allow_html=True)
# Patient Information
st.subheader("Patient Information")
patient_name = st.text_input("Name of Patient:")
patient_age = st.number_input("Age:", min_value=0, max_value=120, step=1)
patient_gender = st.selectbox("Gender:", ["Select", "Male", "Female", "Other"])
patient_notes = st.text_area("Additional Notes:")
st.markdown("---")
# BMI Calculation Section
st.subheader("Body Measurements")
weight = st.number_input("Weight (in kilograms):", min_value=1.0, format="%.2f")
st.markdown("**Height:**")
col1, col2 = st.columns(2)
with col1:
feet = st.number_input("Feet:", min_value=0, format="%i")
with col2:
inches = st.number_input("Inches:", min_value=0, format="%i")
def calculate_bmi(weight, feet, inches):
total_inches = (feet * 12) + inches
height_meters = total_inches * 0.0254
if height_meters == 0:
return None
bmi = weight / (height_meters ** 2)
return round(bmi, 2)
bmi = calculate_bmi(weight, feet, inches)
if bmi:
st.markdown("---")
st.subheader("Result:")
bmi_status = ""
if bmi < 18.5:
st.success(f"Your BMI is {bmi} β€” Underweight.")
bmi_status = "Underweight"
elif 18.5 <= bmi < 24.9:
st.success(f"Your BMI is {bmi} β€” Normal weight. πŸ‘")
bmi_status = "Normal weight"
elif 25 <= bmi < 29.9:
st.warning(f"Your BMI is {bmi} β€” Overweight.")
bmi_status = "Overweight"
else:
st.error(f"Your BMI is {bmi} β€” Obese.")
bmi_status = "Obese"
else:
st.info("Please enter your height and weight.")
st.markdown("---")
# Function to generate PDF
def create_pdf(name, age, gender, notes, bmi_value, bmi_status):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", 'B', 16)
pdf.cell(0, 10, "BMI Report", ln=True, align="C")
pdf.ln(10)
pdf.set_font("Arial", '', 12)
pdf.cell(0, 10, f"Patient Name: {name}", ln=True)
pdf.cell(0, 10, f"Age: {age}", ln=True)
pdf.cell(0, 10, f"Gender: {gender}", ln=True)
pdf.cell(0, 10, f"BMI: {bmi_value} ({bmi_status})", ln=True)
pdf.ln(5)
pdf.multi_cell(0, 10, f"Notes: {notes}")
return pdf
# Only show download if BMI is calculated
if bmi and patient_name:
pdf = create_pdf(patient_name, patient_age, patient_gender, patient_notes, bmi, bmi_status)
# Save PDF temporarily in memory
import io
pdf_buffer = io.BytesIO()
pdf.output(pdf_buffer)
pdf_buffer.seek(0)
st.download_button(
label="πŸ“„ Download BMI Report as PDF",
data=pdf_buffer,
file_name=f"{patient_name}_BMI_Report.pdf",
mime="application/pdf"
)
st.markdown("</div>", unsafe_allow_html=True)