vinay-jose commited on
Commit
364b57c
·
verified ·
1 Parent(s): 7e2f75b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +147 -0
app.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fasthtml.common import *
2
+ from monsterui.all import *
3
+ import qrcode
4
+ import io
5
+ import base64
6
+ from PIL import Image
7
+
8
+ def generate_qr_code(url, box_size=10, border=2, with_logo=False):
9
+ c = qrcode.constants
10
+ error_correction = c.ERROR_CORRECT_H if with_logo else c.ERROR_CORRECT_L
11
+ qr = qrcode.QRCode(
12
+ version=1,
13
+ error_correction=error_correction,
14
+ box_size=box_size,
15
+ border=border,
16
+ )
17
+ qr.add_data(url)
18
+ qr.make(fit=True)
19
+
20
+ img = qr.make_image(fill_color="black", back_color="white")
21
+ return img
22
+
23
+ def qr_to_base64(qr_img, format="PNG"):
24
+ img_byte_arr = io.BytesIO()
25
+ qr_img.save(img_byte_arr, format=format)
26
+ encoded = base64.b64encode(img_byte_arr.getvalue()).decode('ascii')
27
+ return f"data:image/{format.lower()};base64,{encoded}"
28
+
29
+ def add_logo_to_qr(qr_img, logo, logo_size_percentage=25):
30
+ try:
31
+ contents = logo.file.read()
32
+ image_stream = io.BytesIO(contents)
33
+ logo_img = Image.open(image_stream)
34
+ logo.file.seek(0)
35
+
36
+ # Calculate the size of the logo based on the QR code size
37
+ qr_width, qr_height = qr_img.size
38
+ logo_max_size = min(qr_width, qr_height) * logo_size_percentage // 100
39
+
40
+ # Resize the logo to fit within the QR code
41
+ logo_width, logo_height = logo_img.size
42
+ if logo_width > logo_max_size or logo_height > logo_max_size:
43
+ if logo_width > logo_height:
44
+ logo_height = int(logo_height * logo_max_size / logo_width)
45
+ logo_width = logo_max_size
46
+ else:
47
+ logo_width = int(logo_width * logo_max_size / logo_height)
48
+ logo_height = logo_max_size
49
+ logo_img = logo_img.resize((logo_width, logo_height), Image.LANCZOS)
50
+
51
+ # Calculate position to place the logo (center)
52
+ position = ((qr_width - logo_width) // 2, (qr_height - logo_height) // 2)
53
+
54
+ # Create a new image with the same size as the QR code and paste the logo
55
+ qr_with_logo = qr_img.copy().convert("RGBA")
56
+ qr_with_logo.paste(logo_img, position, logo_img)
57
+
58
+ return qr_with_logo.convert("RGB")
59
+ except Exception as e:
60
+ print(f"Error adding logo: {e}")
61
+ return qr_img
62
+
63
+ # Create FastHTML app
64
+ app, rt = fast_app(hdrs = Theme.green.headers())
65
+
66
+ @rt('/')
67
+ def get():
68
+ # Create a form for URL input
69
+ url = LabelInput(label="URL", placeholder="Enter URL here", id="url")
70
+
71
+ box_size = LabelRange(label='Box size', value='10', min=5, max=20, step=1, name="box_size")
72
+ border_width = LabelRange(label='Border width', value='4', min=0, max=10, step=1, name="border_width")
73
+
74
+ logo_upload = DivVStacked(
75
+ # Label("Logo (Optional)", for_="logo"),
76
+ # Upload("Logo (Optional)", id="logo"),
77
+ LabelInput("Logo (Optional)", type="file", name="logo", id="logo", accept="image/*"),
78
+ P("For best results, use a transparent PNG", cls="text-sm")
79
+ )
80
+
81
+ logo_size = LabelRange(label='Logo Size (%)', value='25', min=5, max=30, step=5, name="logo_size")
82
+
83
+ controls = DivVStacked(DivHStacked(box_size, border_width), logo_upload, logo_size)
84
+
85
+ generate_button = Button((UkIcon("rocket", cls='mr-2'), "Generate"), cls=ButtonT.primary)
86
+
87
+ form = Form(
88
+ Div(url, DivCentered(controls), DivCentered(generate_button), cls='space-y-5'),
89
+ cls='space-x-5',
90
+ hx_post="/generate",
91
+ hx_target="#qr-result",
92
+ hx_swap="innerHTML"
93
+ )
94
+
95
+ # Container for QR code result
96
+ result_div = Div(id="qr-result")
97
+
98
+ # Main page content
99
+ return Title("QR Code Generator"), Main(
100
+ DivVStacked(
101
+ H1("QR Code Generator"),
102
+ Card(
103
+ DivHStacked(
104
+ result_div,
105
+ form,
106
+ cls='space-x-4'
107
+ ),
108
+ cls='space-y-4'
109
+ ),
110
+ cls='py-6 space-y-4'
111
+ )
112
+ )
113
+
114
+ @rt('/generate')
115
+ def post(url: str,
116
+ box_size: int = 10,
117
+ border_width: int = 4,
118
+ logo_size: int | None = 25,
119
+ logo: any = None):
120
+ if not url:
121
+ return Alert(DivLAligned(UkIcon('triangle-alert'),
122
+ P("Please enter a URL.")),
123
+ cls=AlertT.error)
124
+
125
+ # Generate QR code
126
+ if not logo:
127
+ qr_img = generate_qr_code(url, box_size=box_size, border=border_width)
128
+ img_base64 = qr_to_base64(qr_img)
129
+
130
+ else:
131
+ # Generate QR code with logo
132
+ qr_img = generate_qr_code(url, box_size=box_size, border=border_width, with_logo=True)
133
+ qr_img_with_logo = add_logo_to_qr(qr_img, logo, logo_size_percentage=logo_size)
134
+
135
+ # Convert to base64
136
+ img_base64 = qr_to_base64(qr_img_with_logo)
137
+
138
+ # Return the QR code image with a download link
139
+ return DivVStacked(
140
+ Card(Img(src=img_base64, alt="QR Code", cls="qr-image")),
141
+ Button((UkIcon("download", cls='mr-2'), "Download"),
142
+ href=img_base64,
143
+ download=f"qr-{url.replace('://', '-').replace('/', '-')}.png",
144
+ cls=ButtonT.secondary)
145
+ )
146
+
147
+ serve()