File size: 626 Bytes
dd5ae0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import requests
import pandas as pd

# Function to fetch nutrition details
def fetch_nutrition(query: str):
    api_url = f'https://api.api-ninjas.com/v1/nutrition?query={query}'
    headers = {'X-Api-Key': 'Hu4DkNyaIFT+E/FfCPRaYw==EUSFTIrXpCdQXrjH'}
    response = requests.get(api_url, headers=headers)

    if response.status_code == requests.codes.ok:
        data = response.json()
        if data:
            df = pd.DataFrame(data)
            # Drop unwanted columns
            df = df.drop(columns=['calories', 'serving_size_g', 'protein_g'], errors='ignore')
            return df
    return None