Spaces:
Runtime error
Runtime error
File size: 2,812 Bytes
177a572 a65a74b 177a572 a65a74b 19525c9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | import json
import os
from pathlib import Path
from typing import List, Dict
from datetime import datetime
MEAL_FILE = "meal_tracker.json"
def save_preferences(preferences: Dict) -> str:
with open('user_preferences.json', 'w') as f:
json.dump(preferences, f, indent=4)
return "Preferences saved successfully!"
def load_preferences() -> Dict:
if os.path.exists('user_preferences.json'):
with open('user_preferences.json', 'r') as f:
return json.load(f)
return {
"physical_info": {"height": 170, "weight": 70.0, "age": 30},
"focus_areas": {"custom": "", "selected": []},
"diet_preferences": {"custom": "", "selected": []}
}
def load_meals():
"""Load meals from the JSON file."""
if Path(MEAL_FILE).exists():
with open(MEAL_FILE, 'r') as f:
return json.load(f)
return []
def save_meal_to_database(new_meal):
"""Save a new meal to the JSON file, maintaining chronological order."""
meals = load_meals()
meals.append(new_meal)
meals.sort(key=lambda x: datetime.strptime(x['date'], "%Y-%m-%d %H:%M:%S"))
with open(MEAL_FILE, 'w') as f:
json.dump(meals, f, indent=2)
def get_meals_markdown(meals):
"""Convert meals to a markdown string for display."""
if not meals:
return "No meals recorded yet."
markdown = "## Recorded Meals\n\n"
for meal in meals:
markdown += f"**Date:** {meal['date']}\n"
markdown += f"**Type:** {meal['meal_type']}\n"
markdown += f"**Description:** {meal['description']}\n"
if meal['tags']:
markdown += f"**Tags:** {', '.join(meal['tags'])}\n"
markdown += "\n---\n\n"
return markdown
def save_meals(meals):
"""Save all meals to the JSON file."""
with open(MEAL_FILE, 'w') as f:
json.dump(meals, f, indent=2)
def get_macros_for_diet(diet_name: str) -> Dict[str, int]:
"""
Return macronutrient ratios for a given diet
"""
# You can expand this function with more diet types and their corresponding macros
macros = {
"Balanced Diet": {"carbs": 50, "protein": 20, "fat": 30},
"Zone Diet": {"carbs": 40, "protein": 30, "fat": 30},
"Ketogenic Diet (Keto)": {"carbs": 75, "protein": 20, "fat": 5},
"Vegan Diet": {"carbs": 58, "protein": 15, "fat": 27},
"Mediterranean Diet": {"carbs": 50, "protein": 17, "fat": 33},
"Paleo Diet": {"carbs": 30, "protein": 30, "fat": 40},
"Low-Carb Diet": {"carbs": 20, "protein": 35, "fat": 45},
"High-Protein Diet": {"carbs": 25, "protein": 35, "fat": 40},
"Vegetarian Diet": {"carbs": 55, "protein": 20, "fat": 25}
}
return macros.get(diet_name, {"carbs": 50, "protein": 20, "fat": 30}) # Default balanced diet
|