| 
 | 1 | +import asyncio  | 
 | 2 | + | 
 | 3 | +from shiny import reactive  | 
 | 4 | +from shiny.express import input, render, ui  | 
 | 5 | + | 
 | 6 | +SLEEP_TIME = 0.25  | 
 | 7 | + | 
 | 8 | +ui.page_opts(title="Hello chat message streams")  | 
 | 9 | + | 
 | 10 | +with ui.sidebar(style="height:100%"):  | 
 | 11 | +    ui.input_action_button("stream_1", "Stream 1")  | 
 | 12 | +    ui.input_action_button("stream_2", "Stream 2")  | 
 | 13 | +    ui.input_action_button("stream_3", "Stream 3")  | 
 | 14 | +    ui.input_action_button("stream_4", "Stream 4")  | 
 | 15 | +    ui.input_action_button("stream_5", "Stream 5")  | 
 | 16 | +    ui.input_action_button("stream_6", "Stream 6")  | 
 | 17 | + | 
 | 18 | +    ui.h6("Message state:", class_="mt-auto mb-0")  | 
 | 19 | + | 
 | 20 | +    @render.code  | 
 | 21 | +    def message_state():  | 
 | 22 | +        return str(chat.messages())  | 
 | 23 | + | 
 | 24 | + | 
 | 25 | +chat = ui.Chat(id="chat")  | 
 | 26 | +chat.ui()  | 
 | 27 | + | 
 | 28 | + | 
 | 29 | +@chat.on_user_submit  | 
 | 30 | +async def _(user_input: str):  | 
 | 31 | +    await chat.append_message(f"You said: {user_input}")  | 
 | 32 | + | 
 | 33 | + | 
 | 34 | +@reactive.effect  | 
 | 35 | +@reactive.event(input.stream_1)  | 
 | 36 | +async def _():  | 
 | 37 | +    async with chat.append_message_context() as msg:  | 
 | 38 | +        await msg.append("Basic")  | 
 | 39 | +        await asyncio.sleep(SLEEP_TIME)  | 
 | 40 | +        await msg.append(" stream")  | 
 | 41 | + | 
 | 42 | + | 
 | 43 | +@reactive.effect  | 
 | 44 | +@reactive.event(input.stream_2)  | 
 | 45 | +async def _():  | 
 | 46 | +    async with chat.append_message_context() as msg:  | 
 | 47 | +        await msg.append("Basic")  | 
 | 48 | +        await asyncio.sleep(SLEEP_TIME)  | 
 | 49 | +        await msg.append(" stream")  | 
 | 50 | +        await asyncio.sleep(SLEEP_TIME)  | 
 | 51 | +        await msg.restore()  | 
 | 52 | +        await asyncio.sleep(SLEEP_TIME)  | 
 | 53 | +        await msg.append("Finished")  | 
 | 54 | + | 
 | 55 | + | 
 | 56 | +@reactive.effect  | 
 | 57 | +@reactive.event(input.stream_3)  | 
 | 58 | +async def _():  | 
 | 59 | +    async with chat.append_message_context() as outer:  | 
 | 60 | +        await outer.append("Outer start")  | 
 | 61 | +        await asyncio.sleep(SLEEP_TIME)  | 
 | 62 | +        async with chat.append_message_context() as inner:  | 
 | 63 | +            await inner.append("Inner start")  | 
 | 64 | +            await asyncio.sleep(SLEEP_TIME)  | 
 | 65 | +            await inner.append("Inner end")  | 
 | 66 | +        await asyncio.sleep(SLEEP_TIME)  | 
 | 67 | +        await outer.append("Outer end")  | 
 | 68 | + | 
 | 69 | + | 
 | 70 | +@reactive.effect  | 
 | 71 | +@reactive.event(input.stream_4)  | 
 | 72 | +async def _():  | 
 | 73 | +    async with chat.append_message_context() as outer:  | 
 | 74 | +        await outer.append("Outer start")  | 
 | 75 | +        await asyncio.sleep(SLEEP_TIME)  | 
 | 76 | +        async with chat.append_message_context() as inner:  | 
 | 77 | +            await inner.append("Inner start")  | 
 | 78 | +            await asyncio.sleep(SLEEP_TIME)  | 
 | 79 | +            await inner.restore()  | 
 | 80 | +            await inner.append("Inner end")  | 
 | 81 | +        await asyncio.sleep(SLEEP_TIME)  | 
 | 82 | +        await outer.append("Outer end")  | 
 | 83 | + | 
 | 84 | + | 
 | 85 | +@reactive.effect  | 
 | 86 | +@reactive.event(input.stream_5)  | 
 | 87 | +async def _():  | 
 | 88 | +    async with chat.append_message_context() as outer:  | 
 | 89 | +        await outer.append("Outer start")  | 
 | 90 | +        await asyncio.sleep(SLEEP_TIME)  | 
 | 91 | +        await outer.restore()  | 
 | 92 | +        async with chat.append_message_context() as inner:  | 
 | 93 | +            await inner.append("Inner start")  | 
 | 94 | +            await asyncio.sleep(SLEEP_TIME)  | 
 | 95 | +            await inner.append("Inner end")  | 
 | 96 | +        await asyncio.sleep(SLEEP_TIME)  | 
 | 97 | +        await outer.append("Outer end")  | 
 | 98 | + | 
 | 99 | + | 
 | 100 | +@reactive.effect  | 
 | 101 | +@reactive.event(input.stream_6)  | 
 | 102 | +async def _():  | 
 | 103 | +    await chat.append_message_stream(outer_stream())  | 
 | 104 | + | 
 | 105 | + | 
 | 106 | +async def outer_stream():  | 
 | 107 | +    yield "Outer start"  | 
 | 108 | +    await asyncio.sleep(SLEEP_TIME)  | 
 | 109 | +    await inner_stream()  | 
 | 110 | +    await asyncio.sleep(SLEEP_TIME)  | 
 | 111 | +    yield "Outer end"  | 
 | 112 | + | 
 | 113 | + | 
 | 114 | +async def inner_stream():  | 
 | 115 | +    async with chat.append_message_context() as stream:  | 
 | 116 | +        await stream.append("Inner start")  | 
 | 117 | +        await asyncio.sleep(SLEEP_TIME)  | 
 | 118 | +        await stream.append("Inner progress")  | 
 | 119 | +        await stream.restore()  | 
 | 120 | +        await stream.append("Inner end")  | 
0 commit comments