grocery / app.py
engralimalik's picture
Create app.py
017b4e8 verified
import streamlit as st
import numpy as np
# Function to generate the grocery list
def generate_grocery_list(family_details, budget, region, preferences):
grocery_list = []
price_estimates = {
"Rice (1kg)": 2.5,
"Flour (1kg)": 1.2,
"Milk (1L)": 1.5,
"Eggs (12)": 2.0,
"Chicken (1kg)": 5.0,
"Beef (1kg)": 8.0,
"Vegetables (1kg)": 3.0,
"Fruits (1kg)": 4.0,
"Bread (loaf)": 1.0,
"Cooking Oil (1L)": 3.5,
"Sugar (1kg)": 1.8,
"Salt (1kg)": 0.5
}
# Estimate quantity based on family members and age groups
rice_needed = family_details['adults'] * 5 + family_details['children_under_5'] * 2 + family_details['children_5_to_12'] * 4
flour_needed = family_details['adults'] * 4 + family_details['children_under_5'] * 2 + family_details['children_5_to_12'] * 3
milk_needed = family_details['adults'] * 10 + family_details['children_under_5'] * 8 + family_details['children_5_to_12'] * 7
eggs_needed = family_details['adults'] * 12 + family_details['children_5_to_12'] * 8
chicken_needed = family_details['adults'] * 4 + family_details['children_5_to_12'] * 2
# Add the items to the grocery list
grocery_list.append(f"Rice (kg): {rice_needed} kg")
grocery_list.append(f"Flour (kg): {flour_needed} kg")
grocery_list.append(f"Milk (L): {milk_needed} L")
grocery_list.append(f"Eggs: {eggs_needed} dozen")
grocery_list.append(f"Chicken (kg): {chicken_needed} kg")
# Calculate the total estimated price
total_cost = (rice_needed * price_estimates["Rice (1kg)"] +
flour_needed * price_estimates["Flour (1kg)"] +
milk_needed * price_estimates["Milk (1L)"] +
eggs_needed * price_estimates["Eggs (12)"] +
chicken_needed * price_estimates["Chicken (1kg)"])
# Ensure the list fits within the budget
if total_cost > budget:
grocery_list.append(f"Warning: Your budget of ${budget} is insufficient. Total cost is ${total_cost:.2f}.")
else:
grocery_list.append(f"Total estimated cost: ${total_cost:.2f}")
return grocery_list
# Streamlit App UI
st.title("Family Grocery List Generator")
st.markdown("""
**Create a monthly grocery list based on your family size, preferences, and budget.**
""")
# Collecting user inputs
st.sidebar.header("Enter Your Family Details")
# Number of family members in different age groups
num_adults = st.sidebar.number_input("Number of Adults (Age 18+)", min_value=0, value=2)
num_children_under_5 = st.sidebar.number_input("Number of Children under 5", min_value=0, value=1)
num_children_5_to_12 = st.sidebar.number_input("Number of Children (5-12 years)", min_value=0, value=1)
num_teenagers = st.sidebar.number_input("Number of Teenagers (13-18 years)", min_value=0, value=1)
# Family preferences
food_preferences = st.sidebar.selectbox("Food Preferences", ["Halal", "Vegetarian", "Non-Vegetarian"])
region = st.sidebar.selectbox("Region", ["South Asia", "Middle East", "North America", "Europe"])
# Budget
budget = st.sidebar.number_input("Monthly Grocery Budget ($)", min_value=50, value=150)
# Family Details Dictionary
family_details = {
'adults': num_adults,
'children_under_5': num_children_under_5,
'children_5_to_12': num_children_5_to_12,
'teenagers': num_teenagers
}
# Button to generate grocery list
if st.sidebar.button("Generate Grocery List"):
# Generate the list based on user inputs
grocery_list = generate_grocery_list(family_details, budget, region, food_preferences)
# Display the grocery list
st.subheader("Your Monthly Grocery List:")
for item in grocery_list:
st.write(f"- {item}")
# Prepare the quotation text
grocery_list_text = "\n".join(grocery_list)
# Provide a download button for the list
st.download_button(
label="Download Grocery List",
data=grocery_list_text.encode('utf-8'),
file_name="monthly_grocery_list.txt",
mime="text/plain"
)
# About Section
st.sidebar.markdown("""
## About the App
This app helps you create a monthly grocery list based on the size and preferences of your family.
You can input the number of adults, children, teenagers, and specify food preferences like Halal, Vegetarian, or Non-Vegetarian.
The app then generates a grocery list along with estimated prices and compares it to your budget.
""")