Skip to content

Commit 3a8b7b2

Browse files
authored
fix(MarkdownStream): correctly set the latest stream when it gets invoked (#1931)
1 parent 9ff95ef commit 3a8b7b2

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

shiny/ui/_markdown_stream.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ async def _task():
158158

159159
_task()
160160

161+
self._latest_stream.set(_task)
162+
161163
# Since the task runs in the background (outside/beyond the current context,
162164
# if any), we need to manually raise any exceptions that occur
163165
@reactive.effect
@@ -208,7 +210,7 @@ def get_latest_stream_result(self) -> Union[str, None]:
208210
"The `.get_latest_stream_result()` method is deprecated and will be removed "
209211
"in a future release. Use `.latest_stream.result()` instead. "
210212
)
211-
self.latest_stream.result()
213+
return self.latest_stream.result()
212214

213215
async def clear(self):
214216
"""
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import asyncio
2+
3+
from shiny import reactive
4+
from shiny.express import input, render, ui
5+
6+
stream = ui.MarkdownStream("stream_id")
7+
stream.ui()
8+
9+
10+
ui.input_action_button("do_stream", "Do stream")
11+
12+
13+
async def gen():
14+
yield "Hello "
15+
await asyncio.sleep(0.1)
16+
yield "world!"
17+
18+
19+
@reactive.effect
20+
@reactive.event(input.do_stream)
21+
async def _():
22+
await stream.stream(gen())
23+
24+
25+
@render.code
26+
def stream_result():
27+
res = stream.latest_stream.result()
28+
return f"Stream result: {res}"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from playwright.sync_api import Page, expect
2+
3+
from shiny.playwright import controller
4+
from shiny.run import ShinyAppProc
5+
6+
7+
def test_latest_stream_result(page: Page, local_app: ShinyAppProc) -> None:
8+
page.goto(local_app.url)
9+
10+
stream = page.locator("#stream_id")
11+
stream_result = controller.OutputCode(page, "stream_result")
12+
stream_result.expect_value("")
13+
14+
btn = controller.InputActionButton(page, "do_stream")
15+
btn.click()
16+
17+
expect(stream).to_contain_text("Hello world!")
18+
stream_result.expect_value("Stream result: Hello world!")

0 commit comments

Comments
 (0)