X-Git-Url: https://plomlompom.com/repos/berlin_corona.txt?a=blobdiff_plain;ds=inline;f=calories.py;h=5f710878460e2cd4a11599eb9850f89f60e7d117;hb=f60cdbddc6395eef3c5ebb69fbbb35797dd76532;hp=54fb64e2d94be3f98ce2a743823c2c3db241a60a;hpb=8d8bb938a9d2248bd44ed53fdb0b87eff9ee2dd9;p=misc
diff --git a/calories.py b/calories.py
index 54fb64e..5f71087 100644
--- a/calories.py
+++ b/calories.py
@@ -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 """
@@ -26,7 +29,7 @@ td, th {
|
- |
+ |
|
@@ -59,7 +62,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);
}
@@ -155,18 +158,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 += '' % (k, selected, v.title)
+ html += '' % (k, v.title)
return html
def add_eatable(self, id_, eatable):
@@ -222,7 +225,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)
@@ -250,7 +253,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
@@ -284,24 +287,26 @@ class MyServer(BaseHTTPRequestHandler):
" | "\
"" % (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 += "
"\
- ""\
- " | "\
- " | "\
+ ""\
+ "%s | "\
+ " | "\
" | "\
"%.1f | "\
"%.1f | "\
- "" % (db.eatables_selection(c.eatable_key), c.unit_count, r["cals"], r["sugar"])
+ "" % (c.eatable_key, db.eatables[c.eatable_key].title, c.unit_count, r["cals"], r["sugar"])
day_rows = ""
- for date, day in db.days.items():
- day_rows += ""\
+ for date in sorted(db.days.keys()):
+ day = db.days[date]
+ day_rows = "
"\
"%s | "\
"%.1f | "\
"%.1f | "\
- "
" % (date, date[:10], day.calories, day.calories, day.sugar_g, day.sugar_g)
- day_rows += ""\
+ "
" % (date, date[:10], day.calories, day.calories, day.sugar_g, day.sugar_g) + day_rows
+ day_rows = ""\
"today: | | | archive? | "\
"
"\
""\
@@ -309,16 +314,14 @@ class MyServer(BaseHTTPRequestHandler):
"%.1f | "\
"%.1f | "\
" | "\
- "
" % (db.today_date, db.today.calories, db.today.calories, db.today.sugar_g, db.today.sugar_g)
+ "" % (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)
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: