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

Add message into css_classes to ChatMessage's markup #6407

Merged
merged 5 commits into from
Mar 11, 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
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
Loading