The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Create a recipe calorie calculator

Here’s a simple recipe calorie calculator script in Python that you can use or adapt for your website. It calculates total calories of a recipe based on ingredients and their amounts.

python
# Recipe Calorie Calculator # Sample calorie database (per 100 grams) calorie_database = { 'chicken breast': 165, 'olive oil': 884, 'rice': 130, 'onion': 40, 'carrot': 41, 'potato': 77, 'butter': 717, 'sugar': 387, 'egg': 155, 'milk': 42, 'flour': 364, 'cheese': 402, 'tomato': 18, 'garlic': 149, 'spinach': 23, } def calculate_recipe_calories(ingredients): """ ingredients: list of tuples [(ingredient_name, weight_in_grams), ...] returns total calories of the recipe """ total_calories = 0 missing_ingredients = [] for ingredient, weight in ingredients: ingredient_lower = ingredient.lower() if ingredient_lower in calorie_database: cal_per_100g = calorie_database[ingredient_lower] calories = (cal_per_100g * weight) / 100 total_calories += calories else: missing_ingredients.append(ingredient) return total_calories, missing_ingredients # Example usage ingredients_list = [ ('Chicken Breast', 200), # 200g chicken breast ('Olive Oil', 20), # 20g olive oil ('Rice', 150), # 150g rice ('Onion', 50), # 50g onion ] total_cal, missing = calculate_recipe_calories(ingredients_list) print(f"Total calories: {total_cal:.2f} kcal") if missing: print(f"Calories for these ingredients are missing: {', '.join(missing)}")

You can extend the calorie_database with more ingredients or link it to a nutrition API or database for accurate values.

If you want, I can help you make a web version (HTML + JavaScript) of this calculator. Would you like that?

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About