Skip to content

Commit

Permalink
Fix plotly showing in Viewer (#4904)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtmok authored Oct 9, 2024
1 parent 7502a2c commit 5441794
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""\
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:

Check failure on line 213 in extensions/positron-python/python_files/positron/positron_ipykernel/ui.py

View workflow job for this annotation

GitHub Actions / Check Python types

Expression of type "None" cannot be assigned to parameter of type "str"   Type "None" cannot be assigned to type "str" (reportGeneralTypeIssues)
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:
Expand Down

0 comments on commit 5441794

Please sign in to comment.