66
77from pyodide .ffi import to_js
88
9+
910class Default (WorkerEntrypoint ):
1011 """
1112 Image Generation Example using Pillow (PIL)
@@ -176,7 +177,7 @@ def generate_placeholder(self, params: dict) -> Response:
176177 draw .line ([(width , 0 ), (0 , height )], fill = text_rgb , width = 2 )
177178
178179 # Draw border
179- draw .rectangle ([(0 , 0 ), (width - 1 , height - 1 )], outline = text_rgb , width = 2 )
180+ draw .rectangle ([(0 , 0 ), (width - 1 , height - 1 )], outline = text_rgb , width = 2 )
180181
181182 # Add dimensions text in the center
182183 text = f"{ width } × { height } "
@@ -197,9 +198,11 @@ def generate_placeholder(self, params: dict) -> Response:
197198 # Draw text with a background for better visibility
198199 padding = 10
199200 draw .rectangle (
200- [(text_x - padding , text_y - padding ),
201- (text_x + text_width + padding , text_y + text_height + padding )],
202- fill = bg_rgb
201+ [
202+ (text_x - padding , text_y - padding ),
203+ (text_x + text_width + padding , text_y + text_height + padding ),
204+ ],
205+ fill = bg_rgb ,
203206 )
204207 draw .text ((text_x , text_y ), text , fill = text_rgb , font = font )
205208
@@ -236,8 +239,14 @@ def generate_chart(self, params: dict) -> Response:
236239 draw = ImageDraw .Draw (image )
237240
238241 # Draw axes
239- draw .line ([(padding , padding ), (padding , height - padding )], fill = (0 , 0 , 0 ), width = 2 ) # Y-axis
240- draw .line ([(padding , height - padding ), (width - padding , height - padding )], fill = (0 , 0 , 0 ), width = 2 ) # X-axis
242+ draw .line (
243+ [(padding , padding ), (padding , height - padding )], fill = (0 , 0 , 0 ), width = 2
244+ ) # Y-axis
245+ draw .line (
246+ [(padding , height - padding ), (width - padding , height - padding )],
247+ fill = (0 , 0 , 0 ),
248+ width = 2 ,
249+ ) # X-axis
241250
242251 # Calculate bar dimensions
243252 num_bars = len (values )
@@ -261,19 +270,29 @@ def generate_chart(self, params: dict) -> Response:
261270 draw .rectangle (
262271 [(x , y ), (x + bar_width , height - padding )],
263272 fill = bar_rgb ,
264- outline = (0 , 0 , 0 )
273+ outline = (0 , 0 , 0 ),
265274 )
266275
267276 # Draw value on top of bar
268277 value_text = str (value )
269278 bbox = draw .textbbox ((0 , 0 ), value_text , font = font )
270279 text_width = bbox [2 ] - bbox [0 ]
271- draw .text ((x + (bar_width - text_width ) // 2 , y - 20 ), value_text , fill = (0 , 0 , 0 ), font = font )
280+ draw .text (
281+ (x + (bar_width - text_width ) // 2 , y - 20 ),
282+ value_text ,
283+ fill = (0 , 0 , 0 ),
284+ font = font ,
285+ )
272286
273287 # Draw label below bar
274288 bbox = draw .textbbox ((0 , 0 ), label , font = font )
275289 text_width = bbox [2 ] - bbox [0 ]
276- draw .text ((x + (bar_width - text_width ) // 2 , height - padding + 5 ), label , fill = (0 , 0 , 0 ), font = font )
290+ draw .text (
291+ (x + (bar_width - text_width ) // 2 , height - padding + 5 ),
292+ label ,
293+ fill = (0 , 0 , 0 ),
294+ font = font ,
295+ )
277296
278297 return self .image_to_response (image , "image/png" )
279298
@@ -291,7 +310,7 @@ def hex_to_rgb(self, hex_color: str) -> tuple:
291310 hex_color = hex_color .lstrip ("#" )
292311
293312 # Convert to RGB
294- return tuple (int (hex_color [i : i + 2 ], 16 ) for i in (0 , 2 , 4 ))
313+ return tuple (int (hex_color [i : i + 2 ], 16 ) for i in (0 , 2 , 4 ))
295314
296315 def image_to_response (self , image : Image .Image , content_type : str ) -> Response :
297316 """
@@ -318,8 +337,8 @@ def image_to_response(self, image: Image.Image, content_type: str) -> Response:
318337 to_js (image_bytes ).buffer ,
319338 headers = {
320339 "Content-Type" : content_type ,
321- "Cache-Control" : "public, max-age=3600"
322- }
340+ "Cache-Control" : "public, max-age=3600" ,
341+ },
323342 )
324343
325344 def show_endpoints (self ) -> Response :
@@ -454,4 +473,4 @@ def show_endpoints(self) -> Response:
454473 </html>
455474 """
456475
457- return Response (html , headers = {"Content-Type" : "text/html" })
476+ return Response (html , headers = {"Content-Type" : "text/html" })
0 commit comments