본문 바로가기
공부/파이썬 Python

칼로리 트래커 / 대시보드 (1)

by 혼밥맨 2022. 3. 18.
반응형

칼로리 트래커 / 대시보드 (1)

 

Introduction

하루 하루 섭취한 음식을 등록할 때 마다 동기적으로 변하는 대시보드가 있다면 누적 칼로리, 하루 칼로리량을 관리하고 한 눈에 확인하기에 간편할 것 같습니다.

 

Full Code

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
from dataclasses import dataclass
 
import numpy as np
import matplotlib.pyplot as plt
 
 
CALORIES_GOAL_LIMIT = 3000 # kcal
PROTEIN_GOAL = 180           # grams
FAT_GOAL = 80                  # grams
CARBS_GOAL = 300           # grams
 
today = []
 
 
@dataclass
class Food:
    name: str
    calories: int
    protein: int
    fat: int
    carbs: int
    
 
done = False
 
while not done: 
    print("""
    (1) Add a new food
    (2) Visualize progress
    (q) quit
 
 
""")
 
    choice = input("Choose an option: ")
    
    if choice == "1":
        print("Adding a new food!")
        name = input("Name: ")
        calories = int(input("Calories: "))
        proteins = int(input("Proteins: "))
        fats = int(input("Fats: "))
        carbs = int(input("Carbs: "))
        food = Food(name, calories, proteins, fats, carbs)
        print("Successfully added!")
 
    elif choice == "2":
        calorie_sum = sum(food.calories for food in today)
        protein_sum = sum(food.protein for food in today)
        fats_sum = sum(food.fat for food in today)        
        carbs_sum = sum(food.carbs for food in today)
 
        fig, axs = plt.subplots(22)
        axs[00].pie([protein_sum, fats_sum, carbs_sum], labels=["Proteins""Fats""Carbs"], autopct="%1.1f%%")
        axs[00].set_title("Macronutirients Distribution")
        axs[01].bar([0,1,2], [protein_sum, fats_sum, carbs_sum], width=0.4)
        axs[01].bar([0.51.52.5], [PROTEIN_GOAL, FAT_GOAL, CARBS_GOAL, width=0.4)
        axs[01].set_title("Macronutrients Progress")
        axs[10].pie([calorie_sum, CALORIE_GOAL_LIMIT - calorie_sum], labels=["Calories""Remaining"], autopct="%1.1f%%")
        axs[10].set_title("Calories Goal Progress")
        axs[11].plot(list(range(len(today))), np.cumsum([food.calories for food in today]), labels="Calories Eaten")
        axs[11].plot(list(range(len(today))), [CALORIE_GOAL_LIMIT] * len(today),     label="Calorie Goal")
        axs[11].set_legend()
        axs[11].set_title("Calories Goal Over Time")
 
        fig.tight_layout()
        fig.show()
 
    elif choice == "q":
        done = True
    else:
        print("Invalid choice!")
cs

 

실행

반응형

댓글