Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Nutritional-Facts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Nutritional Facts
Use this script to retrieve the nutritional information of your favorite food items!

## Setup instructions
### Packages
- Install all required packages using this `pip install -r requirements.txt`.

### API Key
- Before you run the script you will need to receive a CalorieNinja API Key.
- To do this got to the [CalorieNinja](https://calorieninjas.com/register) website and create an account, which is completely free.
- Once you have made an account you can receive a free API-Key by navigating through the tabs and generating one.
- Once you have received an API Key you may paste it into the code here `headers={"X-API-key": "YOUR-API-KEY"}`.

## Output
Here is an example use case of me using the script to find out the nutrition of chicken.

![Chicken example](image.png)

You can also be more specific

![Chicken Drumsticks example](image-1.png)

You can also do multiple food items at once

![Multiple food items example](image-2.png)

## Author(s)

Adithya Bollu

Binary file added Nutritional-Facts/image-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Nutritional-Facts/image-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Nutritional-Facts/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 89 additions & 0 deletions Nutritional-Facts/nutrition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import requests
'''
Nutritional Info Script

The script will be able to retrieve the nutrional facts about foods
using the CalorieNinjas API. Users can enter a food query and receive detailed
nutrional facts like calories, protein, fat etc.
'''


# Function to get the nutrition info of the food item
# Using the calorie ninja api

def get_nutrition_info(food: str):
'''
Retrieves nutritional information of food item,
Args:
food (str): Name of food (apple, chucken)
Returns:
list[dict] | None: A list of nutritional fact dictionaries if found otherwise None
'''

# API Call URL
api_url = f"https://api.calorieninjas.com/v1/nutrition?query={food}"

# Request call to send to API,
# Takes in the api url, headers with api-key and a timeout
# so the request doesn't run forever
response = requests.get(api_url, headers={"X-API-key": "YOUR-API-KEY"}, timeout=30)

# Checking if the request was a 200 or an error.
if response.status_code == requests.codes.ok:
data = response.json()
return data["items"]

# Prints error if response was bad
print("Error:", response.status_code, response.text )
return None


def print_info(facts : dict):
'''
Prints the nutritional information in a legible format

Args:
facts (dict): A dictionary of nutrition facts for a single food item
'''

# prints items in a hierarchical format.
for fact in facts:
# Do not indent name, indent everything else
if fact == "name":
print(f"{fact}: {facts[fact]}")
else:
print(f"\t{fact}: {facts[fact]}")



def main():
'''
Main loop, for the nutrition script
Infintely prompts user for food queries
until user quits
'''

# main loop
while True:
# Print opening message
print("Enter food query: ", end="")
query = input()

# Lower casing the query to normalize the checks
if query.lower() == "q" or query.lower() == "quit":
print("Thank you for using the Nutrition script!")
break

# same thing here
items = get_nutrition_info(query.lower())
# Checks if the query entered was invalid or valid
if not items:
print("Please try another query!\n")
else:
# Looping through each item to print them out in neat format
for item in items:
print_info(item)
print("\n")

if __name__ == "__main__":
main()
30 changes: 30 additions & 0 deletions Nutritional-Facts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
annotated-types==0.7.0
anyio==4.9.0
blinker==1.9.0
certifi==2025.4.26
charset-normalizer==3.4.3
click==8.1.8
colorama==0.4.6
distro==1.9.0
dotenv==0.9.9
Flask==3.1.0
flask-cors==5.0.1
h11==0.16.0
httpcore==1.0.9
httpx==0.28.1
idna==3.10
itsdangerous==2.2.0
Jinja2==3.1.6
jiter==0.9.0
MarkupSafe==3.0.2
openai==1.78.0
pydantic==2.11.4
pydantic_core==2.33.2
python-dotenv==1.1.0
requests==2.32.5
sniffio==1.3.1
tqdm==4.67.1
typing-inspection==0.4.0
typing_extensions==4.13.2
urllib3==2.5.0
Werkzeug==3.1.3