Skip to content

Commit

Permalink
Add message into css_classes to ChatMessage's markup (#6407)
Browse files Browse the repository at this point in the history
* Add message css classes to markup

* Update panel/chat/message.py

* Keep transformed objects

---------

Co-authored-by: Philipp Rudiger <prudiger@anaconda.com>
  • Loading branch information
ahuang11 and philippjfr committed Mar 11, 2024
1 parent 21d19a9 commit 8a7e6b0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
29 changes: 24 additions & 5 deletions panel/chat/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,33 @@ def _select_renderer(
return contents, renderer

def _include_stylesheets_inplace(self, obj):
if hasattr(obj, "objects"):
obj.objects[:] = [
self._include_stylesheets_inplace(o) for o in obj.objects
]
else:
obj = _panel(obj)
obj.stylesheets = [
stylesheet for stylesheet in self._stylesheets + self.stylesheets
if stylesheet not in obj.stylesheets
] + obj.stylesheets
return obj

def _include_message_css_class_inplace(self, obj):
if hasattr(obj, "objects"):
for o in obj.objects:
self._include_stylesheets_inplace(o)
obj.objects[:] = [
self._include_message_css_class_inplace(o)
for o in obj.objects
]
else:
obj = _panel(obj)
is_markup = isinstance(obj, HTMLBasePane) and not isinstance(obj, FileBase)
if obj.css_classes or not is_markup:
return obj
if len(str(obj.object)) > 0: # only show a background if there is content
obj.css_classes = [*(css for css in obj.css_classes if css != "message"), "message"]
obj.sizing_mode = None
return obj

def _set_params(self, obj, **params):
"""
Expand All @@ -486,9 +506,7 @@ def _set_params(self, obj, **params):
self._include_stylesheets_inplace(obj)
is_markup = isinstance(obj, HTMLBasePane) and not isinstance(obj, FileBase)
if is_markup:
if len(str(obj.object)) > 0: # only show a background if there is content
params['css_classes'] = [*(css for css in obj.css_classes if css != "message"), "message"]
params['sizing_mode'] = None
self._include_message_css_class_inplace(obj)
else:
if obj.sizing_mode is None and not obj.width:
params['sizing_mode'] = "stretch_width"
Expand All @@ -508,6 +526,7 @@ def _create_panel(self, value, old=None):
if isinstance(value, Viewable):
self._internal = False
self._include_stylesheets_inplace(value)
self._include_message_css_class_inplace(value)
return value

renderer = None
Expand Down
21 changes: 21 additions & 0 deletions panel/tests/chat/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,27 @@ def test_include_stylesheets_inplace_on_layouts(self):
assert message.object.stylesheets == ChatMessage._stylesheets + ["chat.css", "row.css"]
assert message.object.objects[0].stylesheets == ChatMessage._stylesheets + ["chat.css", "row2.css"]

def test_include_message_css_class_inplace(self):
# markdown
message = ChatMessage(object=Markdown("hello"))
assert message.object.css_classes == ["message"]

# custom css class; no message appended
message = ChatMessage(object=Markdown("hello", css_classes=["custom"]))
assert message.object.css_classes == ["custom"]

# nested in layout; message appended
message = ChatMessage(object=Row(Markdown("hello")))
assert message.object.objects[0].css_classes == ["message"]

# nested in layout as a string; message appended
message = ChatMessage(object=Row("hello"))
assert message.object.objects[0].css_classes == ["message"]

# nested in layout with custom css; no message appended
message = ChatMessage(object=Row(Markdown("hello", css_classes=["custom"])))
assert message.object.objects[0].css_classes == ["custom"]

@mpl_available
def test_can_display_any_python_object_that_panel_can_display(self):
# For example matplotlib figures
Expand Down

0 comments on commit 8a7e6b0

Please sign in to comment.