Spaces:
Sleeping
Sleeping
File size: 1,155 Bytes
dd5ae0f |
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 |
import requests
# Function to fetch recipe based on the food query
def fetch_recipe(query: str):
api_url = f'https://api.api-ninjas.com/v1/recipe?query={query}'
headers = {'X-Api-Key': 'Hu4DkNyaIFT+E/FfCPRaYw==EUSFTIrXpCdQXrjH'}
response = requests.get(api_url, headers=headers)
if response.status_code == requests.codes.ok:
recipes = response.json() # Convert the response to JSON
return recipes
return None
# Function to display recipes in a readable format
def display_recipes(recipes):
recipe_text = ""
if recipes:
for recipe in recipes:
recipe_text += f"**Title**: {recipe['title']}\n"
recipe_text += "**Ingredients**:\n"
for ingredient in recipe['ingredients'].split('|'):
recipe_text += f"- {ingredient}\n"
recipe_text += f"**Servings**: {recipe['servings']}\n"
recipe_text += "**Instructions**:\n"
recipe_text += f"{recipe['instructions'][:300]}...\n"
recipe_text += "-" * 40 + "\n"
else:
recipe_text = "No recipes found."
return recipe_text
|