소스 검색

code review

Falko Schindler 1 년 전
부모
커밋
1e12b228d1
3개의 변경된 파일20개의 추가작업 그리고 19개의 파일을 삭제
  1. 1 0
      examples/generate_pdf/.gitignore
  2. 17 17
      examples/generate_pdf/main.py
  3. 2 2
      examples/generate_pdf/requirements.txt

+ 1 - 0
examples/generate_pdf/.gitignore

@@ -0,0 +1 @@
+*.pdf

+ 17 - 17
examples/generate_pdf/main.py

@@ -1,3 +1,4 @@
+#!/usr/bin/env python3
 from io import BytesIO
 from io import BytesIO
 from pathlib import Path
 from pathlib import Path
 
 
@@ -5,10 +6,12 @@ import cairo
 
 
 from nicegui import ui
 from nicegui import ui
 
 
+PDF_PATH = Path('output.pdf')
+
 
 
 def generate_svg() -> str:
 def generate_svg() -> str:
     output = BytesIO()
     output = BytesIO()
-    surface = cairo.SVGSurface(output, 200, 150)
+    surface = cairo.SVGSurface(output, 300, 200)
     draw(surface)
     draw(surface)
     surface.finish()
     surface.finish()
     return output.getvalue().decode('utf-8')
     return output.getvalue().decode('utf-8')
@@ -16,36 +19,33 @@ def generate_svg() -> str:
 
 
 def generate_pdf() -> bytes:
 def generate_pdf() -> bytes:
     output = BytesIO()
     output = BytesIO()
-    surface = cairo.PDFSurface(output, 200, 150)
+    surface = cairo.PDFSurface(output, 300, 200)
     draw(surface)
     draw(surface)
     surface.finish()
     surface.finish()
     return output.getvalue()
     return output.getvalue()
 
 
 
 
-def draw(surface: cairo.SVGSurface):
+def draw(surface: cairo.SVGSurface) -> None:
     context = cairo.Context(surface)
     context = cairo.Context(surface)
-
-    context.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
+    context.select_font_face('Arial', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
     context.set_font_size(20)
     context.set_font_size(20)
     context.move_to(10, 40)
     context.move_to(10, 40)
-    context.show_text(f"{name_input.value}")
+    context.show_text(name.value)
     context.move_to(10, 80)
     context.move_to(10, 80)
-    context.show_text(f"{email_input.value}")
+    context.show_text(email.value)
 
 
 
 
-def update():
+def update() -> None:
     preview.content = generate_svg()
     preview.content = generate_svg()
-    Path('output.pdf').write_bytes(generate_pdf())
+    PDF_PATH.write_bytes(generate_pdf())
 
 
 
 
-with ui.row().classes('items-stretch gap-12'):
-    with ui.column():
-        name_input = ui.input("Name", placeholder="Enter your name", on_change=update)
-        email_input = ui.input("E-Mail", placeholder="Enter your E-Mail address", on_change=update)
-    with ui.column():
-        preview = ui.html().classes('w-[400px] h-[300px] border-2 border-gray-500')
+with ui.row():
     with ui.column():
     with ui.column():
-        ui.button("Download PDF", on_click=lambda: ui.download('output.pdf'))\
-            .bind_visibility_from(preview, 'content')
+        name = ui.input('Name', placeholder='Enter your name', on_change=update)
+        email = ui.input('E-Mail', placeholder='Enter your E-Mail address', on_change=update)
+    preview = ui.html().classes('border-2 border-gray-500')
+    update()
+    ui.button('Download PDF', on_click=lambda: ui.download(PDF_PATH)).bind_visibility_from(name, 'value')
 
 
 ui.run()
 ui.run()

+ 2 - 2
examples/generate_pdf/requirements.txt

@@ -1,2 +1,2 @@
-pycairo
-nicegui
+nicegui
+pycairo