Skip to content

Commit

Permalink
Add message css classes to markup
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuang11 committed Feb 28, 2024
1 parent 829c018 commit e2ed2a4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
19 changes: 16 additions & 3 deletions panel/chat/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,16 +470,28 @@ def _include_stylesheets_inplace(self, obj):
for o in obj.objects:
self._include_stylesheets_inplace(o)

def _include_message_css_class_inplace(self, obj):
if hasattr(obj, "objects"):
for o in obj.objects:
self._include_message_css_class_inplace(o)
elif isinstance(obj, str):
return self._include_message_css_class_inplace(Markdown(obj))

is_markup = isinstance(obj, HTMLBasePane) and not isinstance(obj, FileBase)
if obj.css_classes or not is_markup:
return
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

def _set_params(self, obj, **params):
"""
Set the sizing mode and height of the object.
"""
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 @@ -499,6 +511,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 @@ -205,6 +205,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 e2ed2a4

Please sign in to comment.