Skip to content

Commit 3a2f751

Browse files
committed
new ws ext syntax
1 parent f108d01 commit 3a2f751

File tree

5 files changed

+11
-11
lines changed

5 files changed

+11
-11
lines changed

02_chatbot/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ In this version, when a client sends a message, the server immediately sends bac
8989

9090
## Websockets
9191

92-
As an alternative, we can use the `websockets` [extension of htmx](https://v1.htmx.org/extensions/web-sockets/) to handle communication between the client and the server. To do this, add the requisite headers by passing `ws_hdr=True` to the FastHTML constructor, and then use the `hx_ext="ws"` attribute in the form to specify that the form should be submitted via websockets. We specify the route, and then in the form use `ws_send=""` so that it sends a message to the websocket (as opposed to the default behavior of sending the form data):
92+
As an alternative, we can use the `websockets` [extension of htmx](https://v1.htmx.org/extensions/web-sockets/) to handle communication between the client and the server. To do this, add the requisite headers by passing `exts='ws'` to the FastHTML constructor, and then use the `hx_ext="ws"` attribute in the form to specify that the form should be submitted via websockets. We specify the route, and then in the form use `ws_send=""` so that it sends a message to the websocket (as opposed to the default behavior of sending the form data):
9393

9494
```python
9595
Form(Group(ChatInput(), Button("Send", cls="btn btn-primary")),
@@ -115,7 +115,7 @@ async def ws(msg:str, send):
115115
await send(Div(ChatMessage(messages[-1]), hx_swap_oob='beforeend', id="chatlist"))
116116
```
117117

118-
For more on websockets, see the [htmx documentation](https://v1.htmx.org/extensions/web-sockets/), the ['Game Of Life' example](https://github.com/AnswerDotAI/fasthtml-example/tree/main/00_game_of_life), the ['basic_ws.py` example](https://github.com/AnswerDotAI/fasthtml/blob/main/examples/basic_ws.py) or the [Starlette documentation](https://www.starlette.io/websockets/).
118+
For more on websockets, see the [htmx documentation](https://v1.htmx.org/extensions/web-sockets/), the ['Game Of Life' example](https://github.com/AnswerDotAI/fasthtml-example/tree/main/00_game_of_life), the [`basic_ws.py` example](https://github.com/AnswerDotAI/fasthtml/blob/main/examples/basic_ws.py) or the [Starlette documentation](https://www.starlette.io/websockets/).
119119

120120
When streaming the response from the model, we could repeatedly send a chat message with the content so far, replacing the previous message. This looks fine but has some subtle issues, for example if the user starts selecting text while the message is still streaming in, the selection will be lost when the message is updated. Instead, we can add new content to the end of the message with something like this (as shown in `ws_streaming.py`):
121121

@@ -131,7 +131,7 @@ A final note: in cases like this where the server sends multiple messages to the
131131

132132
Using `Transfer-Encoding: chunked` is a way to send a response in chunks, which can be useful when the response is large and you want to start sending data to the client before the entire response is ready. This version is not much different from the [`basic.py example`], but instead of receiving the entire response from the chat model, we send it in chunks.
133133

134-
We can use the [transfer-encoding-chunked](https://www.npmjs.com/package/htmx-ext-transfer-encoding-chunked) htmx extension to enable chunked transfer of the response. To do this, add the requisite headers by passing `ct_hdr=True` to the FastHTML constructor, and then use the `hx_ext="chunked-transfer"` attribute in the form.
134+
We can use the [transfer-encoding-chunked](https://www.npmjs.com/package/htmx-ext-transfer-encoding-chunked) htmx extension to enable chunked transfer of the response. To do this, add the requisite headers by passing `exts='chunked-transfer'` to the FastHTML constructor, and then use the `hx_ext="chunked-transfer"` attribute in the form.
135135

136136
```python
137137
Form(hx_post=send, hx_target="#chatlist", hx_swap="beforeend", hx_ext="chunked-transfer", hx_disabled_elt="#msg-group")

02_chatbot/ws.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Set up the app, including daisyui and tailwind for the chat component
66
tlink = Script(src="https://cdn.tailwindcss.com"),
77
dlink = Link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css")
8-
app = FastHTML(hdrs=(tlink, dlink, picolink), ws_hdr=True)
8+
app = FastHTML(hdrs=(tlink, dlink, picolink), exts='ws')
99

1010
# Set up a chat model client and list of messages (https://claudette.answer.ai/)
1111
cli = Client(models[-1])
@@ -20,11 +20,11 @@ def ChatMessage(msg):
2020
Div(msg['content'], cls=f"chat-bubble {bubble_class}"),
2121
cls=f"chat {chat_class}")
2222

23-
# The input field for the user message. Also used to clear the
23+
# The input field for the user message. Also used to clear the
2424
# input field after sending a message via an OOB swap
2525
def ChatInput():
26-
return Input(type="text", name='msg', id='msg-input',
27-
placeholder="Type a message",
26+
return Input(type="text", name='msg', id='msg-input',
27+
placeholder="Type a message",
2828
cls="input input-bordered w-full", hx_swap_oob='true')
2929

3030
# The main screen
@@ -36,7 +36,7 @@ def get():
3636
Form(Group(ChatInput(), Button("Send", cls="btn btn-primary")),
3737
ws_send=True, hx_ext="ws", ws_connect="/wscon",
3838
cls="flex space-x-2 mt-2",
39-
),
39+
),
4040
cls="p-4 max-w-lg mx-auto",
4141
)
4242
return Title('Chatbot Demo'), page

02_chatbot/ws_streaming.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Set up the app, including daisyui and tailwind for the chat component
66
tlink = Script(src="https://cdn.tailwindcss.com"),
77
dlink = Link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css")
8-
app = FastHTML(hdrs=(tlink, dlink, picolink), ws_hdr=True)
8+
app = FastHTML(hdrs=(tlink, dlink, picolink), exts='ws')
99

1010
# Set up a chat model client and list of messages (https://claudette.answer.ai/)
1111
cli = AsyncClient(models[-1])

htmx/ws.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from fasthtml.common import *
22

3-
app, rt = fast_app(ws_hdr=True)
3+
app, rt = fast_app(exts='ws')
44

55

66
@rt("/")

htmx/ws_no.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from fasthtml.common import *
22

3-
app, rt = fast_app(ws_hdr=True)
3+
app, rt = fast_app()
44

55

66
@rt

0 commit comments

Comments
 (0)