make download work for state vars (#2092)

This commit is contained in:
Thomas Brandého 2023-11-01 01:08:10 +01:00 committed by GitHub
parent 853a43eaab
commit 96c09b0f6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 7 deletions

View File

@ -171,7 +171,8 @@ export const applyEvent = async (event, socket) => {
const a = document.createElement('a'); const a = document.createElement('a');
a.hidden = true; a.hidden = true;
a.href = event.payload.url; a.href = event.payload.url;
a.download = event.payload.filename; if (event.payload.filename)
a.download = event.payload.filename;
a.click(); a.click();
a.remove(); a.remove();
return false; return false;

View File

@ -434,7 +434,7 @@ def set_clipboard(content: str) -> EventSpec:
) )
def download(url: str, filename: Optional[str] = None) -> EventSpec: def download(url: str | Var, filename: Optional[str | Var] = None) -> EventSpec:
"""Download the file at a given path. """Download the file at a given path.
Args: Args:
@ -447,12 +447,16 @@ def download(url: str, filename: Optional[str] = None) -> EventSpec:
Returns: Returns:
EventSpec: An event to download the associated file. EventSpec: An event to download the associated file.
""" """
if not url.startswith("/"): if isinstance(url, Var) and filename is None:
raise ValueError("The URL argument should start with a /") filename = ""
# if filename is not provided, infer it from url if isinstance(url, str):
if filename is None: if not url.startswith("/"):
filename = url.rpartition("/")[-1] raise ValueError("The URL argument should start with a /")
# if filename is not provided, infer it from url
if filename is None:
filename = url.rpartition("/")[-1]
return server_side( return server_side(
"_download", "_download",