From cad328b2e57e87b1aed00e8645b682b10068573b Mon Sep 17 00:00:00 2001 From: Andrew Huang Date: Wed, 28 Feb 2024 11:01:48 -0800 Subject: [PATCH 1/2] Merge stylesheets with user input layouts --- panel/chat/message.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/panel/chat/message.py b/panel/chat/message.py index 923d88476e..4ffee50fed 100644 --- a/panel/chat/message.py +++ b/panel/chat/message.py @@ -461,20 +461,18 @@ def _select_renderer( contents = contents.decode("utf-8") return contents, renderer + def _update_stylesheets(self, obj): + obj.stylesheets =[ + stylesheets for stylesheets in self._stylesheets + if stylesheets not in obj.stylesheets + ] + self._stylesheets + return obj + 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 - + obj = self._update_stylesheets(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 @@ -498,6 +496,7 @@ def _create_panel(self, value, old=None): """ if isinstance(value, Viewable): self._internal = False + value = self._update_stylesheets(value) return value renderer = None From 829c01815f5a0a2fbf789f9e9536170627b8e5ab Mon Sep 17 00:00:00 2001 From: Andrew Huang Date: Wed, 28 Feb 2024 11:26:56 -0800 Subject: [PATCH 2/2] Add tests and fixes --- panel/chat/message.py | 18 ++++++++++-------- panel/tests/chat/test_message.py | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/panel/chat/message.py b/panel/chat/message.py index 4ffee50fed..fe00a0f2ed 100644 --- a/panel/chat/message.py +++ b/panel/chat/message.py @@ -461,18 +461,20 @@ def _select_renderer( contents = contents.decode("utf-8") return contents, renderer - def _update_stylesheets(self, obj): - obj.stylesheets =[ - stylesheets for stylesheets in self._stylesheets - if stylesheets not in obj.stylesheets - ] + self._stylesheets - return obj + 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. """ - obj = self._update_stylesheets(obj) + 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 @@ -496,7 +498,7 @@ def _create_panel(self, value, old=None): """ if isinstance(value, Viewable): self._internal = False - value = self._update_stylesheets(value) + self._include_stylesheets_inplace(value) return value renderer = None diff --git a/panel/tests/chat/test_message.py b/panel/tests/chat/test_message.py index 96759e69b8..b9fa9d9e40 100644 --- a/panel/tests/chat/test_message.py +++ b/panel/tests/chat/test_message.py @@ -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