Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward not ChatFeed params to message_params #6089

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -716,7 +715,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"You can pass `ChatEntry` params through `entry_params`."
"You can pass `ChatEntry` params through `message_params`."
]
},
{
Expand All @@ -734,6 +733,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 @@ -221,6 +222,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 @@ -379,6 +379,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