From 5441794b204ac206850942e4e56d91688dc89139 Mon Sep 17 00:00:00 2001 From: Tim Mok Date: Wed, 9 Oct 2024 10:16:45 -0400 Subject: [PATCH] Fix plotly showing in Viewer (#4904) --- .../positron_ipykernel/tests/test_ui.py | 9 +++++++-- .../positron/positron_ipykernel/ui.py | 19 +++++++++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/extensions/positron-python/python_files/positron/positron_ipykernel/tests/test_ui.py b/extensions/positron-python/python_files/positron/positron_ipykernel/tests/test_ui.py index ff220844654..132da3704f7 100644 --- a/extensions/positron-python/python_files/positron/positron_ipykernel/tests/test_ui.py +++ b/extensions/positron-python/python_files/positron/positron_ipykernel/tests/test_ui.py @@ -249,7 +249,7 @@ def test_plotly_show_sends_events( mock_handle_request: Mock, ) -> None: """ - Test that showing a Plotly plot sends the expected UI events. + Test that showing a Plotly plot sends the expected UI events when using `fig.show()` and `fig`. """ shell.run_cell( """\ @@ -265,11 +265,16 @@ def test_plotly_show_sends_events( fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2]) fig.show() +fig """ ) mock_handle_request.assert_called() - assert len(ui_comm.messages) == 1 + assert len(ui_comm.messages) == 2 params = ui_comm.messages[0]["data"]["params"] assert params["title"] == "" assert params["is_plot"] assert params["height"] == 0 + params = ui_comm.messages[1]["data"]["params"] + assert params["title"] == "" + assert params["is_plot"] + assert params["height"] == 0 diff --git a/extensions/positron-python/python_files/positron/positron_ipykernel/ui.py b/extensions/positron-python/python_files/positron/positron_ipykernel/ui.py index abf84570996..24fd6ee232b 100644 --- a/extensions/positron-python/python_files/positron/positron_ipykernel/ui.py +++ b/extensions/positron-python/python_files/positron/positron_ipykernel/ui.py @@ -198,7 +198,7 @@ def open(self, url, new=0, autoraise=True) -> bool: for addr in _localhosts: if addr in url: - is_plot = self._is_module_function("plotly.basedatatypes", "show") + is_plot = self._is_module_function("plotly.basedatatypes") if is_plot: return self._send_show_html_event(url, is_plot) else: @@ -210,15 +210,18 @@ def open(self, url, new=0, autoraise=True) -> bool: return False @staticmethod - def _is_module_function(module_name: str, function_name: str) -> bool: + def _is_module_function(module_name: str, function_name: str = None) -> bool: module = sys.modules.get(module_name) if module: - for frame_info in inspect.stack(): - if ( - inspect.getmodule(frame_info.frame, frame_info.filename) == module - and frame_info.function == function_name - ): - return True + if function_name: + for frame_info in inspect.stack(): + if ( + inspect.getmodule(frame_info.frame, frame_info.filename) == module + and frame_info.function == function_name + ): + return True + else: + return True return False def _send_show_html_event(self, url: str, is_plot: bool) -> bool: