From: Christian Heller <c.heller@plomlompom.de> Date: Mon, 2 Oct 2023 19:11:52 +0000 (+0200) Subject: Various improvements to calories counter. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/static/bar%20baz.html?a=commitdiff_plain;h=26462d618eea05e4f4f468d34a9ff875ad1ebb4a;p=misc Various improvements to calories counter. --- diff --git a/calories.py b/calories.py index 771b11a..0f65aeb 100644 --- a/calories.py +++ b/calories.py @@ -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)) diff --git a/income_progress_bars.py b/income_progress_bars.py index 5a4f5ad..af2afd4 100644 --- a/income_progress_bars.py +++ b/income_progress_bars.py @@ -3,7 +3,7 @@ import os import json hostName = "localhost" -serverPort = 8080 +serverPort = 8081 header = """<html> <meta charset="UTF-8">