From 9aed58cd807f0d0ee321b8871470658f9484bb51 Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Mon, 29 Jan 2024 16:06:49 -0600 Subject: [PATCH 1/2] Fix render.download in Shiny Express --- shiny/api-examples/download_button/app-express.py | 15 +++++++++++++++ shiny/render/_render.py | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 shiny/api-examples/download_button/app-express.py diff --git a/shiny/api-examples/download_button/app-express.py b/shiny/api-examples/download_button/app-express.py new file mode 100644 index 000000000..96bd64273 --- /dev/null +++ b/shiny/api-examples/download_button/app-express.py @@ -0,0 +1,15 @@ +import asyncio +import random +from datetime import date + +from shiny.express import render + + +@render.download( + filename=lambda: f"新型-{date.today().isoformat()}-{random.randint(100,999)}.csv" +) +async def downloadData(): + await asyncio.sleep(0.25) + yield "one,two,three\n" + yield "新,1,2\n" + yield "型,4,5\n" diff --git a/shiny/render/_render.py b/shiny/render/_render.py index adb9cf6df..81fb7a10c 100644 --- a/shiny/render/_render.py +++ b/shiny/render/_render.py @@ -30,6 +30,7 @@ from .._docstring import add_example, no_example from .._namespaces import ResolvedId from .._typing_extensions import Self +from ..express._mock_session import MockSession from ..session import get_current_session, require_active_session from ..session._session import DownloadHandler, DownloadInfo from ..types import MISSING, MISSING_TYPE, ImgData @@ -688,7 +689,7 @@ def url() -> str: # not being None is because in Express, when the UI is rendered, this function # `render.download()()` called once before any sessions have been started. session = get_current_session() - if session is not None: + if session is not None and not isinstance(session, MockSession): session._downloads[self.output_id] = DownloadInfo( filename=self.filename, content_type=self.media_type, From 70da924ccd4c380c5b56f30de745057f965f8416 Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Mon, 29 Jan 2024 16:09:45 -0600 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 826144ea3..530c45432 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [UNRELEASED] +* Fixed `render.download` not working in Express. (#1085) ## [0.7.0] - 2024-01-25