瀏覽代碼

code review

Falko Schindler 1 年之前
父節點
當前提交
b994f3f884
共有 1 個文件被更改,包括 11 次插入13 次删除
  1. 11 13
      examples/download_text_as_file/main.py

+ 11 - 13
examples/download_text_as_file/main.py

@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
-from io import StringIO
-from uuid import uuid4
+import io
+import uuid
 
 from fastapi.responses import StreamingResponse
 
@@ -9,19 +9,17 @@ from nicegui import Client, app, ui
 
 @ui.page('/')
 async def index(client: Client):
-    download_path = f"/download/{uuid4()}.txt"
+    download_path = f'/download/{uuid.uuid4()}.txt'
 
     @app.get(download_path)
-    async def download():
-        # create a file-like object from the string
-        string_io = StringIO(text_entry.value)
-        return StreamingResponse(
-            string_io, media_type="text/plain",
-            headers={'Content-Disposition': 'attachment; filename=download.txt', }
-        )
-
-    text_entry = ui.textarea(value="hello world")
-    ui.button("Download", on_click=lambda: ui.download(download_path))
+    def download():
+        string_io = io.StringIO(textarea.value)  # create a file-like object from the string
+        headers = {'Content-Disposition': 'attachment; filename=download.txt'}
+        return StreamingResponse(string_io, media_type='text/plain', headers=headers)
+
+    textarea = ui.textarea(value='Hello World!')
+    ui.button('Download', on_click=lambda: ui.download(download_path))
+
     # cleanup the download route after the client disconnected
     await client.disconnected()
     app.routes[:] = [route for route in app.routes if route.path != download_path]