Python Calorie Counting Script
Python Calorie Counting Script
I made this
script to track my calories without the use of a paid calorie tracking app such as
MyFitnessPal. You can edit it however you want with any ingredient you so choose, and I will
show you how you can use this script with absolutely zero coding knowledge. You can also
accurately count the calories in meals that you want to make. You must note that every
thing needs to be measured out carefully in grams, even including those items that seem
they have no nutritional value must be included in your recipes, for when you go to divide
your meal up in grams the weight of all items matter, unless its something very miniscule
like cilantro. This script will take a little bit to get used to it, but I have it setup in such a way
that you make any additions to it that you want when it comes to adding ingredients and
recipes. It is a little time consuming having to set up the recipes and ingredients and
weighing everything up but it beats having to pay for MyfitnessPal. I like to use nutritionix to
find nutrition facts for ingredients.
First for those of us with zero coding knowledge, that’s okay, use can go to google colab and
use their free python coding interface. Just copy and paste my script into the +code section
and hit the run button.
For Example, you want to add an ingredient, bok choy to your “Define Ingredients” section,
you would enter something like this:
This means that for every 170 grams of bok choy you will find 20 calories, 1.7g of fiber 3g of
carbs, etc.
Then, when you create a meal with bok choy in it for example a beef stew you would include
the ingredient in your beef stew recipe, with the weight of bok choy that went into your
recipe:
Beef_stew = Recipe()
Beef_stew.add_ingredient(chuck_roast, 1159)
Beef_stew.add_ingredient(bok_choy, 186)
Beef_stew.add_ingredient(onion, 207)
Beef_stew.add_ingredient(mushroom, 330)
Beef_stew.add_ingredient(carrot, 306)
Beef_stew.add_ingredient(salt, 13)
Now, when I run the script I have to pay close attention to the single serving size of my
meal. I have the script set up in such a way that I can see what the total serving size (in
grams) is when I enter a 1 into the serving size:
single_serving_size_Beef_stew = 1
nutrition_single_serving_Beef_stew = {}
And now below I have the total nutrition facts for my beef stew, FYI you have to be careful
when adding water or broth to a recipe such as this and include that weight, for this
particular example I cooked it in a pressure cooker with no added liquid so I can safely
assume that this is relatively accurate, although not perfect, it works for my general sense
of accuracy, other recipes can have a greater accuracy for example those without
evaporating liquid. But you can always increase accuracy since you already have the
weights of your ingredients and liquid, you can always weigh the whole meal after cooking
and subtract your post cooked weight from your precooked weight and make that
subtraction to your liquid for true accuracy you should keep in mind that when broth
evaporates you are only losing water and not necessarily the broth since the broth simply
concentrates and does not necessarily lose its nutritional value, so when using broth you
must have a section for water(even if you didn’t use any) for when you go to subtract the
evaporated liquid from the total weight of the recipe:
Nutrition per Single Serving for Beef Stew (total serving size/1):
And now I look at the carbs, fat, and protein of this meal and I say, okay what is my “limiting
agent” I want, e.g. how much protein do I want to consume per meal, with each serving of
this stew I want to consume 30 grams of protein, so I would divide the total protein by the
amount of protein I want in a serving (299.49/30)~ 10 servings. Therefore, I would enter a 10
in the place where there was once a 1:
single_serving_size_Beef_stew = 10
nutrition_single_serving_Beef_stew = {}
I run the script again and I receive the nutrition facts for one serving of beef stew when it is
divided into 10 servings:
Nutrition per Single Serving for Beef Stew (total serving size/10):
Now I can see that when I go to divide the servings I will be measuring out 220 grams of this
stew, obviously not perfect since some servings will get more meat than others, but it still
gives you a good idea of how you can use this script to build a complete meal plan with
recipes as well as single ingredients.
Now at the bottom of the script you will notice some more fancy features such as:
nutrition_per_single_serving = {}
print(f"{key}: {value}")
You never have to do anything with this, it is just calling a function that allows me to print
single serving sizes of any recipe. Below that is a section that allows me to show what I
want to be printed:
#######################
# Example usage: Calculate nutrition per single serving for any recipe
print_nutrition_per_single_serving("Ceasar Dressing", nutrition_info_Ceasar_Dressing,
single_serving_size_Ceasar_Dressing)
print_nutrition_per_single_serving("salmon", nutrition_info_salmon,
single_serving_size_salmon)
print_nutrition_per_single_serving("guacamole", nutrition_info_guacamole,
single_serving_size_guacamole)
print_nutrition_per_single_serving("platanos", nutrition_info_platanos,
single_serving_size_platanos)
#############################
As of right now this will print all of these recipes and show me what the nutrition facts are
for one of my already decided single serving sizes. Now, for example, if I only want to see
the nutrition facts for ceasar dressing, beef stew, and beef stroganoff, I would put a “#”
symbol in front of everything else:
#######################
# Example usage: Calculate nutrition per single serving for any recipe
#print_nutrition_per_single_serving("salmon", nutrition_info_salmon,
single_serving_size_salmon)
#print_nutrition_per_single_serving("guacamole", nutrition_info_guacamole,
single_serving_size_guacamole)
#print_nutrition_per_single_serving("platanos", nutrition_info_platanos,
single_serving_size_platanos)
#############################
Now when I print I will only see the nutrition facts for those three recipes.
Now, if I want to see what the daily nutrition facts would be for the day, if I were to eat a
single serving of platanos, beef stew, and bacon egg salad I would have this:
total_nutrition_day = {}
Adding or subtracting meals for your day would be as easy as copy pasting +
nutrition_single_serving_nut_pouch[key] if we wanted to add a serving of nut pouch for the
day and the resulting script and ouput would be as follows:
total_nutrition_day = {}
total_nutrition_day[key] = nutrition_single_serving_platanos[key] +
nutrition_single_serving_Beef_stew[key] + nutrition_single_serving_bacon_egg_salad[key]
+ nutrition_single_serving_nut_pouch[key]
I have already done quite a bit of work for you in entering some ingredients and recipes but
it is up to you to customize this to fit the ingredients you cook with as well as enter the
recipes that you eat. It’s a simple but powerful script in calorie counting. Good luck!
class CustomIngredient:
def __init__(self, name, serving_size, calories, fiber, carbs, fat, protein, sugar, sodium):
self.name = name
self.serving_size = serving_size
self.calories = calories
self.fiber = fiber
self.carbs = carbs
self.fat = fat
self.protein = protein
self.sugar = sugar
self.sodium = sodium
class Recipe:
def __init__(self):
self.ingredients = []
self.ingredients.append((ingredient, amount))
def get_nutrition(self):
total_serving_size = 0
total_calories = 0
total_fiber = 0
total_carbs = 0
total_fat = 0
total_protein = 0
total_sugar = 0
total_sodium = 0
total_serving_size += amount
return {
# Define ingredients
plantains = CustomIngredient("plantains", 240, 278, 5.5, 75, .4, 1.9, 34, 12)
egg_yolk = CustomIngredient("egg yolk", 17, 55, 0, .6, 4.5, 2.7, .1, 8.2)
lemon_juice = CustomIngredient("lemon juice", 122, 27, 0.4, 8.5, .3, .4, 3.1, 1.2)
#Define Recipes
Ceasar_Dressing = Recipe()
Ceasar_Dressing.add_ingredient(dijon, 66)
Ceasar_Dressing.add_ingredient(avocado_oil, 457)
Ceasar_Dressing.add_ingredient(sherry_wine_vinegar, 70)
Ceasar_Dressing.add_ingredient(egg_yolk, 108)
Ceasar_Dressing.add_ingredient(garlic, 29)
Ceasar_Dressing.add_ingredient(anchovie, 51)
Ceasar_Dressing.add_ingredient(tobasco, 5)
Ceasar_Dressing.add_ingredient(worchestershire, 72)
Ceasar_Dressing.add_ingredient(lemon_juice, 159)
Beef_stew = Recipe()
Beef_stew.add_ingredient(chuck_roast, 1159)
Beef_stew.add_ingredient(bok_choy, 186)
Beef_stew.add_ingredient(onion, 207)
Beef_stew.add_ingredient(mushroom, 330)
Beef_stew.add_ingredient(carrot, 306)
Beef_stew.add_ingredient(salt, 13)
beef_stroganof = Recipe()
beef_stroganof.add_ingredient(onion, 92)
beef_stroganof.add_ingredient(coconut_aminos, 74.4)
beef_stroganof.add_ingredient(red_wine_vinegar, 60)
beef_stroganof.add_ingredient(stew_meat, 681)
beef_stroganof.add_ingredient(mushroom, 100)
beef_stroganof.add_ingredient(carrot, 100)
beef_stroganof.add_ingredient(coconut_milk, 66)
beef_stroganof.add_ingredient(arrowroot, 27)
beef_stroganof.add_ingredient(water, 296)
pan_chicken = Recipe()
pan_chicken.add_ingredient(olive_oil, 64)
pan_chicken.add_ingredient(bacon, 340)
pan_chicken.add_ingredient(chicken_breast, 1192)
bacon_egg_salad = Recipe()
bacon_egg_salad.add_ingredient(olive_oil, 160)
bacon_egg_salad.add_ingredient(bacon, 340)
bacon_egg_salad.add_ingredient(eggs, 1150)
salmon = Recipe()
salmon.add_ingredient(Salmon, 114)
guacamole = Recipe()
guacamole.add_ingredient(onion, 92)
guacamole.add_ingredient(avocado, 593)
guacamole.add_ingredient(tomato, 378)
guacamole.add_ingredient(jalapeno, 120)
platanos = Recipe()
platanos.add_ingredient(plantains, 834)
platanos.add_ingredient(coconut_oil, 180)
nut_pouch = Recipe()
nut_pouch.add_ingredient(cashews, 28)
nut_pouch.add_ingredient(pecans, 28)
nut_pouch.add_ingredient(walnuts, 28)
nut_pouch.add_ingredient(almonds, 28)
nut_pouch.add_ingredient(pistachios, 28)
sweet_potatoe_mash = Recipe()
sweet_potatoe_mash.add_ingredient(sweet_potatoes, 2270)
sweet_potatoe_mash.add_ingredient(coconut_milk, 165)
sweet_potatoe_mash.add_ingredient(salt, 10)
nutrition_info_Ceasar_Dressing = Ceasar_Dressing.get_nutrition()
#print(f"{key}: {value}")
single_serving_size_Ceasar_Dressing = 20
nutrition_single_serving_Ceasar_Dressing = {}
nutrition_single_serving_Ceasar_Dressing[key] = value /
single_serving_size_Ceasar_Dressing
nutrition_info_Beef_stew = Beef_stew.get_nutrition()
#print(f"{key}: {value}")
single_serving_size_Beef_stew = 10
nutrition_single_serving_Beef_stew = {}
nutrition_info_beef_stroganof = beef_stroganof.get_nutrition()
#print(f"{key}: {value}")
single_serving_size_beef_stroganof = 8
nutrition_single_serving_beef_stroganof = {}
nutrition_single_serving_beef_stroganof[key] = value /
single_serving_size_beef_stroganof
nutrition_info_salmon = salmon.get_nutrition()
#print(f"{key}: {value}")
single_serving_size_salmon = 1
nutrition_single_serving_salmon = {}
nutrition_info_guacamole = guacamole.get_nutrition()
#print(f"{key}: {value}")
single_serving_size_guacamole = 4
nutrition_single_serving_guacamole = {}
nutrition_info_platanos = platanos.get_nutrition()
#print(f"{key}: {value}")
single_serving_size_platanos = 29
nutrition_single_serving_platanos = {}
nutrition_info_pan_chicken = pan_chicken.get_nutrition()
#print("\nNutrition Facts for Pan Chicken:")
#print(f"{key}: {value}")
single_serving_size_pan_chicken = 12
nutrition_single_serving_pan_chicken = {}
nutrition_info_bacon_egg_salad = bacon_egg_salad.get_nutrition()
#print(f"{key}: {value}")
single_serving_size_bacon_egg_salad = 10.7
nutrition_single_serving_bacon_egg_salad = {}
nutrition_single_serving_bacon_egg_salad[key] = value /
single_serving_size_bacon_egg_salad
nutrition_info_sweet_potatoe_mash = sweet_potatoe_mash.get_nutrition()
#print(f"{key}: {value}")
single_serving_size_sweet_potatoe_mash = 25
nutrition_single_serving_sweet_potatoe_mash = {}
nutrition_single_serving_sweet_potatoe_mash[key] = value /
single_serving_size_sweet_potatoe_mash
nutrition_info_nut_pouch = nut_pouch.get_nutrition()
#print(f"{key}: {value}")
single_serving_size_nut_pouch = 2
nutrition_single_serving_nut_pouch = {}
nutrition_per_single_serving = {}
for key, value in nutrition_info.items():
print(f"{key}: {value}")
#######################
# Example usage: Calculate nutrition per single serving for any recipe
#print_nutrition_per_single_serving("salmon", nutrition_info_salmon,
single_serving_size_salmon)
#print_nutrition_per_single_serving("guacamole", nutrition_info_guacamole,
single_serving_size_guacamole)
#print_nutrition_per_single_serving("platanos", nutrition_info_platanos,
single_serving_size_platanos)
#############################
# Add the nutrition facts for any recipes together
total_nutrition_day = {}
total_nutrition_day[key] = nutrition_single_serving_platanos[key] +
nutrition_single_serving_Beef_stew[key] + nutrition_single_serving_bacon_egg_salad[key]
+ nutrition_single_serving_nut_pouch[key]
print(f"{key}: {value}")