forked from AnswerDotAI/fasthtml-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsse_clock_plus.py
39 lines (34 loc) · 1.44 KB
/
sse_clock_plus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""This uses sse-starlette to better structure the SSE response."""
import asyncio
from datetime import datetime
from fasthtml.common import *
try:
from sse_starlette.sse import EventSourceResponse
except ImportError:
raise ImportError("Please install sse-starlette with 'pip install sse-starlette'")
sselink = Script(src="https://unpkg.com/htmx-ext-sse@2.2.1/sse.js")
app, rt = fast_app(hdrs=(sselink,))
@rt("/")
def get():
return Titled("SSE Clock",
Span(hx_ext="sse", sse_connect="/time-sender", sse_swap="TimeUpdateEvent")(
P("XX:XX")
)
)
async def time_generator():
while True:
# EventSourceResponse converts this dict to the proper format. Accepts
# data, event, id, retry, comment keys
# See https://github.com/sysid/sse-starlette/blob/main/sse_starlette/sse.py#L78-L94
yield dict(
# Maybe we can patch EventSourceResponse to run to_xml automatically if FT
# components are detected in the data key, so we don't have to do it here
# Note: For data, converts carriage returns to extra `data:` lines in the response
data=to_xml(P(datetime.now().strftime('%H:%M:%S'))),
event="TimeUpdateEvent")
await asyncio.sleep(1)
@rt("/time-sender")
async def get():
"Send time to all connected clients every second"
return EventSourceResponse(time_generator(), media_type="text/event-stream")
serve()