Skip to content

Commit 4ddeb6c

Browse files
committed
Update examples to use render.download
1 parent cea58ea commit 4ddeb6c

File tree

5 files changed

+18
-19
lines changed
  • examples/annotation-export
  • shiny/api-examples
  • tests/playwright/shiny/bugs/0696-resolve-id

5 files changed

+18
-19
lines changed

examples/annotation-export/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def annotations():
109109
df = df.loc[df["annotation"] != ""]
110110
return df
111111

112-
@session.download(filename="data.csv")
112+
@render.download(filename="data.csv")
113113
def download():
114114
yield annotated_data().to_csv()
115115

shiny/api-examples/download/app.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import matplotlib.pyplot as plt
88
import numpy as np
99

10-
from shiny import App, Inputs, Outputs, Session, ui
10+
from shiny import App, Inputs, Outputs, Session, render, ui
1111

1212

1313
def make_example(id: str, label: str, title: str, desc: str, extra: Any = None):
@@ -77,7 +77,7 @@ def make_example(id: str, label: str, title: str, desc: str, extra: Any = None):
7777

7878

7979
def server(input: Inputs, output: Outputs, session: Session):
80-
@session.download()
80+
@render.download()
8181
def download1():
8282
"""
8383
This is the simplest case. The implementation simply returns the name of a file.
@@ -88,12 +88,12 @@ def download1():
8888
path = os.path.join(os.path.dirname(__file__), "mtcars.csv")
8989
return path
9090

91-
@session.download(filename="image.png")
91+
@render.download(filename="image.png")
9292
def download2():
9393
"""
9494
Another way to implement a file download is by yielding bytes; either all at
9595
once, like in this case, or by yielding multiple times. When using this
96-
approach, you should pass a filename argument to @session.download, which
96+
approach, you should pass a filename argument to @render.download, which
9797
determines what the browser will name the downloaded file.
9898
"""
9999

@@ -107,7 +107,7 @@ def download2():
107107
plt.savefig(buf, format="png")
108108
yield buf.getvalue()
109109

110-
@session.download(
110+
@render.download(
111111
filename=lambda: f"新型-{date.today().isoformat()}-{np.random.randint(100,999)}.csv"
112112
)
113113
async def download3():
@@ -116,7 +116,8 @@ async def download3():
116116
yield "新,1,2\n"
117117
yield "型,4,5\n"
118118

119-
@session.download(id="download4", filename="failuretest.txt")
119+
@output(id="download4")
120+
@render.download(filename="failuretest.txt")
120121
async def _():
121122
yield "hello"
122123
raise Exception("This error was caused intentionally")

shiny/api-examples/download_button/app.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import asyncio
2+
import random
23
from datetime import date
34

4-
import numpy as np
5-
6-
from shiny import App, Inputs, Outputs, Session, ui
5+
from shiny import App, Inputs, Outputs, Session, render, ui
76

87
app_ui = ui.page_fluid(
98
ui.download_button("downloadData", "Download"),
109
)
1110

1211

1312
def server(input: Inputs, output: Outputs, session: Session):
14-
@session.download(
15-
filename=lambda: f"新型-{date.today().isoformat()}-{np.random.randint(100,999)}.csv"
13+
@render.download(
14+
filename=lambda: f"新型-{date.today().isoformat()}-{random.randint(100,999)}.csv"
1615
)
1716
async def downloadData():
1817
await asyncio.sleep(0.25)

shiny/api-examples/download_link/app.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import asyncio
2+
import random
23
from datetime import date
34

4-
import numpy as np
5-
6-
from shiny import App, Inputs, Outputs, Session, ui
5+
from shiny import App, Inputs, Outputs, Session, render, ui
76

87
app_ui = ui.page_fluid(
98
ui.download_link("downloadData", "Download"),
109
)
1110

1211

1312
def server(input: Inputs, output: Outputs, session: Session):
14-
@session.download(
15-
filename=lambda: f"新型-{date.today().isoformat()}-{np.random.randint(100,999)}.csv"
13+
@render.download(
14+
filename=lambda: f"新型-{date.today().isoformat()}-{random.randint(100,999)}.csv"
1615
)
1716
async def downloadData():
1817
await asyncio.sleep(0.25)

tests/playwright/shiny/bugs/0696-resolve-id/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def out_ui():
258258

259259
download_button_count = 0
260260

261-
@session.download(filename=lambda: f"download_button-{session.ns}.csv")
261+
@render.download(filename=lambda: f"download_button-{session.ns}.csv")
262262
async def download_button():
263263
nonlocal download_button_count
264264
download_button_count += 1
@@ -267,7 +267,7 @@ async def download_button():
267267

268268
download_link_count = 0
269269

270-
@session.download(filename=lambda: f"download_link-{session.ns}.csv")
270+
@render.download(filename=lambda: f"download_link-{session.ns}.csv")
271271
async def download_link():
272272
nonlocal download_link_count
273273
download_link_count += 1

0 commit comments

Comments
 (0)