Spaces:
Running
Running
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 | |