Skip to content

Commit

Permalink
Forward not ChatFeed params to message_params (#6089)
Browse files Browse the repository at this point in the history
* Forward message params to ChatMessage

* Update docs
  • Loading branch information
ahuang11 authored Jan 10, 2024
1 parent bcd2a92 commit 445ff15
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
24 changes: 21 additions & 3 deletions examples/reference/chat/ChatFeed.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
},
"outputs": [],
"source": [
"import time\n",
"import panel as pn\n",
"\n",
"pn.extension()"
Expand Down Expand Up @@ -44,7 +43,7 @@
"##### Styling\n",
"\n",
"* **`card_params`** (Dict): Parameters to pass to Card, such as `header`, `header_background`, `header_color`, etc.\n",
"* **`message_params`** (Dict): Parameters to pass to each ChatMessage, such as `reaction_icons`, `timestamp_format`, `show_avatar`, `show_user`, and `show_timestamp`.\n",
"* **`message_params`** (Dict): Parameters to pass to each ChatMessage, such as `reaction_icons`, `timestamp_format`, `show_avatar`, `show_user`, and `show_timestamp` Params passed that are not ChatFeed params will be forwarded into `message_params`.\n",
"\n",
"##### Other\n",
"\n",
Expand Down Expand Up @@ -717,7 +716,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"You can pass `ChatEntry` params through `entry_params`."
"You can pass `ChatEntry` params through `message_params`."
]
},
{
Expand All @@ -735,6 +734,25 @@
"chat_feed"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alternatively, directly pass those params to the ChatFeed constructor and it'll be forwarded into `message_params` automatically."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"chat_feed = pn.chat.ChatFeed(default_avatars={\"System\": \"S\", \"User\": \"👤\"}, reaction_icons={\"like\": \"thumb-up\"})\n",
"chat_feed.send(user=\"System\", value=\"This is the System speaking.\")\n",
"chat_feed.send(user=\"User\", value=\"This is the User speaking.\")\n",
"chat_feed"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
11 changes: 10 additions & 1 deletion panel/chat/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ class ChatFeed(ListPanel):

message_params = param.Dict(default={}, doc="""
Params to pass to each ChatMessage, like `reaction_icons`, `timestamp_format`,
`show_avatar`, `show_user`, and `show_timestamp`.""")
`show_avatar`, `show_user`, and `show_timestamp`. Params passed
that are not ChatFeed params will be forwarded into `message_params`.""")

header = param.Parameter(doc="""
The header of the chat feed; commonly used for the title.
Expand Down Expand Up @@ -224,6 +225,14 @@ def __init__(self, *objects, **params):
params["renderers"] = [params["renderers"]]
if params.get("width") is None and params.get("sizing_mode") is None:
params["sizing_mode"] = "stretch_width"

# forward message params to ChatMessage for convenience
message_params = params.get("message_params", {})
for param_key in list(params.keys()):
if param_key not in ChatFeed.param and param_key in ChatMessage.param:
message_params[param_key] = params.pop(param_key)
params["message_params"] = message_params

super().__init__(*objects, **params)

# instantiate the card's column) is not None)
Expand Down
9 changes: 9 additions & 0 deletions panel/tests/chat/test_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,15 @@ def callback(contents, user, instance):
wait_until(lambda: len(chat_feed.objects) == 1)
assert chat_feed.objects[0].object == "Mutated"

def test_forward_message_params(self, chat_feed):
chat_feed = ChatFeed(reaction_icons={"like": "thumb-up"}, reactions=["like"])
chat_feed.send("Hey!")
chat_message = chat_feed.objects[0]
assert chat_feed.message_params == {"reaction_icons": {"like": "thumb-up"}, "reactions": ["like"]}
assert chat_message.object == "Hey!"
assert chat_message.reactions == ["like"]
assert chat_message.reaction_icons.options == {"like": "thumb-up"}


@pytest.mark.xdist_group("chat")
class TestChatFeedCallback:
Expand Down

0 comments on commit 445ff15

Please sign in to comment.