From dd7fe6f81cd6c33d5b4674fb12262fbe93c92762 Mon Sep 17 00:00:00 2001 From: Don Armstrong Date: Sat, 23 Mar 2024 14:58:02 -0700 Subject: [PATCH] add templates for label printer --- src/ptouch_web/dashapp.py | 119 ++++++++++++++++++++++++++++++-------- 1 file changed, 95 insertions(+), 24 deletions(-) diff --git a/src/ptouch_web/dashapp.py b/src/ptouch_web/dashapp.py index 9b3b11d..0683563 100644 --- a/src/ptouch_web/dashapp.py +++ b/src/ptouch_web/dashapp.py @@ -1,5 +1,6 @@ -from dash import Dash, html, dcc, Input, Output, State, ctx +from dash import Dash, html, dcc, Input, Output, State, ctx, ALL import requests +from datetime import datetime, timedelta app = Dash( __name__, @@ -8,30 +9,32 @@ app = Dash( assets_folder="../../assets/", ) +templates_div = html.Div(id="templates", className="d-flex gap-2", children=[]) app.layout = html.Div( - [ + className="container-md", + children=[ html.Div( - className="mb-3", children=[ + html.Label("Label Text", className="form-label"), + dcc.Textarea( + id="label-text", + style={"width": "100%"}, + className="form-control form-control-lg", + placeholder="Text for label", + rows="5", + ), html.Div( - className="mb-3", - children=[ - html.Label("Label Text", className="form-label"), - dcc.Textarea( - id="label-text", - style={"width": "100%"}, - className="form-control", - placeholder="Text for label", - ), - html.Div( - id="label-help", - className="form-text", - children=["Multiple lines print as shown"], - ), - ], + id="label-help", + className="form-text", + children=["Multiple lines print as shown"], ), + ], + ), + html.Div( + className="row", + children=[ html.Div( - className="mb-3", + className="col-sm-2", children=[ html.Label("Font size", className="form-label"), dcc.Input( @@ -49,7 +52,7 @@ app.layout = html.Div( ], ), html.Div( - className="mb-3", + className="col-sm-2", children=[ html.Label("Copies", className="form-label"), dcc.Input( @@ -62,13 +65,32 @@ app.layout = html.Div( ), ], ), - html.Button( - "Print", name="print", id="print", className="btn btn-primary" + html.Div( + className="col-sm-8 align-self-center", + children=[ + html.Button( + "Print", + name="print", + id="print", + type="submit", + className="btn btn-primary btn-lg", + ) + ], ), - html.Div(id="print-result"), ], ), - ] + html.Div( + className="row", children=[html.Div(id="print-result", className="mb-3")] + ), + html.Div( + id="template-section", + className="container", + children=[ + html.Label("Form Templates", className="form-label"), + templates_div, + ], + ), + ], ) @@ -92,3 +114,52 @@ def print_button(print, label_text, font_size, copies): res = requests.post("http://localhost:8000/v1/queue", json=queue_item) return res.text, 1 + + +def bottle_template(): + today = datetime.now() + tomorrow = today + timedelta(days=1) + return f"BM 4 floz\nexp {today.strftime('%b %d')}\nuse {tomorrow.strftime('%b %d')}" + + +def puree_template(): + tomorrow = datetime.now() + timedelta(days=1) + return f"\nPurée\nuse {tomorrow.strftime('%b %d')}" + + +templates = { + "bottle": {"label": "Bottle", "function": bottle_template}, + "rayleigh": {"label": "Rayleigh", "value": "Rayleigh\nArmstrong-Dodgen"}, + "kepler": {"label": "Kepler", "value": "Kepler\nArmstrong-Dodgen"}, + "puree": {"label": "Purée", "function": puree_template}, +} + +for name, template in templates.items(): + templates_div.children.append( + html.Button( + template["label"], + name=name, + id={"type": "template_button", "name": name}, + className="btn btn-secondary", + ) + ) + + +@app.callback( + Output("label-text", "value"), + Input({"type": "template_button", "name": ALL}, "n_clicks"), +) +def template_button(button): + if ctx.triggered_id is None: + return + if "name" not in ctx.triggered_id: + return + name = ctx.triggered_id["name"] + if name not in templates: + return + template = templates[name] + if "value" in template: + return template["value"] + if "function" in template: + return template["function"]() + return "No template defined" -- 2.39.5