|
| 1 | +import streamlit as st |
| 2 | +import pandas as pd |
| 3 | + |
| 4 | +st.set_page_config(page_title="Macro Tracker App", layout="wide") |
| 5 | + |
| 6 | +st.title("Macronutrient Calculator & Tracker") |
| 7 | + |
| 8 | +# Sidebar for User Profile & Macro Calculation |
| 9 | +st.sidebar.header("User Profile") |
| 10 | +weight = st.sidebar.number_input("Weight (kg)", value=70.0, step=0.5) |
| 11 | +height = st.sidebar.number_input("Height (cm)", value=175.0, step=0.5) |
| 12 | +age = st.sidebar.number_input("Age", value=25, step=1) |
| 13 | +gender = st.sidebar.selectbox("Gender", ["Male", "Female"]) |
| 14 | +activity = st.sidebar.selectbox("Activity Level", ["Sedentary", "Lightly Active", "Moderately Active", "Very Active"]) |
| 15 | +goal = st.sidebar.selectbox("Goal", ["Lose Weight", "Maintain", "Build Muscle"]) |
| 16 | + |
| 17 | +# BMR calculation using Mifflin-St Jeor Equation |
| 18 | +if gender == "Male": |
| 19 | + bmr = (10 * weight) + (6.25 * height) - (5 * age) + 5 |
| 20 | +else: |
| 21 | + bmr = (10 * weight) + (6.25 * height) - (5 * age) - 161 |
| 22 | + |
| 23 | +activity_multipliers = { |
| 24 | + "Sedentary": 1.2, |
| 25 | + "Lightly Active": 1.375, |
| 26 | + "Moderately Active": 1.55, |
| 27 | + "Very Active": 1.725 |
| 28 | +} |
| 29 | + |
| 30 | +tdee = bmr * activity_multipliers[activity] |
| 31 | + |
| 32 | +if goal == "Lose Weight": |
| 33 | + target_calories = tdee - 500 |
| 34 | +elif goal == "Build Muscle": |
| 35 | + target_calories = tdee + 300 |
| 36 | +else: |
| 37 | + target_calories = tdee |
| 38 | + |
| 39 | +# Macro distribution (30% Protein, 40% Carbs, 30% Fat) |
| 40 | +protein_grams = (target_calories * 0.30) / 4 |
| 41 | +carb_grams = (target_calories * 0.40) / 4 |
| 42 | +fat_grams = (target_calories * 0.30) / 9 |
| 43 | + |
| 44 | +st.header("Your Daily Targets") |
| 45 | +col1, col2, col3, col4 = st.columns(4) |
| 46 | +col1.metric("Calories", f"{int(target_calories)} kcal") |
| 47 | +col2.metric("Protein", f"{int(protein_grams)} g") |
| 48 | +col3.metric("Carbs", f"{int(carb_grams)} g") |
| 49 | +col4.metric("Fat", f"{int(fat_grams)} g") |
| 50 | + |
| 51 | +# Food Log Section |
| 52 | +st.markdown("---") |
| 53 | +st.header("Daily Food Log") |
| 54 | + |
| 55 | +if 'food_log' not in st.session_state: |
| 56 | + st.session_state.food_log = pd.DataFrame(columns=["Food Item", "Calories", "Protein (g)", "Carbs (g)", "Fat (g)"]) |
| 57 | + |
| 58 | +with st.form("food_form", clear_on_submit=True): |
| 59 | + col_f1, col_f2, col_f3, col_f4, col_f5 = st.columns(5) |
| 60 | + with col_f1: |
| 61 | + food_name = st.text_input("Food Item") |
| 62 | + with col_f2: |
| 63 | + cals = st.number_input("Calories", min_value=0, value=0) |
| 64 | + with col_f3: |
| 65 | + p = st.number_input("Protein (g)", min_value=0.0, value=0.0) |
| 66 | + with col_f4: |
| 67 | + c = st.number_input("Carbs (g)", min_value=0.0, value=0.0) |
| 68 | + with col_f5: |
| 69 | + f = st.number_input("Fat (g)", min_value=0.0, value=0.0) |
| 70 | + |
| 71 | + submitted = st.form_submit_button("Add Food Item") |
| 72 | + if submitted and food_name: |
| 73 | + new_row = pd.DataFrame([[food_name, cals, p, c, f]], columns=st.session_state.food_log.columns) |
| 74 | + st.session_state.food_log = pd.concat([st.session_state.food_log, new_row], ignore_index=True) |
| 75 | + |
| 76 | +if not st.session_state.food_log.empty: |
| 77 | + st.dataframe(st.session_state.food_log, use_container_width=True) |
| 78 | + |
| 79 | + total_cals = st.session_state.food_log["Calories"].sum() |
| 80 | + total_p = st.session_state.food_log["Protein (g)"].sum() |
| 81 | + total_c = st.session_state.food_log["Carbs (g)"].sum() |
| 82 | + total_f = st.session_state.food_log["Fat (g)"].sum() |
| 83 | + |
| 84 | + st.subheader("Progress Summary") |
| 85 | + st.write(f"Calories: {total_cals} / {int(target_calories)} kcal") |
| 86 | + st.progress(min(total_cals / target_calories, 1.0)) |
| 87 | + |
| 88 | + if st.button("Clear Log"): |
| 89 | + st.session_state.food_log = pd.DataFrame(columns=["Food Item", "Calories", "Protein (g)", "Carbs (g)", "Fat (g)"]) |
| 90 | + st.rerun() |
1 | 91 | .. _installing-packages: |
2 | 92 |
|
3 | 93 | =================== |
|
0 commit comments