|
1 | 1 | from __future__ import annotations as _annotations |
2 | 2 |
|
3 | 3 | import asyncio |
4 | | -from datetime import datetime |
5 | | -from typing import AsyncIterable |
6 | 4 |
|
7 | 5 | from fastapi import APIRouter |
8 | | -from fastapi.responses import StreamingResponse |
9 | 6 | from fastui import AnyComponent, FastUI |
10 | 7 | from fastui import components as c |
11 | 8 | from fastui.events import GoToEvent, PageEvent |
@@ -144,7 +141,12 @@ class Delivery(BaseModel): |
144 | 141 | c.Div( |
145 | 142 | components=[ |
146 | 143 | c.Heading(text='Server Load SSE', level=2), |
147 | | - c.Markdown(text='`ServerLoad` can also be used to load content from an SSE stream.'), |
| 144 | + c.Markdown( |
| 145 | + text=( |
| 146 | + '`ServerLoad` can also be used to load content from an SSE stream.\n\n' |
| 147 | + "Here the response is the streamed output from OpenAI's GPT-4 chat model." |
| 148 | + ) |
| 149 | + ), |
148 | 150 | c.Button(text='Load SSE content', on_click=PageEvent(name='server-load-sse')), |
149 | 151 | c.Div( |
150 | 152 | components=[ |
@@ -219,21 +221,3 @@ class Delivery(BaseModel): |
219 | 221 | async def modal_view() -> list[AnyComponent]: |
220 | 222 | await asyncio.sleep(0.5) |
221 | 223 | return [c.Paragraph(text='This is some dynamic content. Open devtools to see me being fetched from the server.')] |
222 | | - |
223 | | - |
224 | | -async def sse_generator() -> AsyncIterable[str]: |
225 | | - while True: |
226 | | - d = datetime.now() |
227 | | - m = FastUI( |
228 | | - root=[ |
229 | | - c.Div(components=[c.Text(text=f'Time {d:%H:%M:%S}')], class_name='font-monospace'), |
230 | | - c.Paragraph(text='This content is updated every second using an SSE stream.'), |
231 | | - ] |
232 | | - ) |
233 | | - yield f'data: {m.model_dump_json(by_alias=True)}\n\n' |
234 | | - await asyncio.sleep(1) |
235 | | - |
236 | | - |
237 | | -@router.get('/sse') |
238 | | -async def sse_experiment() -> StreamingResponse: |
239 | | - return StreamingResponse(sse_generator(), media_type='text/event-stream') |
0 commit comments