mirror of
https://github.com/StefBuwalda/cal_counter.git
synced 2025-10-30 03:10:00 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb160d364a | ||
| fdf264b9c5 | |||
| 91595ccc11 | |||
| 6c01c6a923 | |||
|
|
5a9e1f77c7 | ||
| 2454bc61cb | |||
| 85297daaaf |
13
.github/workflows/docker-ghcr.yml
vendored
13
.github/workflows/docker-ghcr.yml
vendored
@@ -14,9 +14,11 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
# Checkout code
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4.2.2
|
||||
|
||||
# Log in to GHCR
|
||||
- name: Log in to GitHub Container Registry
|
||||
uses: docker/login-action@v3.5.0
|
||||
with:
|
||||
@@ -24,13 +26,22 @@ jobs:
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
# Set up Docker Buildx (needed for ARM64 cross-build)
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3.11.1
|
||||
|
||||
# Build and push Docker image with caching
|
||||
- name: Build and push ARM64 image
|
||||
uses: docker/build-push-action@v6.18.0
|
||||
with:
|
||||
context: .
|
||||
push: true
|
||||
platforms: linux/arm64
|
||||
tags: ghcr.io/stefbuwalda/cal_counter:arm64,ghcr.io/stefbuwalda/cal_counter:latest
|
||||
tags: |
|
||||
ghcr.io/stefbuwalda/cal_counter:arm64
|
||||
ghcr.io/stefbuwalda/cal_counter:latest
|
||||
cache-from: type=registry,ref=ghcr.io/stefbuwalda/cal_counter:cache
|
||||
cache-to: type=registry,ref=ghcr.io/stefbuwalda/cal_counter:cache,mode=max
|
||||
build-args: |
|
||||
PIP_NO_CACHE_DIR=1
|
||||
|
||||
|
||||
@@ -2,8 +2,13 @@ FROM python:3.12-slim
|
||||
|
||||
# Everything will be done in /app (Not in the main OS Image)
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
|
||||
COPY requirements.txt .
|
||||
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN chmod +x ./entrypoint.sh
|
||||
|
||||
ENV FLASK_APP=app.py
|
||||
|
||||
1
app.py
1
app.py
@@ -5,7 +5,6 @@ from flask import (
|
||||
)
|
||||
from flask_login import (
|
||||
login_required,
|
||||
logout_user,
|
||||
current_user,
|
||||
)
|
||||
from models import User
|
||||
|
||||
0
application/static/css/macros.css
Normal file
0
application/static/css/macros.css
Normal file
@@ -25,6 +25,9 @@
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ url_for('user.daily_log') }}">Daily Log</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ url_for('user.daily_log2') }}">Daily Log (new)</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ url_for('user.dashboard') }}">Dashboard</a>
|
||||
</li>
|
||||
|
||||
@@ -13,6 +13,8 @@ from forms import FoodItemForm
|
||||
from models import FoodItem, FoodLog
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from application.utils import login_required
|
||||
from typing import cast
|
||||
from numpy import array
|
||||
|
||||
user_bp = Blueprint(
|
||||
"user",
|
||||
@@ -21,6 +23,57 @@ user_bp = Blueprint(
|
||||
)
|
||||
|
||||
|
||||
def macro_arr_to_json(data: list[float]):
|
||||
assert len(data) == 4
|
||||
cal = data[0]
|
||||
pro = data[3]
|
||||
car = data[2]
|
||||
fat = data[1]
|
||||
macros = [
|
||||
{
|
||||
"name": "Calories",
|
||||
"current": cal,
|
||||
"target": 2000,
|
||||
"bar_width": 100 - abs(cal / 20 - 100),
|
||||
"bar_width_overflow": max(0, cal / 20 - 100),
|
||||
"unit": " kcal",
|
||||
"color": "bg-calories",
|
||||
"overflow_color": "bg-calories-dark",
|
||||
},
|
||||
{
|
||||
"name": "Protein",
|
||||
"current": pro,
|
||||
"target": 150,
|
||||
"bar_width": 100 - abs(pro / 1.5 - 100),
|
||||
"bar_width_overflow": max(0, pro / 1.5 - 100),
|
||||
"unit": "g",
|
||||
"color": "bg-protein",
|
||||
"overflow_color": "bg-protein-dark",
|
||||
},
|
||||
{
|
||||
"name": "Carbs",
|
||||
"current": car,
|
||||
"target": 250,
|
||||
"bar_width": 100 - abs(car / 2.5 - 100),
|
||||
"bar_width_overflow": max(0, car / 2.5 - 100),
|
||||
"unit": "g",
|
||||
"color": "bg-carbs",
|
||||
"overflow_color": "bg-carbs-dark",
|
||||
},
|
||||
{
|
||||
"name": "Fat",
|
||||
"current": fat,
|
||||
"target": 70,
|
||||
"bar_width": 100 - abs(fat / 0.7 - 100),
|
||||
"bar_width_overflow": max(0, fat / 0.7 - 100),
|
||||
"unit": "g",
|
||||
"color": "bg-fat",
|
||||
"overflow_color": "bg-fat-dark",
|
||||
},
|
||||
]
|
||||
return macros
|
||||
|
||||
|
||||
user_bp.before_request(login_required)
|
||||
|
||||
|
||||
@@ -100,6 +153,18 @@ def daily_log(offset: int = 0):
|
||||
)
|
||||
|
||||
|
||||
@user_bp.route("/daily_log2", methods=["GET"])
|
||||
def daily_log2():
|
||||
today = datetime.now(timezone.utc).date()
|
||||
logs_today = current_user.food_logs.filter_by(date_=today).all()
|
||||
macros = array((0.0, 0.0, 0.0, 0.0))
|
||||
for log in logs_today:
|
||||
item = cast(FoodItem, log.food_item)
|
||||
macros += array(item.macros()) / 100 * log.amount
|
||||
macros = macro_arr_to_json(macros.tolist())
|
||||
return render_template("daily_log2.html", macros=macros, logs=logs_today)
|
||||
|
||||
|
||||
@user_bp.route("/remove_log/<int:id>", methods=["POST"])
|
||||
def remove_log(id: int):
|
||||
log = db.session.get(FoodLog, id)
|
||||
|
||||
67
application/user/templates/daily_log2.html
Normal file
67
application/user/templates/daily_log2.html
Normal file
@@ -0,0 +1,67 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block title %}Daily Calorie Dashboard{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='css/macros.css') }}">
|
||||
|
||||
<div class="container my-4">
|
||||
<h2 class="mb-3">Daily Calorie Dashboard</h2>
|
||||
|
||||
<!-- Macro Summary -->
|
||||
<div class="card p-3 mb-3">
|
||||
<h5>Macros</h5>
|
||||
{% for macro in macros %}
|
||||
<div class="mb-2">
|
||||
<span class="macro-text">{{ macro.name }}: {{ macro.current }} / {{ macro.target }}</span>
|
||||
<div class="progress rounded" style="height: 24px;">
|
||||
<div class="progress-bar bg-danger macro-bar" role="progressbar"
|
||||
style="width: {{ macro.bar_width_overflow }}%">
|
||||
{{ macro.current - macro.target }}{{ macro.unit }}
|
||||
</div>
|
||||
<div class="progress-bar bg-success macro-bar" role="progressbar" style="width: {{ macro.bar_width }}%">
|
||||
{{ macro.current }}{{ macro.unit }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
<!-- Items List -->
|
||||
<div class="card p-3">
|
||||
<h5>Items Eaten Today</h5>
|
||||
<div class="list-group list-group-flush">
|
||||
{% for log in logs %}
|
||||
<div class="list-group-item item-row d-flex justify-content-between align-items-center bg-dark text-light">
|
||||
<span>({{ log.amount }}g) {{ log.food_item.name }}</span>
|
||||
<span>{{ log.food_item.energy_100 }} kcal</span>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<!-- Bottom Navigation Buttons -->
|
||||
<div class="container-fluid fixed-bottom py-2">
|
||||
<div class="d-flex p-3">
|
||||
<!-- Left Button -->
|
||||
<a href="" class="btn btn-outline-light flex-fill me-2 rounded-pill">
|
||||
‹ Previous
|
||||
</a>
|
||||
|
||||
<!-- Center Button (highlighted) -->
|
||||
<a href="{{ url_for('add_meal.step1', meal_type=0) }}"
|
||||
class="btn btn-success flex-fill mx-2 fw-bold rounded-pill">
|
||||
+ Add Item
|
||||
</a>
|
||||
|
||||
<!-- Right Button -->
|
||||
<a href="" class="btn btn-outline-light flex-fill ms-2 rounded-pill">
|
||||
Next ›
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
41
models.py
41
models.py
@@ -49,17 +49,18 @@ class Unit(db.Model):
|
||||
|
||||
class FoodItem(db.Model):
|
||||
__tablename__ = "food_item"
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
barcode = db.Column(db.String)
|
||||
owner_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
||||
name = db.Column(db.String(150), nullable=False)
|
||||
|
||||
energy_100 = db.Column(db.Float, nullable=False)
|
||||
protein_100 = db.Column(db.Float, nullable=False)
|
||||
carbs_100 = db.Column(db.Float, nullable=False)
|
||||
sugar_100 = db.Column(db.Float)
|
||||
fat_100 = db.Column(db.Float, nullable=False)
|
||||
saturated_fat_100 = db.Column(db.Float)
|
||||
energy_100: float = db.Column(db.Float, nullable=False)
|
||||
protein_100: float = db.Column(db.Float, nullable=False)
|
||||
carbs_100: float = db.Column(db.Float, nullable=False)
|
||||
sugar_100: Optional[float] = db.Column(db.Float)
|
||||
fat_100: float = db.Column(db.Float, nullable=False)
|
||||
saturated_fat_100: Optional[float] = db.Column(db.Float)
|
||||
|
||||
food_logs = db.relationship(
|
||||
"FoodLog",
|
||||
@@ -99,26 +100,20 @@ class FoodItem(db.Model):
|
||||
|
||||
def updateFromForm(self, form: FoodItemForm):
|
||||
self.name = form.name.data
|
||||
self.energy_100 = form.energy.data
|
||||
self.protein_100 = form.protein.data
|
||||
self.carbs_100 = form.carbs.data
|
||||
self.energy_100 = form.energy.data or 0
|
||||
self.protein_100 = form.protein.data or 0
|
||||
self.carbs_100 = form.carbs.data or 0
|
||||
self.sugar_100 = form.sugar.data
|
||||
self.fat_100 = form.fat.data
|
||||
self.fat_100 = form.fat.data or 0
|
||||
self.saturated_fat_100 = form.saturated_fat.data
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"barcode": self.barcode,
|
||||
"name": self.name,
|
||||
"owner_id": self.owner_id,
|
||||
"energy_100": self.energy_100,
|
||||
"protein_100": self.protein_100,
|
||||
"carbs_100": self.carbs_100,
|
||||
"sugar_100": self.sugar_100,
|
||||
"fat_100": self.fat_100,
|
||||
"saturated_fat_100": self.saturated_fat_100,
|
||||
}
|
||||
def macros(self) -> tuple[float, float, float, float]:
|
||||
return (
|
||||
self.energy_100,
|
||||
self.fat_100,
|
||||
self.carbs_100,
|
||||
self.protein_100,
|
||||
)
|
||||
|
||||
|
||||
class FoodLog(db.Model):
|
||||
|
||||
Reference in New Issue
Block a user