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

Include stylesheets downstream, including layouts in ChatMessage #6405

Merged
merged 3 commits into from
Mar 2, 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
21 changes: 11 additions & 10 deletions panel/chat/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,20 +461,20 @@ def _select_renderer(
contents = contents.decode("utf-8")
return contents, renderer

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

def _set_params(self, obj, **params):
"""
Set the sizing mode and height of the object.
"""
if hasattr(obj, "objects"):
params['css_classes'] = (
[css for css in obj.stylesheets if css not in self._stylesheets] +
self._stylesheets
)
for subobj in obj.objects:
self._set_params(subobj)
obj.param.update(params)
return

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
Expand All @@ -498,6 +498,7 @@ def _create_panel(self, value, old=None):
"""
if isinstance(value, Viewable):
self._internal = False
self._include_stylesheets_inplace(value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm always a little wary for applying stylesheets inplace on objects the user created but I guess it's okay here.

return value

renderer = None
Expand Down
19 changes: 19 additions & 0 deletions panel/tests/chat/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,25 @@ def test_does_not_turn_widget_into_str(self):
message = ChatMessage(object=button)
assert message.object == button

def test_include_stylesheets_inplace_on_layouts(self):
message = ChatMessage(
Row(Markdown("Hello", css_classes=["message"]), stylesheets=["row.css"]),
stylesheets=["chat.css"]
)
assert message.stylesheets == ["chat.css"]
assert message.object.stylesheets == message._stylesheets + ["chat.css", "row.css"]

# # nested
message = ChatMessage(
Row(
Row(Markdown("Hello", css_classes=["message"]), stylesheets=["row2.css"]),
stylesheets=["row.css"]
),
stylesheets=["chat.css"]
)
assert message.object.stylesheets == ChatMessage._stylesheets + ["chat.css", "row.css"]
assert message.object.objects[0].stylesheets == ChatMessage._stylesheets + ["chat.css", "row2.css"]

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