Skip to content

Commit

Permalink
tweak repr (#7521)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuang11 authored Dec 2, 2024
1 parent d1d8280 commit b296960
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions panel/chat/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
2 changes: 1 addition & 1 deletion panel/chat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions panel/tests/chat/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[])"

0 comments on commit b296960

Please sign in to comment.