]> git.donarmstrong.com Git - ptouch_web.git/commitdiff
add templates for label printer
authorDon Armstrong <don@donarmstrong.com>
Sat, 23 Mar 2024 21:58:02 +0000 (14:58 -0700)
committerDon Armstrong <don@donarmstrong.com>
Sat, 23 Mar 2024 21:58:02 +0000 (14:58 -0700)
src/ptouch_web/dashapp.py

index 9b3b11d0ff96f6aab68bf76bdafea1152bfea034..0683563be3c6a88e4cd15260e1830900b418ddcd 100644 (file)
@@ -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"