From 82e90b1d8e0a0d5f54d15680fecbdb681656999d Mon Sep 17 00:00:00 2001 From: Lendemor Date: Tue, 31 Oct 2023 15:48:32 +0100 Subject: [PATCH] make download work for state vars --- reflex/.templates/web/utils/state.js | 3 ++- reflex/event.py | 16 ++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/reflex/.templates/web/utils/state.js b/reflex/.templates/web/utils/state.js index 1c0c637744..90affc5651 100644 --- a/reflex/.templates/web/utils/state.js +++ b/reflex/.templates/web/utils/state.js @@ -171,7 +171,8 @@ export const applyEvent = async (event, socket) => { const a = document.createElement('a'); a.hidden = true; a.href = event.payload.url; - a.download = event.payload.filename; + if (event.payload.filename) + a.download = event.payload.filename; a.click(); a.remove(); return false; diff --git a/reflex/event.py b/reflex/event.py index 4221832351..176a30f97f 100644 --- a/reflex/event.py +++ b/reflex/event.py @@ -381,7 +381,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. Args: @@ -394,12 +394,16 @@ def download(url: str, filename: Optional[str] = None) -> EventSpec: Returns: EventSpec: An event to download the associated file. """ - if not url.startswith("/"): - raise ValueError("The URL argument should start with a /") + if isinstance(url, Var) and filename is None: + filename = "" - # if filename is not provided, infer it from url - if filename is None: - filename = url.rpartition("/")[-1] + if isinstance(url, str): + if not url.startswith("/"): + 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( "_download",