home · contact · privacy
Various improvements to calories counter.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 2 Oct 2023 19:11:52 +0000 (21:11 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 2 Oct 2023 19:11:52 +0000 (21:11 +0200)
calories.py
income_progress_bars.py

index 771b11af8eab34f14e683066b1acf521e409b10e..0f65aebae5a09b04f90bccef302ce5f90fcdbcdb 100644 (file)
@@ -1,6 +1,7 @@
 from http.server import BaseHTTPRequestHandler, HTTPServer
 import os
 import json
+import datetime
 
 def build_page(eatable_rows, consumption_rows, eatables_selection, day_rows):
     return """<html>
@@ -17,17 +18,15 @@ td, th {
 <form action="/" method="POST">
 <td><input name="update" type="submit" value="update" /></td>
 <table>
-<tr><th>title</th><th>calories</th><th>sugar (g)</th><th>standard weight (g)</th><th>comments</th><th>delete</th></tr>
-{eatable_rows}
+<tr><th>eatable</th><th>unit count</th><th>unit weight (g)</th><th>calories</th><th>sugar (g)</th></tr>
+{consumption_rows}
 <tr>
-<th>add:</th>
+<th>add from DB:</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><input name="comments" type="text" value="" /></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>
 <table>
@@ -35,20 +34,38 @@ td, th {
 {day_rows}
 </table>
 <table>
-<tr><th>eatable</th><th>unit count</th><th>unit weight (g)</th><th>calories</th><th>sugar (g)</th></tr>
-{consumption_rows}
+<tr><th>title</th><th>calories</th><th>sugar (g)</th><th>standard weight (g)</th><th>comments</th><th>delete</th></tr>
+{eatable_rows}
 <tr>
-<th>add from DB:</th>
+<th>add:</th>
 </tr>
 <tr>
-<td><select name="eatable_key">{eatables_selection}</select></td>
-<td><input name="unit_count" type="number" step="1" min="0" value="0" /></td>
-<td></td>
+<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><input name="comments" type="text" value="" /></td>
 </tr>
 </table>
 </form>
 </body>
-</html>"""
+<script>
+""" + """
+var unit_count_inputs = document.getElementsByClassName("unit_count");
+for (let i = 0; i < unit_count_inputs.length; i++) {
+    let input = unit_count_inputs[i];
+    let button = document.createElement('button');
+    button.innerHTML = '+1'; 
+    button.onclick = function(event) {
+        event.preventDefault();
+        input.value = parseInt(input.value) + 1;
+    };
+    input.insertAdjacentElement('afterend', button);
+}
+
+</script>
+</html>
+"""
 
 class LockFileDetected(Exception):
     pass
@@ -140,8 +157,9 @@ class Database:
         return {"cals": calories, "sugar": sugar_g }
 
     def eatables_selection(self, selection=None):
-        html = '' # if selection else '<option value="" />'
-        for k,v in self.eatables.items():
+        html = ''
+        for k, v in sorted(self.eatables.items(), key=lambda item: item[1].title):
+            v = self.eatables[k]
             selected = ' selected' if k==selection else ''
             html += '<option value="%s"%s>%s</option>' % (k, selected, v.title)
         return html
@@ -152,7 +170,9 @@ class Database:
     def add_consumption(self, consumption):
         self.consumptions += [consumption] 
 
-    def add_day(self, date, day):
+    def add_day(self, date, day, archives_today=False):
+        if archives_today:
+            date = date + str(datetime.datetime.now())[10:]
         self.days[date] = day 
 
     def set_today_date(self, today_date):
@@ -179,7 +199,6 @@ class MyServer(BaseHTTPRequestHandler):
     def do_POST(self):
         from uuid import uuid4
         from urllib.parse import parse_qs
-        import datetime
         length = int(self.headers['content-length'])
         postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
         db = Database(False)
@@ -220,8 +239,8 @@ class MyServer(BaseHTTPRequestHandler):
         if 'archive_day' in postvars.keys():
             new_cals = postvars["new_day_cals"][0]
             new_sugar = postvars["new_day_sugar"][0]
-            db.add_day(db.today_date, Day(float(new_cals), float(new_sugar)))
-            db.set_today_date(str(datetime.datetime.now()))#[:10])
+            db.add_day(db.today_date, Day(float(new_cals), float(new_sugar)), archives_today=True)
+            db.set_today_date(str(datetime.datetime.now())[:10])
             db.consumptions = []
         try:
             db.write()
@@ -255,7 +274,7 @@ class MyServer(BaseHTTPRequestHandler):
             r = db.calc_consumption(c)
             consumptions += "<tr />"\
                     "<td><select name=\"eatable_key\">%s</select></td>"\
-                    "<td><input name=\"unit_count\" type=\"number\" min=\"0\" value=\"%d\" /></td>"\
+                    "<td><input class=\"unit_count\" name=\"unit_count\" type=\"number\" min=\"0\" value=\"%d\" /></td>"\
                     "<td></td>"\
                     "<td>%.1f</td>"\
                     "<td>%.1f</td>"\
@@ -281,7 +300,7 @@ class MyServer(BaseHTTPRequestHandler):
 
 
 hostName = "localhost"
-serverPort = 8080
+serverPort = 8081
 if __name__ == "__main__":        
     webServer = HTTPServer((hostName, serverPort), MyServer)
     print("Server started http://%s:%s" % (hostName, serverPort))
index 5a4f5ad013eae7e8bbc68b301e1d429c78e23b72..af2afd484e87e9c911303b59a9c37b9866af3b2f 100644 (file)
@@ -3,7 +3,7 @@ import os
 import json
 
 hostName = "localhost"
-serverPort = 8080
+serverPort = 8081
 
 header = """<html>
 <meta charset="UTF-8">