forked from splitbrain/bql-label-printer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·144 lines (114 loc) · 4.1 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
"""
Simple Web Interface to create labels on a Brother Printer
"""
import sys
from glob import glob
from os.path import basename
from io import BytesIO
import argparse
from datetime import datetime
from PIL import Image
from brother_ql import BrotherQLRaster, create_label
from brother_ql.backends import backend_factory, guess_backend
from brother_ql.devicedependent import models, label_type_specs, label_sizes
from fastapi import FastAPI, Request, File, Form
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
import uvicorn
app = FastAPI()
templates = Jinja2Templates(directory="templates")
app.mount("/static", StaticFiles(directory="static"), name="static")
DEBUG = False
MODEL = None
BACKEND_CLASS = None
BACKEND_STRING_DESCR = None
LABEL_SIZES = [(name, label_type_specs[name]['name']) for name in label_sizes]
@app.get('/')
async def do_editor(request: Request):
"""
The main editor view
:return:
"""
return templates.TemplateResponse(
'index.html',
{"request": request, "labels": get_labels()}
)
@app.get('/labels')
async def show_labels():
"""
List the available label templates
:return:
"""
filenames = glob(sys.path[0] + '/static/labels/*.html')
filenames.sort()
return [basename(x[:-5]) for x in filenames]
@app.post('/print')
async def do_print(data: bytes = File(...), size: str = Form(...), allow_red: str = Form(...)):
"""
Receive the image from the frontend and print it
:return: string a simple 'ok' when no exception was thrown
"""
print(data)
allow_red = allow_red == "true"
print(f"allow red? {allow_red} {type(allow_red)}")
im = Image.open(BytesIO(data))
timestamp = datetime.timestamp(datetime.now())
filename = f"{timestamp}.png"
im.save(filename)
print(f"Image size: {size}")
print(LABEL_SIZES)
# uncomment me to print to printer
# TODO: add a dev mode?
# Autoscale image size with PIL
if size == "62x29":
im = im.rotate(90, expand=True)
# new_size = label_type_specs[size]["dots_total"]
# im.resize(new_size, Image.ANTIALIAS)
await print_label(im, size, allow_red)
return 'ok'
async def print_label(im, size, allow_red, rotate=True):
qlr = BrotherQLRaster(MODEL)
rotate = 90 if rotate else 0
create_label(qlr, im, size, threshold=70, cut=True, rotate=rotate, red=allow_red)
# noinspection PyCallingNonCallable
be = BACKEND_CLASS(BACKEND_STRING_DESCR)
be.write(qlr.data)
be.dispose()
del be
def get_labels():
"""
List the available label templates
:return:
"""
# get labels from some the disk?
filenames = glob(sys.path[0] + '/static/labels/*.html')
filenames.sort()
return [basename(x[:-5]) for x in filenames]
def main():
"""
Initializes the webserver
:return:
"""
global DEBUG, MODEL, BACKEND_CLASS, BACKEND_STRING_DESCR
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--host', default='127.0.0.1', help='The IP the webserver should bind to. Use 0.0.0.0 for all')
parser.add_argument('--port', default=8013, help='The port the webserver should start on')
parser.add_argument('--debug', action='store_true', default=False, help='Activate local dev debugging')
parser.add_argument('--model', default='QL-500', choices=models, help='The model of your printer (default: QL-500)')
parser.add_argument('printer',
help='String descriptor for the printer to use (like tcp://192.168.0.23:9100 or '
'file:///dev/usb/lp0)')
args = parser.parse_args()
DEBUG = args.debug
MODEL = args.model
try:
selected_backend = guess_backend(args.printer)
BACKEND_CLASS = backend_factory(selected_backend)['backend_class']
BACKEND_STRING_DESCR = args.printer
except:
parser.error("Couldn't guess the backend to use from the printer string descriptor")
uvicorn.run(app, host=args.host, port=int(args.port))
if __name__ == "__main__":
main()