-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabelmaker.py
More file actions
109 lines (95 loc) · 3.33 KB
/
Copy pathlabelmaker.py
File metadata and controls
109 lines (95 loc) · 3.33 KB
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
#!/usr/bin/env python3
from flask import Flask, request, render_template, jsonify
from labelle.lib.devices.device_manager import (
DeviceManager,
DeviceManagerNoDevices
)
from labelle.lib.constants import PIXELS_PER_MM
from labelle.lib.devices.dymo_labeler import DymoLabeler
from labelle.lib.render_engines import (
HorizontallyCombinedRenderEngine,
RenderContext,
RenderEngine,
TextRenderEngine,
PrintPayloadRenderEngine,
)
from labelle.lib.constants import (
Direction,
DEFAULT_MARGIN_PX,
)
from labelle.lib.font_config import (
DefaultFontStyle,
get_font_path,
)
from math import ceil
app = Flask(__name__)
def get_device_manager() -> DeviceManager:
device_manager = DeviceManager()
try:
device_manager.scan()
except DeviceManagerNoDevices as e:
print(f'Error with DeviceManager: {e}')
return device_manager
def print_text(text: str, printOut: bool, tape_size_mm=9) -> str:
device_manager = get_device_manager()
device = device_manager.find_and_select_device()
device.setup()
dymo_labeler = DymoLabeler(tape_size_mm=tape_size_mm, device=device)
# use deault font
font_path = get_font_path(font=None, style=DefaultFontStyle)
lines = len(text.splitlines())
render_engines: list[RenderEngine] = []
render_engines.append(
TextRenderEngine(
text_lines=text,
font_file_name=font_path,
frame_width_px=0,
font_size_ratio=1/lines,
align=Direction.LEFT,
)
)
render_engine = HorizontallyCombinedRenderEngine(render_engines)
render_context = RenderContext(
background_color="white",
foreground_color="black",
height_px=dymo_labeler.height_px,
preview_show_margins=False,
)
render = PrintPayloadRenderEngine(
render_engine=render_engine,
justify=Direction.LEFT,
visible_horizontal_margin_px=DEFAULT_MARGIN_PX,
labeler_margin_px=dymo_labeler.labeler_margin_px,
max_width_px=None,
min_width_px=0,
)
bitmap, _ = render.render_with_meta(render_context)
if printOut:
dymo_labeler.print(bitmap)
label_length = bitmap.size[0] / PIXELS_PER_MM
margin = DEFAULT_MARGIN_PX / PIXELS_PER_MM * 2
notStr = "<strong>not</strong> "
label_fit = "" if label_length < 32 else notStr
label_fit_cut = "" if (label_length - margin) < 32 else notStr
return {'label_length': f"{ceil(label_length):3.0f}",
'label_fit': label_fit,
'label_fit_cut': label_fit_cut}
@app.route('/', methods=['GET', 'POST'])
def handle_text():
output = {'error-msg': ""}
if request.method == 'POST':
text_content = request.form['text_content']
printOut = True if request.form['submit_type'] == 'manual' else False
# Call the method that handles the text content
try:
output = print_text(text_content, printOut)
except Exception as e:
output['error_msg'] = f"Error: {e}"
return jsonify(result=output)
return render_template('template.html',
label_length="",
label_fit="",
label_fit_cut="",
error_msg="")
if __name__ == '__main__':
app.run(debug=True)