-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.py
149 lines (117 loc) · 4.38 KB
/
project.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import requests
import streamlit as st
import re
def main():
st.set_page_config(layout='wide')
page_title = st.title("The Web Cookbook")
query = st.text_input("Search for a recipe")
col1, col2 = st.columns(2)
with col1:
intolerances = st.multiselect("Intolerances", [
"Dairy",
"Egg",
"Gluten",
"Grain",
"Peanut",
"Seafood",
"Sesame",
"Soy",
"Wheat"
])
with col2:
diet = st.selectbox("Diet", [
"No Specific Diet",
"Gluten Free",
"Ketogenic",
"Vegetarian",
"Lacto-Vegetarian",
"Ovo-Vegetarian",
"Vegan",
"Pescetarian",
"Paleo",
"Primal",
"Low FODMAP",
"Whole30"
])
data = None
if query:
try:
data = get_recipe(query=query, diet=diet, intolerances=intolerances)
except Exception as e:
st.error(f"An error occurred: {e}")
else:
pass
# Display retrieved data if available
if data and "results" in data:
recipes = data["results"][:9]
# Create columns to display data
cols = st.columns(3)
col3, col4 = st.columns(2)
for _, recipe in enumerate(recipes):
title = recipe["title"]
cut_title = recipe["title"][:32] + "..." if len(recipe["title"]) > 32 else recipe["title"]
recipe_id = recipe["id"]
with cols[_ % 3]:
st.image(recipe["image"])
st.text(cut_title)
if st.button(label="View Ingredients" + " "*_):
ingredients = get_ingredients(recipe_id)
instructions = get_instructions(recipe_id)
with col3: # Ingredients
st.subheader(f"Ingredients for {title}")
for __, ingredient in enumerate(ingredients["ingredients"]):
name = ingredient["name"]
amount = ingredient["amount"]["metric"]["value"]
unit = ingredient["amount"]["metric"]["unit"]
__ += 1
st.markdown(f"**{__}**. **{name}** - {amount}{unit.lower()}")
with col4: # Instructions
st.subheader(f"Instructions for {title}")
instructions = instructions[0]["steps"]
for step in instructions:
number = step["number"]
description = re.sub(r'\.(?!\s|$)', '. ', step["step"]) #Regular expression to format the description
st.markdown(f"**{number}**. {description}")
def get_recipe(query="", cuisine="", diet="", type="", intolerances="", number=9):
key = "4f088b97d9a3497a83eecf3e6f984805"
URL = "https://api.spoonacular.com/recipes/complexSearch"
if diet == "No Specific Diet":
diet = ""
params = {
"cuisine": cuisine.title(),
"diet": diet,
"type": type,
"intolerances": intolerances,
"number": number,
"query": query,
"apiKey": key
}
try:
response = requests.get(URL, params=params)
return response.json()
except requests.RequestException as e:
return f"Request failed: {str(e)}"
def get_ingredients(recipe_id, system="metric"):
key = "4f088b97d9a3497a83eecf3e6f984805"
URL = f"https://api.spoonacular.com/recipes/{recipe_id}/ingredientWidget.json"
params = {"apiKey": key}
try:
response = requests.get(URL, params=params)
return response.json()
except requests.RequestException as e:
return f"Request failed: {str(e)}"
def get_instructions(recipe_id):
key = "4f088b97d9a3497a83eecf3e6f984805"
URL = f"https://api.spoonacular.com/recipes/{recipe_id}/analyzedInstructions"
params = {"id": int(recipe_id), "apiKey": key}
try:
response = requests.get(URL, params=params)
return response.json()
except requests.RequestException as e:
return f"Request failed: {str(e)}"
if __name__ == "__main__":
main()
#4f088b97d9a3497a83eecf3e6f984805 - Aymen
#9815d21b945e4546bc171dba4b266adb - NizarAlt
#c90b4f27ba294c868fc10585d175526d - Uniflare
#1923d24e46514b2b802f73f6e41626f4 - Original