|
1 | 1 | # spell-checker: disable |
2 | 2 | """ |
3 | | -All Recipes Page for Smart Pantry Application (SQLite version) |
| 3 | +All Recipes Page for Smart Pantry Manager (SQLite version) |
| 4 | +Shows all recipes with diet type info. |
4 | 5 | """ |
5 | 6 |
|
6 | 7 | import ast |
|
11 | 12 | import streamlit as st |
12 | 13 |
|
13 | 14 | st.set_page_config(page_title="All Recipes", page_icon="π", layout="wide") |
14 | | - |
15 | 15 | st.title("π All Recipes") |
16 | | -st.caption("Browse all available recipes in the Smart Pantry system.") |
| 16 | +st.caption("Browse all recipes and see diet type (Vegan/Vegetarian/Non-Vegetarian)") |
17 | 17 |
|
18 | 18 |
|
19 | | -# ---------- Load recipes from SQLite ---------- |
| 19 | +# ---------- Load recipes ---------- |
20 | 20 | @st.cache_data |
21 | 21 | def load_recipes(): |
22 | | - """ |
23 | | - Load recipes from the SQLite database and normalize column names. |
24 | | - Returns a DataFrame with columns: Recipe, Ingredients, Instructions |
25 | | - """ |
26 | | - db_path = "smart_pantry_manager/data/Recipe_Dataset.sqlite" |
27 | | - |
28 | | - # Check if database file exists |
| 22 | + db_path = "smart_pantry_manager/data/cleaned_data.sqlite" |
29 | 23 | if not os.path.exists(db_path): |
30 | | - st.error(f"β Database file not found at: {db_path}") |
31 | | - return pd.DataFrame(columns=["Recipe", "Ingredients", "Instructions"]) |
32 | | - |
33 | | - # Connect to SQLite database |
| 24 | + st.error("β οΈ Recipes database not found.") |
| 25 | + return pd.DataFrame( |
| 26 | + columns=["Recipe", "Ingredients", "Instructions", "Diet_type"] |
| 27 | + ) |
34 | 28 | conn = sqlite3.connect(db_path) |
35 | | - |
36 | 29 | try: |
37 | | - # Load the table "recipes" |
38 | | - df = pd.read_sql_query("SELECT * FROM recipes", conn) |
| 30 | + df = pd.read_sql_query("SELECT * FROM all_recipes", conn) |
39 | 31 | except Exception as e: |
40 | | - st.error(f"Error reading database: {e}") |
| 32 | + st.error(f"Error reading recipes: {e}") |
41 | 33 | conn.close() |
42 | | - return pd.DataFrame(columns=["Recipe", "Ingredients", "Instructions"]) |
43 | | - |
| 34 | + return pd.DataFrame( |
| 35 | + columns=["Recipe", "Ingredients", "Instructions", "Diet_type"] |
| 36 | + ) |
44 | 37 | conn.close() |
45 | | - |
46 | | - # Normalize column names |
| 38 | + # Normalize columns |
47 | 39 | df.columns = [c.strip().lower() for c in df.columns] |
48 | | - |
49 | | - # Rename columns if they exist |
50 | | - rename_map = { |
51 | | - "title": "Recipe", |
52 | | - "cleaned_ingredients": "Ingredients", |
53 | | - "instruction": "Instructions", |
54 | | - "instructions": "Instructions", |
55 | | - } |
56 | 40 | df.rename( |
57 | | - columns={k: v for k, v in rename_map.items() if k in df.columns}, inplace=True |
| 41 | + columns={ |
| 42 | + "title": "Recipe", |
| 43 | + "instruction": "Instructions", |
| 44 | + "instructions": "Instructions", |
| 45 | + "diet_type": "Diet_type", |
| 46 | + "ingredient": "Ingredients", |
| 47 | + "cleaned_ingredients": "Ingredients", |
| 48 | + }, |
| 49 | + inplace=True, |
58 | 50 | ) |
| 51 | + for col in ["Recipe", "Ingredients", "Instructions", "Diet_type"]: |
| 52 | + if col not in df.columns: |
| 53 | + df[col] = "" |
| 54 | + return df[["Recipe", "Ingredients", "Instructions", "Diet_type"]] |
59 | 55 |
|
60 | | - # Keep only required columns |
61 | | - required_cols = ["Recipe", "Ingredients", "Instructions"] |
62 | | - df = df[[col for col in required_cols if col in df.columns]] |
63 | | - |
64 | | - return df |
65 | 56 |
|
66 | | - |
67 | | -def format_ingredients(ingredients_str): |
68 | | - """ |
69 | | - Convert ingredients from string representation of list to a Python list. |
70 | | - """ |
| 57 | +def parse_ingredients(ingredients_str): |
| 58 | + if not ingredients_str: |
| 59 | + return [] |
| 60 | + s = str(ingredients_str) |
71 | 61 | try: |
72 | | - # Try to parse as a Python list |
73 | | - if ingredients_str.startswith("["): |
74 | | - return ast.literal_eval(ingredients_str) |
75 | | - # If it's comma-separated |
76 | | - elif "," in ingredients_str: |
77 | | - return [item.strip() for item in ingredients_str.split(",")] |
78 | | - else: |
79 | | - return [ingredients_str] |
| 62 | + if s.startswith("[") and s.endswith("]"): |
| 63 | + parsed = ast.literal_eval(s) |
| 64 | + return [str(x).strip() for x in parsed if str(x).strip()] |
| 65 | + if "," in s: |
| 66 | + return [x.strip() for x in s.split(",") if x.strip()] |
| 67 | + return [s] |
80 | 68 | except: |
81 | | - # If parsing fails, return as single item list |
82 | | - return [ingredients_str] |
| 69 | + return [s] |
83 | 70 |
|
84 | 71 |
|
85 | 72 | recipes = load_recipes() |
86 | | - |
87 | | -# ---------- Display recipes ---------- |
88 | 73 | if recipes.empty: |
89 | 74 | st.info("No recipes found.") |
90 | | -else: |
91 | | - search = st.text_input("π Search for a recipe:") |
92 | | - filtered = ( |
93 | | - recipes[recipes["Recipe"].str.contains(search, case=False, na=False)] |
94 | | - if search |
95 | | - else recipes |
96 | | - ) |
97 | | - |
98 | | - for _, row in filtered.iterrows(): |
99 | | - with st.expander(f"π {row['Recipe']}"): |
100 | | - st.markdown("**π§ Ingredients:**") |
101 | | - ingredients_list = format_ingredients(row["Ingredients"]) |
102 | | - for ingredient in ingredients_list: |
103 | | - st.write(f"β’ {ingredient}") |
104 | | - |
105 | | - st.markdown("**π©βπ³ Instructions:**") |
106 | | - st.write(row["Instructions"]) |
| 75 | + st.stop() |
| 76 | + |
| 77 | +search = st.text_input("π Search for a recipe:") |
| 78 | +filtered = ( |
| 79 | + recipes[recipes["Recipe"].str.contains(search, case=False, na=False)] |
| 80 | + if search |
| 81 | + else recipes |
| 82 | +) |
| 83 | + |
| 84 | +for _, row in filtered.iterrows(): |
| 85 | + diet = row.get("Diet_type", "Unknown").capitalize() |
| 86 | + recipe_name = row.get("Recipe", "Unnamed Recipe") |
| 87 | + with st.expander(f"π {recipe_name} β {diet}"): |
| 88 | + st.markdown(f"**Diet Type:** {diet}") |
| 89 | + st.markdown("**π§ Ingredients:**") |
| 90 | + ing_list = parse_ingredients(row["Ingredients"]) |
| 91 | + if ing_list: |
| 92 | + for ing in ing_list: |
| 93 | + st.write(f"β’ {ing}") |
| 94 | + else: |
| 95 | + st.write("No ingredient data available.") |
| 96 | + st.markdown("**π©βπ³ Instructions:**") |
| 97 | + st.write(row["Instructions"] or "No instructions available.") |
0 commit comments