home · contact · privacy
Improve ledger.py.
[misc] / calories.py
index dc40d5ff9e58cfaabf14ca2c43c6841e42dbd0c4..5d2fc98c3aaf99ceaeae0708e6f06094370c8bc3 100644 (file)
@@ -10,12 +10,10 @@ def build_page(eatable_rows, consumption_rows, eatables_selection, day_rows):
     return """<html>
 <meta charset="UTF-8">
 <style>
-table {
-  margin-bottom: 2em;
-}
-td, th {
-  text-align: right;
-}
+table { margin-bottom: 2em; }
+th, td { text-align: left; }
+td.number { text-align: right; }
+input[type="number"] { text-align: right; }
 </style>""" + f"""
 <body>
 <form action="/" method="POST">
@@ -28,8 +26,8 @@ td, th {
 </tr>
 <tr>
 <input type="hidden" name="keep_visible" value="0">
+<td class="number"><input class="unit_count" name="unit_count" type="number" step="0.1" min="0" value="0" /></td>
 <td><select name="eatable_key">{eatables_selection}</select></td>
-<td><input class="unit_count" name="unit_count" type="number" step="1" min="0" value="0" /></td>
 <td></td>
 </tr>
 </table>
@@ -45,9 +43,9 @@ td, th {
 </tr>
 <tr>
 <td><input name="title" type="text" value="" /></td>
-<td><input name="cals" type="number" min="0" step="0.1" value="0" /></td>
-<td><input name="sugar_g" type="number" min="0" step="0.1" value="0" /></td>
-<td><input name="standard_g" type="number" min="1" step="0.1" value="1" /></td>
+<td class="number"><input name="cals" type="number" min="0" step="0.1" value="0" /></td>
+<td class="number"><input name="sugar_g" type="number" min="0" step="0.1" value="0" /></td>
+<td class="number"><input name="standard_g" type="number" min="1" step="0.1" value="1" /></td>
 <td><input name="comments" type="text" value="" /></td>
 </tr>
 </table>
@@ -62,7 +60,7 @@ for (let i = 0; i < unit_count_inputs.length; i++) {
     button.innerHTML = '+1';
     button.onclick = function(event) {
         event.preventDefault();
-        input.value = parseInt(input.value) + 1;
+        input.value = parseFloat(input.value) + 1.0;
     };
     input.insertAdjacentElement('afterend', button);
 }
@@ -158,18 +156,18 @@ class Database:
         eatable = self.eatables[c.eatable_key]
         calories = eatable.cals * c.unit_count
         sugar_g = eatable.sugar_g * c.unit_count
-        # calories = float(eatable.cals / eatable.standard_g) * c.unit_count * c.unit_weight
-        # sugar_g = float(eatable.sugar_g / eatable.standard_g) * c.unit_count * c.unit_weight
         self.today.calories += calories
         self.today.sugar_g += sugar_g
         return {"cals": calories, "sugar": sugar_g }
 
-    def eatables_selection(self, selection=None):
+    def eatables_selection(self):
         html = ''
+        already_selected = [c.eatable_key for c in self.consumptions]
         for k, v in sorted(self.eatables.items(), key=lambda item: item[1].title):
+            if k in already_selected:
+                continue
             v = self.eatables[k]
-            selected = ' selected' if k==selection else ''
-            html += '<option value="%s"%s>%s</option>' % (k, selected, v.title)
+            html += '<option value="%s">%s</option>' % (k, v.title)
         return html
 
     def add_eatable(self, id_, eatable):
@@ -225,7 +223,7 @@ class MyServer(BaseHTTPRequestHandler):
                 if uuid not in to_delete:
                     e = Eatable(decode("title", i, False), decode("cals", i), decode("sugar_g", i), decode("standard_g", i), decode("comments", i, False))
                     db.add_eatable(uuid, e)
-                i += 1   
+                i += 1
         if 'title' in postvars.keys() and len(postvars['title'][i]) > 0:
              e = Eatable(decode("title", i, False), decode("cals", i), decode("sugar_g", i), decode("standard_g", i), decode("comments", i, False))
              db.add_eatable(str(uuid4()), e)
@@ -253,7 +251,7 @@ class MyServer(BaseHTTPRequestHandler):
                 if c.unit_count > 0:
                     db.eatables[c.eatable_key].popularity += 1
             db.consumptions = []
-            default_slots = 7
+            default_slots = 10
             for k, v in sorted(db.eatables.items(), key=lambda item: -item[1].popularity):
                 db.add_consumption(Consumption(k, 0))
                 default_slots -= 1
@@ -280,38 +278,39 @@ class MyServer(BaseHTTPRequestHandler):
             eatables += "<tr>"\
                     "<input name=\"eatable_uuid\" type=\"hidden\" value=\"%s\" />"\
                     "<td><input name=\"title\" value=\"%s\" /></td>"\
-                    "<td><input name=\"cals\" type=\"number\" step=\"0.1\" min=\"0\" value=\"%.1f\" /></td>"\
-                    "<td><input name=\"sugar_g\" type=\"number\" step=\"0.1\" min=\"0\" value=\"%.1f\" /></td>"\
-                    "<td><input name=\"standard_g\" type=\"number\" step=\"0.1\" min=\"1\" value=\"%1.f\" /></td>"\
+                    "<td class\"number\"><input name=\"cals\" type=\"number\" step=\"0.1\" min=\"0\" value=\"%.1f\" /></td>"\
+                    "<td class\"number\"><input name=\"sugar_g\" type=\"number\" step=\"0.1\" min=\"0\" value=\"%.1f\" /></td>"\
+                    "<td class\"number\"><input name=\"standard_g\" type=\"number\" step=\"0.1\" min=\"1\" value=\"%1.f\" /></td>"\
                     "<td><input name=\"comments\" value=\"%s\" /></td>"\
                     "<td><input name=\"delete\" type=\"checkbox\" value=\"%s\" /></td>"\
                     "</tr>" % (k, v.title, v.cals, v.sugar_g, v.standard_g, v.comments, k)
         consumptions = ""
+        db.consumptions = sorted(db.consumptions, key=lambda x: db.eatables[x.eatable_key].title)
         for c in db.consumptions:
             r = db.calc_consumption(c)
             consumptions += "<tr />"\
-                    "<input type=\"hidden\" name=\"keep_visible\" value=\"1\">"\
-                    "<td><select name=\"eatable_key\">%s</select></td>"\
-                    "<td><input class=\"unit_count\" name=\"unit_count\" type=\"number\" min=\"0\" value=\"%d\" /></td>"\
+                    "<input type=\"hidden\" name=\"keep_visible\" value=\"1\"><input name=\"eatable_key\" type=\"hidden\" value=\"%s\">"\
+                    "<td class\"number\"><input class=\"unit_count number\" name=\"unit_count\" type=\"number\" min=\"0\" step=\"0.1\" value=\"%.1f\" /></td>"\
+                    "<td>%s</td>"\
                     "<td></td>"\
-                    "<td>%.1f</td>"\
-                    "<td>%.1f</td>"\
-                    "</tr>" % (db.eatables_selection(c.eatable_key), c.unit_count, r["cals"], r["sugar"])
+                    "<td class=\"number\">%.1f</td>"\
+                    "<td class=\"number\">%.1f</td>"\
+                    "</tr>" % (c.eatable_key, c.unit_count, db.eatables[c.eatable_key].title, r["cals"], r["sugar"])
         day_rows = ""
         for date in sorted(db.days.keys()):
             day = db.days[date]
             day_rows = "<tr>"\
                     "<td><input name=\"day_date\" type=\"hidden\" value=\"%s\" />%s</td>"\
-                    "<td><input name=\"day_cals\" type=\"hidden\" step=\"0.1\" min=\"0\" value=\"%.1f\" />%.1f</td>"\
-                    "<td><input name=\"day_sugar\" type=\"hidden\" step=\"0.1\" min=\"0\" value=\"%.1f\" />%.1f</td>"\
+                    "<td class=\"number\"><input name=\"day_cals\" type=\"hidden\" step=\"0.1\" min=\"0\" value=\"%.1f\" />%.1f</td>"\
+                    "<td class=\"number\"><input name=\"day_sugar\" type=\"hidden\" step=\"0.1\" min=\"0\" value=\"%.1f\" />%.1f</td>"\
                     "</tr>" % (date, date[:10], day.calories, day.calories, day.sugar_g, day.sugar_g) + day_rows
         day_rows = "<tr>"\
                 "<th>today:</th><th></th><th></th><th>archive?</th>"\
                 "</tr>"\
                 "<tr>"\
                 "<td><input name=\"new_date\" size=8 value=\"%s\" /></td>"\
-                "<td><input name=\"new_day_cals\" type=\"hidden\" value=\"%.1f\" readonly />%.1f</td>"\
-                "<td><input name=\"new_day_sugar\" type=\"hidden\" value=\"%.1f\" readonly />%.1f</td>"\
+                "<td class=\"number\"><input name=\"new_day_cals\" type=\"hidden\" value=\"%.1f\" readonly />%.1f</td>"\
+                "<td class=\"number\"><input name=\"new_day_sugar\" type=\"hidden\" value=\"%.1f\" readonly />%.1f</td>"\
                 "<td><input name=\"archive_day\" type=\"checkbox\" /></td>"\
                 "</tr>" % (db.today_date, db.today.calories, db.today.calories, db.today.sugar_g, db.today.sugar_g) + day_rows
         page = build_page(eatables, consumptions, db.eatables_selection(), day_rows)