Browse Source

allow passing local file path to ui.download

Rodja Trappe 1 year ago
parent
commit
74c2b54dc1
4 changed files with 35 additions and 15 deletions
  1. 7 4
      nicegui/functions/download.py
  2. 8 0
      tests/conftest.py
  3. 19 11
      tests/test_download.py
  4. 1 0
      tests/test_helpers.py

+ 7 - 4
nicegui/functions/download.py

@@ -1,14 +1,17 @@
-from typing import Optional
+from pathlib import Path
+from typing import Optional, Union
 
 
 from .. import globals
 from .. import globals
 
 
 
 
-def download(url: str, filename: Optional[str] = None) -> None:
+def download(src: Union[str, Path], filename: Optional[str] = None) -> None:
     """Download
     """Download
 
 
     Function to trigger the download of a file.
     Function to trigger the download of a file.
 
 
-    :param url: target URL of the file to download
+    :param src: target URL or local filename which should be downloaded
     :param filename: name of the file to download (default: name of the file on the server)
     :param filename: name of the file to download (default: name of the file on the server)
     """
     """
-    globals.get_client().download(url, filename)
+    if Path(src).is_file():
+        src = globals.app.add_static_file(local_file=src)
+    globals.get_client().download(src, filename)

+ 8 - 0
tests/conftest.py

@@ -1,5 +1,6 @@
 import importlib
 import importlib
 import os
 import os
+import shutil
 from typing import Dict, Generator
 from typing import Dict, Generator
 
 
 import icecream
 import icecream
@@ -10,6 +11,7 @@ from webdriver_manager.chrome import ChromeDriverManager
 
 
 from nicegui import Client, globals
 from nicegui import Client, globals
 from nicegui.page import page
 from nicegui.page import page
+from .test_helpers import DOWNLOAD_DIR
 
 
 from .screen import Screen
 from .screen import Screen
 
 
@@ -21,6 +23,11 @@ def chrome_options(chrome_options: webdriver.ChromeOptions) -> webdriver.ChromeO
     chrome_options.add_argument('headless')
     chrome_options.add_argument('headless')
     chrome_options.add_argument('disable-gpu')
     chrome_options.add_argument('disable-gpu')
     chrome_options.add_argument('window-size=600x600')
     chrome_options.add_argument('window-size=600x600')
+    chrome_options.add_experimental_option('prefs', {
+        "download.default_directory": str(DOWNLOAD_DIR),
+        "download.prompt_for_download": False,  # To auto download the file
+        "download.directory_upgrade": True,
+    })
     return chrome_options
     return chrome_options
 
 
 
 
@@ -71,3 +78,4 @@ def screen(driver: webdriver.Chrome, request: pytest.FixtureRequest, caplog: pyt
     logs = screen.caplog.get_records('call')
     logs = screen.caplog.get_records('call')
     assert not logs, f'There were unexpected logs:\n-------\n{logs}\n-------'
     assert not logs, f'There were unexpected logs:\n-------\n{logs}\n-------'
     screen.stop_server()
     screen.stop_server()
+    shutil.rmtree(DOWNLOAD_DIR)

+ 19 - 11
tests/test_download.py

@@ -1,23 +1,31 @@
-from fastapi import HTTPException
+from pathlib import Path
+from fastapi.responses import PlainTextResponse
 
 
 from nicegui import app, ui
 from nicegui import app, ui
 
 
-from .screen import PORT, Screen
+from .screen import Screen
+from .test_helpers import TEST_DIR, DOWNLOAD_DIR
 
 
+IMAGE_FILE = Path(TEST_DIR).parent / 'examples' / 'slideshow' / 'slides' / 'slide1.jpg'
 
 
-def test_download(screen: Screen):
-    success = False
 
 
-    @app.get('/static/test.py')
+def test_download_text_file(screen: Screen):
+    @app.get('/static/test.txt')
     def test():
     def test():
-        nonlocal success
-        success = True
-        raise HTTPException(404, 'Not found')
+        return PlainTextResponse('test')
 
 
-    ui.button('Download', on_click=lambda: ui.download('static/test.py'))
+    ui.button('Download', on_click=lambda: ui.download('static/test.txt'))
 
 
     screen.open('/')
     screen.open('/')
     screen.click('Download')
     screen.click('Download')
     screen.wait(0.5)
     screen.wait(0.5)
-    assert success
-    screen.assert_py_logger('WARNING', f'http://localhost:{PORT}/static/test.py not found')
+    assert (DOWNLOAD_DIR / 'test.txt').read_text() == 'test'
+
+
+def test_downloading_local_file_as_src(screen: Screen):
+    ui.button('download', on_click=lambda: ui.download(IMAGE_FILE))
+
+    screen.open('/')
+    screen.click('download')
+    screen.wait(0.5)
+    assert (DOWNLOAD_DIR / 'slide1.jpg').exists()

+ 1 - 0
tests/test_helpers.py

@@ -7,6 +7,7 @@ from pathlib import Path
 from nicegui import helpers
 from nicegui import helpers
 
 
 TEST_DIR = Path(__file__).parent
 TEST_DIR = Path(__file__).parent
+DOWNLOAD_DIR = TEST_DIR / 'download'
 
 
 
 
 def test_is_port_open():
 def test_is_port_open():