home · contact · privacy
Minor calories counter improvement.
[misc] / calories.py
index 7a4846acdf40b5304fc5cc4ba088de48d16830da..5ab57044eb3bd7333f7764ab62938d071473e72b 100644 (file)
@@ -3,6 +3,9 @@ import os
 import json
 import datetime
 
+hostName = "localhost"
+serverPort = 8081
+
 def build_page(eatable_rows, consumption_rows, eatables_selection, day_rows):
     return """<html>
 <meta charset="UTF-8">
@@ -284,6 +287,7 @@ class MyServer(BaseHTTPRequestHandler):
                     "<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 />"\
@@ -295,7 +299,8 @@ class MyServer(BaseHTTPRequestHandler):
                     "<td>%.1f</td>"\
                     "</tr>" % (db.eatables_selection(c.eatable_key), c.unit_count, r["cals"], r["sugar"])
         day_rows = ""
-        for date, day in db.days.items():
+        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>"\
@@ -314,11 +319,9 @@ class MyServer(BaseHTTPRequestHandler):
         self.wfile.write(bytes(page, "utf-8"))
 
 
-hostName = "localhost"
-serverPort = 8081
 if __name__ == "__main__":
     webServer = HTTPServer((hostName, serverPort), MyServer)
-    print("Server started http://%s:%s" % (hostName, serverPort))
+    print(f"Server started http://{hostName}:{serverPort}")
     try:
         webServer.serve_forever()
     except KeyboardInterrupt: