From 445ff154112376a4d31bd212ac476a9f0f57be37 Mon Sep 17 00:00:00 2001 From: Andrew <15331990+ahuang11@users.noreply.github.com> Date: Wed, 10 Jan 2024 07:12:53 -0800 Subject: [PATCH] Forward not ChatFeed params to message_params (#6089) * Forward message params to ChatMessage * Update docs --- examples/reference/chat/ChatFeed.ipynb | 24 +++++++++++++++++++++--- panel/chat/feed.py | 11 ++++++++++- panel/tests/chat/test_feed.py | 9 +++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/examples/reference/chat/ChatFeed.ipynb b/examples/reference/chat/ChatFeed.ipynb index d5acd159bf..8350500803 100644 --- a/examples/reference/chat/ChatFeed.ipynb +++ b/examples/reference/chat/ChatFeed.ipynb @@ -8,7 +8,6 @@ }, "outputs": [], "source": [ - "import time\n", "import panel as pn\n", "\n", "pn.extension()" @@ -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", @@ -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`." ] }, { @@ -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": {}, diff --git a/panel/chat/feed.py b/panel/chat/feed.py index 37a38aa35e..d8d29da7f4 100644 --- a/panel/chat/feed.py +++ b/panel/chat/feed.py @@ -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. @@ -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) diff --git a/panel/tests/chat/test_feed.py b/panel/tests/chat/test_feed.py index 69a41943bc..7a1a63f960 100644 --- a/panel/tests/chat/test_feed.py +++ b/panel/tests/chat/test_feed.py @@ -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: