Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from fpdf import FPDF
|
3 |
+
import base64
|
4 |
+
|
5 |
+
def calculate_bmi(height_ft, height_in, weight_kg):
|
6 |
+
total_height_m = (height_ft * 0.3048) + (height_in * 0.0254)
|
7 |
+
if total_height_m > 0:
|
8 |
+
bmi = weight_kg / (total_height_m ** 2)
|
9 |
+
return round(bmi, 2)
|
10 |
+
else:
|
11 |
+
return None
|
12 |
+
|
13 |
+
def create_pdf(user_name, bmi):
|
14 |
+
pdf = FPDF()
|
15 |
+
pdf.add_page()
|
16 |
+
pdf.set_font("Arial", 'B', 16)
|
17 |
+
pdf.cell(0, 10, "App by Amna Shoukat", ln=True, align="C")
|
18 |
+
pdf.ln(10)
|
19 |
+
pdf.set_font("Arial", '', 14)
|
20 |
+
pdf.cell(0, 10, f"User Name: {user_name}", ln=True)
|
21 |
+
pdf.cell(0, 10, f"Your BMI is: {bmi}", ln=True)
|
22 |
+
return pdf
|
23 |
+
|
24 |
+
def download_pdf(pdf):
|
25 |
+
pdf_output = pdf.output(dest='S').encode('latin1')
|
26 |
+
b64 = base64.b64encode(pdf_output).decode()
|
27 |
+
href = f'<a href="data:application/octet-stream;base64,{b64}" download="bmi_report.pdf">Download BMI Report</a>'
|
28 |
+
st.markdown(href, unsafe_allow_html=True)
|
29 |
+
|
30 |
+
def main():
|
31 |
+
st.title("BMI Converter")
|
32 |
+
|
33 |
+
user_name = st.text_input("Enter your name")
|
34 |
+
height_ft = st.number_input("Enter height (feet)", min_value=0, format="%d")
|
35 |
+
height_in = st.number_input("Enter height (inches)", min_value=0, format="%d")
|
36 |
+
weight_kg = st.number_input("Enter weight (kg)", min_value=0.0, format="%f")
|
37 |
+
|
38 |
+
if st.button("Calculate BMI"):
|
39 |
+
bmi = calculate_bmi(height_ft, height_in, weight_kg)
|
40 |
+
if bmi:
|
41 |
+
st.success(f"Hello {user_name}, your BMI is: {bmi}")
|
42 |
+
pdf = create_pdf(user_name, bmi)
|
43 |
+
download_pdf(pdf)
|
44 |
+
else:
|
45 |
+
st.error("Please enter valid height.")
|
46 |
+
|
47 |
+
if __name__ == "__main__":
|
48 |
+
main()
|