The Right Tool for the Job

During the pandemic, working from home, afforded me time to pay more attention to my nutrition. Primarily interested in tracking macros (Calories, Protein, Fats, Carbohydrates) in my diet, I began to log what I eat in a google sheet. The sheet has four tabs. The first tab has ingredients available in my kitchen with their macros and portions:

Ingredients

Recipes go on the second tab. Each recipe links (Column C) to member ingredients from the first tab (Column A), calculating the recipe’s macros:

Recipes

Meals go on the third tab. Each meal links (Columns Name and Ingredient) to ingredients (Column A) and recipes (Column B) from the first two tabs, calculating the meal’s macros:

Meals

Daily tracker is on the fourth tab. Each day (Column A) links to meals consumed on that day from the previous tab (Column Date), calculating the day’s macros:

Tracker

Each day, I add portions of recipes or ingredients to the Meals tab, and track macros on the Daily tracker tab. Adding new ingredients and recipes is a chore, but rare enough to be a bother. This setup works well for tracking daily nutrient consumption, but evolves as new challenges come up.

Challenge 1: Significant variance between daily tracker goals and consumed macros. While the tracker shows my progress, it doesn’t necessarily guide me towards the right food choices. I need a meal planner to suggest what and how much to eat based on availability and macros. The meal planner can be modeled as a constraint programming problem, and since google sheets conveniently supports Excel’s NLSOLVE() solver which finds exact or min-error solutions to a system of algebraic equations, google sheets is still the right tool for the job.

Meal Planner is added as the fifth tab. Each mealplan links to ingredients and recipes (Column A) from the first two tabs, calculating portion quantities based on macros. This setup works well for tracking daily nutrient consumption, and generating mealplans.

Mealplan

Challenge 2: The ingredients tab requires a major refresh when traveling, to include local foods from Delhi. For a more comprehensive food and nutrition database, I choose USDA FoodData Central which provides macro and micronutrient data for a large variety of foods. While the whole database in unnecessary for now, I opt to scrape and parse the foods I need using requests and BeautifulSoup, storing one file per food on disk. OR-TOOLS provides efficient solvers for integer constraint programming problem to generate mealplans. I switch to python as the right tool for the job to manage a bigger ingredient library. This setup works well for tracking daily nutrient consumption, and generating mealplans across a larger food and nutrition database.

Challenge 3: As the ingredient library expands, searching and linking the correct reference cells across tabs becomes error-prone. I look for a tool that provides me with capable CRUD interfaces to manage data objects, can integrate with RDBMS (for larger food libraries) and is preferably written in python (compatible with OR-TOOLS), and choose Django as the right tool for the job. USDA FoodData Central database is written to PostgreSQL, and I’m happy with Django admin UIs running on my laptop to log food. This setup works well for tracking daily nutrient consumption with referential integrity, and generating mealplans across a larger food and nutrition database.

Challenge 4: USDA FoodData Central database is missing Trader Joe’s products. Often while shopping at Trader Joe’s, I wait to get home to find / add foods to the database. I deploy the Django app on Famnom opting for a deployment platform that supports Django, PostgreSQL, Redis, etc and is easy to manage and scale. Heroku is the right tool for the job. This setup works well for tracking daily nutrient consumption with referential integrity, and generating mealplans across a larger food and nutrition database, accessible from anywhere.

Challenge 5: Majority of foods in USDA FoodData Central database are packaged foods, and have barcodes. Support for lookups by barcode can improve accuracy of search results. While there is a browser Barcode Detection API, most mobile browsers (looking at you iOS) don’t implement it. On the other hand, both iOS and Android offer camera APIs to read barcodes. I choose Flutter for app code and Django REST Framework (DRF) for mobile REST APIs as the right tools for the job. This setup works well for tracking daily nutrient consumption with referential integrity, lookup foods by name and barcodes, and generating mealplans across a larger food and nutrition database, accessible from anywhere.