Skip to content

Commit

Permalink
code review; merge download and download_raw
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Nov 22, 2023
1 parent e1854fb commit 9dbf8b9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 43 deletions.
10 changes: 3 additions & 7 deletions nicegui/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,9 @@ def open(self, target: Union[Callable[..., Any], str], new_tab: bool = False) ->
path = target if isinstance(target, str) else self.page_routes[target]
outbox.enqueue_message('open', {'path': path, 'new_tab': new_tab}, self.id)

def download(self, url: str, filename: Optional[str] = None) -> None:
"""Download a file from the given URL."""
outbox.enqueue_message('download', {'url': url, 'filename': filename}, self.id)

def download_bytes(self, data: bytes, filename: Optional[str] = None) -> None:
"""Download a file from the given bytes."""
outbox.enqueue_message('download_bytes', {'data': data, 'filename': filename}, self.id)
def download(self, src: Union[str, bytes], filename: Optional[str] = None) -> None:
"""Download a file from a given URL or raw bytes."""
outbox.enqueue_message('download', {'src': src, 'filename': filename}, self.id)

def on_connect(self, handler: Union[Callable[..., Any], Awaitable]) -> None:
"""Register a callback to be called when the client connects."""
Expand Down
26 changes: 8 additions & 18 deletions nicegui/functions/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,17 @@
from .. import context, core, helpers


def download(src: Union[str, Path], filename: Optional[str] = None) -> None:
def download(src: Union[str, Path, bytes], filename: Optional[str] = None) -> None:
"""Download
Function to trigger the download of a file.
Function to trigger the download of a file, URL or bytes.
:param src: target URL or local path of the file which should be downloaded
:param src: target URL, local path of a file or raw data which should be downloaded
:param filename: name of the file to download (default: name of the file on the server)
"""
if helpers.is_file(src):
src = core.app.add_static_file(local_file=src, single_use=True)
else:
src = str(src)
if not isinstance(src, bytes):
if helpers.is_file(src):
src = core.app.add_static_file(local_file=src, single_use=True)
else:
src = str(src)
context.get_client().download(src, filename)


def download_raw(data: bytes, filename: Optional[str] = None) -> None:
"""Download
Function to trigger the download of a file from raw data.
:param data: raw data of a file which should be downloaded
:param filename: name of the file to download (default: browser generated name)
"""
context.get_client().download_bytes(data, filename)
22 changes: 6 additions & 16 deletions nicegui/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -208,26 +208,17 @@
});
}

function download(url, filename) {
function download(src, filename) {
const anchor = document.createElement("a");
anchor.href = url;
anchor.href = typeof src === "string" ? src : URL.createObjectURL(new Blob([src]));
anchor.target = "_blank";
anchor.download = filename || "";
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
}

function download_bytes(data, filename) {
const anchor = document.createElement("a");
const url = URL.createObjectURL(new Blob([data]));
anchor.href = url;
anchor.target = "_blank";
anchor.download = filename || "";
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
URL.revokeObjectURL(url);
if (typeof src !== "string") {
URL.revokeObjectURL(url);
}
}

async function loadDependencies(element) {
Expand Down Expand Up @@ -309,8 +300,7 @@
const target = msg.new_tab ? '_blank' : '_self';
window.open(url, target);
},
download: (msg) => download(msg.url, msg.filename),
download_bytes: (msg) => download_bytes(msg.data, msg.filename),
download: (msg) => download(msg.src, msg.filename),
notify: (msg) => Quasar.Notify.create(msg),
};
const socketMessageQueue = [];
Expand Down
2 changes: 1 addition & 1 deletion nicegui/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
from .elements.tree import Tree as tree
from .elements.upload import Upload as upload
from .elements.video import Video as video
from .functions.download import download, download_raw
from .functions.download import download
from .functions.html import add_body_html, add_head_html
from .functions.javascript import run_javascript
from .functions.notify import notify
Expand Down
2 changes: 1 addition & 1 deletion tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_downloading_local_file_as_src(screen: Screen):


def test_download_raw_data(screen: Screen):
ui.button('download', on_click=lambda: ui.download_raw(b'test', 'test.txt'))
ui.button('download', on_click=lambda: ui.download(b'test', 'test.txt'))

screen.open('/')
screen.click('download')
Expand Down

0 comments on commit 9dbf8b9

Please sign in to comment.