Skip to content

Commit 24f05e5

Browse files
committed
Improve docstring
1 parent 5e822fc commit 24f05e5

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

shiny/ui/_chat.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -595,16 +595,50 @@ async def append_message_context(self):
595595
"""
596596
Message stream context manager.
597597
598-
A context manager for streaming messages into the chat. Note this context
599-
manager can be used in isolation, nested within itself, or used while a
600-
long-running `.append_message_stream()` is in progress.
598+
A context manager for appending streaming messages into the chat. This context
599+
manager can:
600+
601+
1. Be used in isolation to append a new streaming message to the chat.
602+
* Compared to `.append_message_stream()` this method is more flexible but
603+
isn't non-blocking by default (i.e., it doesn't launch an extended task).
604+
2. Be nested within itself
605+
* Nesting is primarily useful for making checkpoints to `.restore()` back
606+
to (see the example below).
607+
3. Be used from within a `.append_message_stream()`
608+
* Useful for inserting additional content from another context into the
609+
stream (e.g., see the note about tool calls below).
601610
602611
Yields
603612
------
604613
:
605614
A `MessageStream` class instance, which has a method for `.append()`ing
606-
message chunks to as well as way to `.restore()` the stream back to it's
607-
initial state.
615+
message content chunks to as well as way to `.restore()` the stream back to
616+
it's initial state. Note that `.append()` supports the same message content
617+
types as `.append_message()`.
618+
619+
Example
620+
-------
621+
```python
622+
import asyncio
623+
624+
from shiny import reactive
625+
from shiny.express import ui
626+
627+
chat = ui.Chat(id="my_chat")
628+
chat.ui()
629+
630+
@reactive.effect
631+
async def _():
632+
async with chat.append_message_context() as msg:
633+
await msg.append("Starting stream...\n\nProgress:")
634+
async with chat.append_message_context() as progress:
635+
for x in [0, 50, 100]:
636+
await progress.append(f" {x}%")
637+
await asyncio.sleep(1)
638+
await progress.restore()
639+
await msg.restore()
640+
await msg.append("Completed stream")
641+
```
608642
609643
Note
610644
----

0 commit comments

Comments
 (0)