diff --git a/panel/chat/message.py b/panel/chat/message.py index af67993355..b69043cd16 100644 --- a/panel/chat/message.py +++ b/panel/chat/message.py @@ -698,3 +698,6 @@ def serialize( prefix_with_viewable_label=prefix_with_viewable_label, prefix_with_container_label=prefix_with_container_label, ) + + def __repr__(self, depth: int = 0) -> str: + return f"ChatMessage(object={self.object!r}, user={self.user!r}, reactions={self.reactions!r})" diff --git a/panel/chat/utils.py b/panel/chat/utils.py index 773d395f72..32fce1bffa 100644 --- a/panel/chat/utils.py +++ b/panel/chat/utils.py @@ -127,7 +127,7 @@ def get_obj_label(obj): Get the label for the object; defaults to specified object name; if unspecified, defaults to the type name. """ - label = obj.name + label = obj.name if hasattr(obj, "name") else "" type_name = type(obj).__name__ # If the name is just type + ID, simply use type # e.g. Column10241 -> Column diff --git a/panel/tests/chat/test_feed.py b/panel/tests/chat/test_feed.py index 64aa7304d4..b29dc5b0c5 100644 --- a/panel/tests/chat/test_feed.py +++ b/panel/tests/chat/test_feed.py @@ -608,6 +608,15 @@ def test_update_chat_log_params(self, chat_feed): assert chat_feed._chat_log.scroll_button_threshold == 10 assert chat_feed._chat_log.auto_scroll_limit == 10 + def test_repr(self, chat_feed): + chat_feed.send("A") + chat_feed.send("B") + assert repr(chat_feed) == ( + "ChatFeed(_placeholder=ChatMessage, sizing_mode='stretch_width')\n" + " [0] ChatMessage(object='A', user='User', reactions=[])\n" + " [1] ChatMessage(object='B', user='User', reactions=[])" + ) + @pytest.mark.xdist_group("chat") class TestChatFeedPromptUser: diff --git a/panel/tests/chat/test_message.py b/panel/tests/chat/test_message.py index 29870587bc..4d56c81e3f 100644 --- a/panel/tests/chat/test_message.py +++ b/panel/tests/chat/test_message.py @@ -390,3 +390,11 @@ def test_serialize_audio(self): def test_serialize_dataframe(self): message = ChatMessage(DataFrame(pd.DataFrame({'a': [1, 2, 3]}))) assert message.serialize() == "DataFrame= a\n0 1\n1 2\n2 3" + + def test_repr(self): + message = ChatMessage(object="Hello", user="User", avatar="A", reactions=["favorite"]) + assert repr(message) == "ChatMessage(object='Hello', user='User', reactions=['favorite'])" + + def test_repr_dataframe(self): + message = ChatMessage(pd.DataFrame({'a': [1, 2, 3]}), avatar="D") + assert repr(message) == "ChatMessage(object= a\n0 1\n1 2\n2 3, user='User', reactions=[])"