Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace .get_latest_stream_result() with a more general .latest_stream #1880

Merged
merged 5 commits into from
Mar 3, 2025

Conversation

cpsievert
Copy link
Collaborator

@cpsievert cpsievert commented Feb 28, 2025

Follow up to #1846

Coming back to write about this, I changed my mind about get_latest_stream_result(). Having a more general .latest_stream is better, especially once I realized returning a "mock" ExtendedTask could get rid of some of the None/req(False) awkwardness.

Here's that same example from #1846 with the new API. It also adds status reporting and cancelling

import asyncio

from shiny import reactive
from shiny.express import input, render, ui

ui.input_action_button("cancel", "Cancel stream")

chat = ui.Chat("chat")

chat.ui()
chat.update_user_input(value="Press Enter to start the stream")


async def stream_generator():
    for i in range(10):
        await asyncio.sleep(0.25)
        yield f"Message {i} \n\n"


@chat.on_user_submit
async def _(message: str):
    await chat.append_message_stream(stream_generator())


@render.code
def _():
    return f"Status: {chat.latest_stream.status()}"

@render.code
async def stream_result_ui():
    return f"Result: {chat.latest_stream.result()}"


@reactive.effect
@reactive.event(input.cancel)
def _():
    chat.latest_stream.cancel()

@cpsievert cpsievert requested a review from gadenbuie February 28, 2025 23:05
@cpsievert cpsievert added this to the Next Release milestone Feb 28, 2025
@cpsievert cpsievert merged commit ee998f9 into main Mar 3, 2025
41 checks passed
@cpsievert cpsievert deleted the chat-get-message-stream branch March 3, 2025 18:09
@cpsievert cpsievert changed the title Replace .get_latest_stream_result() with a more general .get_message_stream() Replace .get_latest_stream_result() with a more general .latest_stream Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants