Skip to content

Commit 214dff4

Browse files
authored
Add .latest_stream to ui.MarkdownStream() and deprecate .get_latest_stream_result() (#1884)
1 parent e1d4a50 commit 214dff4

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to Shiny for Python will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [UNRELEASED]
9+
10+
### Changes
11+
12+
* The `.get_latest_stream_result()` method on `ui.MarkdownStream()` was deprecated in favor of the new `.latest_stream` property. Call `.result()` on the property to get the latest result, `.status` to check the status, and `.cancel()` to cancel the stream.
13+
814
## [1.3.0] - 2025-03-03
915

1016
### New features

shiny/ui/_markdown_stream.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from htmltools import css
55

66
from .. import _utils, reactive
7+
from .._deprecated import warn_deprecated
78
from .._docstring import add_example
89
from .._namespaces import resolve_id
910
from .._typing_extensions import TypedDict
@@ -87,9 +88,14 @@ def __init__(
8788
self.on_error = on_error
8889

8990
with session_context(self._session):
90-
self._latest_stream: reactive.Value[
91-
Union[reactive.ExtendedTask[[], str], None]
92-
] = reactive.Value(None)
91+
92+
@reactive.extended_task
93+
async def _mock_task() -> str:
94+
return ""
95+
96+
self._latest_stream: reactive.Value[reactive.ExtendedTask[[], str]] = (
97+
reactive.Value(_mock_task)
98+
)
9399

94100
async def stream(
95101
self,
@@ -151,32 +157,46 @@ async def _handle_error():
151157

152158
return _task
153159

154-
def get_latest_stream_result(self) -> Union[str, None]:
160+
@property
161+
def latest_stream(self):
155162
"""
156-
Reactively read the latest stream result.
163+
React to changes in the latest stream.
164+
165+
Reactively reads for the :class:`~shiny.reactive.ExtendedTask` behind the
166+
latest stream.
157167
158-
This method reads a reactive value containing the result of the latest
159-
`.stream()`. Therefore, this method must be called in a reactive context (e.g.,
160-
a render function, a :func:`~shiny.reactive.calc`, or a
161-
:func:`~shiny.reactive.effect`).
168+
From the return value (i.e., the extended task), you can then:
169+
170+
1. Reactively read for the final `.result()`.
171+
2. `.cancel()` the stream.
172+
3. Check the `.status()` of the stream.
162173
163174
Returns
164175
-------
165176
:
166-
The result of the latest stream (a string).
177+
An extended task that represents the streaming task. The `.result()` method
178+
of the task can be called in a reactive context to get the final state of the
179+
stream.
167180
168-
Raises
169-
------
170-
:
171-
A silent exception if no stream has completed yet.
181+
Note
182+
----
183+
If no stream has yet been started when this method is called, then it returns an
184+
extended task with `.status()` of `"initial"` and that it status doesn't change
185+
state until a message is streamed.
172186
"""
173-
stream = self._latest_stream()
174-
if stream is None:
175-
from .. import req
187+
return self._latest_stream()
176188

177-
req(False)
178-
else:
179-
return stream.result()
189+
def get_latest_stream_result(self) -> Union[str, None]:
190+
"""
191+
Reactively read the latest stream result.
192+
193+
Deprecated. Use `latest_stream.result()` instead.
194+
"""
195+
warn_deprecated(
196+
"The `.get_latest_stream_result()` method is deprecated and will be removed "
197+
"in a future release. Use `.latest_stream.result()` instead. "
198+
)
199+
self.latest_stream.result()
180200

181201
async def clear(self):
182202
"""

0 commit comments

Comments
 (0)