plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

`FigureWidget` fails with `anywidget` in v6.0.0

Open turbotimon opened this issue 10 months ago • 10 comments

Problem: Since v6.0.0 FigureWidget uses anywidget. However, it fails with a java script error.

$ pip show plotly anywidget
Name: plotly
Version: 6.0.0
Summary: An open-source, interactive data visualization library for Python
Home-page: https://plotly.com/python/
Author: Chris P
Author-email: [email protected]
License: MIT
Location: /home/erti/PROJECT_repos/ForeST_HPO/hpo-innosuisse/.venv_anywidget/lib/python3.11/site-packages
Requires: narwhals, packaging
Required-by: 
---
Name: anywidget
Version: 0.9.13
Summary: custom jupyter widgets made easy
Home-page: 
Author: 
Author-email: Trevor Manz <[email protected]>
License: MIT
Location: /home/erti/PROJECT_repos/ForeST_HPO/hpo-innosuisse/.venv_anywidget/lib/python3.11/site-packages
Requires: ipywidgets, psygnal, typing-extensions
Required-by: 
import plotly.graph_objs as go
fig = go.FigureWidget()
fig.add_scatter(y=[2, 1, 4, 3])
fig

Either, no output (no figure) is shown or this is shown:

Failed to load view class 'AnyView' from module 'anywidget'
Error: No version of module anywidget is registered
    at WidgetManager.loadClass (https://file+.vscode-resource.vscode-cdn.net/home/erti/.vscode/extensions/ms-toolsai.jupyter-renderers-1.1.0/out/node_modules/%40vscode/jupyter-ipywidgets8/dist/ipywidgets.js:2:2420023)
    at WidgetManager.loadClass (https://file+.vscode-resource.vscode-cdn.net/home/erti/.vscode/extensions/ms-toolsai.jupyter-renderers-1.1.0/out/node_modules/%40vscode/jupyter-ipywidgets8/dist/ipywidgets.js:2:2803837)
    at WidgetManager.loadViewClass (https://file+.vscode-resource.vscode-cdn.net/home/erti/.vscode/extensions/ms-toolsai.jupyter-renderers-1.1.0/out/node_modules/%40vscode/jupyter-ipywidgets8/dist/ipywidgets.js:2:2417001)
    at https://file+.vscode-resource.vscode-cdn.net/home/erti/.vscode/extensions/ms-toolsai.jupyter-renderers-1.1.0/out/node_modules/%40vscode/jupyter-ipywidgets8/dist/ipywidgets.js:2:2410155
    at async Tt.renderWidget (https://file+.vscode-resource.vscode-cdn.net/home/erti/.vscode/extensions/ms-toolsai.jupyter-2024.11.0-linux-x64/dist/webviews/webview-side/ipywidgetsKernel/ipywidgetsKernel.js:17:17328)
    at async op (https://file+.vscode-resource.vscode-cdn.net/home/erti/.vscode/extensions/ms-toolsai.jupyter-2024.11.0-linux-x64/dist/webviews/webview-side/ipywidgetsKernel/ipywidgetsKernel.js:17:31113)

Workaround: Downgrading to plotly-5.24 solves the problem

Related: Maybe somewhat related to https://github.com/plotly/plotly.py/issues/4996

turbotimon avatar Feb 11 '25 10:02 turbotimon

Could you describe the way that you installed anywidget? I was seeing this issue when I installed anywidget while JupyterLab was running without restarting it. I'm curious if restarting JupyterLab / reloading the page would help. (Still a bug, just good info for debugging)

marthacryan avatar Feb 11 '25 17:02 marthacryan

@marthacryan I created a fresh venv with python3.11 and run pip install plotly anywidget and run the code in vscode notebook. I also restarted the kernel several times, somties just no output is shown, somtimes the json error. I also tried to run it in google colab. There, I don't see the json error, but also get no output (the figure is not shown).

turbotimon avatar Feb 12 '25 10:02 turbotimon

Thank you for the info @turbotimon! Looks like this is an issue with plotly.js==3.0.0, which we updated to for the plotly.py==6.0.0 release. I'll investigate why this is happening but as a note, I think it may be related to removing require.js and needing to update the way that we include plotly.js.

marthacryan avatar Feb 13 '25 20:02 marthacryan

Update here: looks like there is a PR open that does fix this issue for plotly.js: plotly/plotly.js#7367.

marthacryan avatar Feb 14 '25 22:02 marthacryan

Plotly.js just released the 3.0.1 patch which will fix this! We'll need to update the plotly.js version in plotly.py, so I'll wait on that to close this issue.

marthacryan avatar Feb 18 '25 19:02 marthacryan

Hi, I'm having this issue too. Could any one suggest how to I apply patch to my existing installation of plotly in Python?

ershov avatar Mar 20 '25 01:03 ershov

You can't upgrade?

afeld avatar Mar 20 '25 03:03 afeld

Hi, I'm having this issue too. Could any one suggest how to I apply patch to my existing installation of plotly in Python?

Upgrade or downgrade, just don't use v6.0.0, e.g.: pip install plotly!=6.0.0

turbotimon avatar Mar 20 '25 07:03 turbotimon

@turbotimon Is this now working for you?

marthacryan avatar Mar 20 '25 18:03 marthacryan

@turbotimon Is this now working for you?

With 5.24 it is, I did not try it with 6.0.1 yet..

Edit: Just tried with 6.0.1 in colab. It works but behaves different, a single fig or fig.show() at the end of the cell won't show the plot (as it did in 5.24), but a display(fig) will. @marthacryan is this a "feature or a bug" and is this due to your #4869?

Test code:

# %%
!pip install -U plotly anywidget
# %%
import plotly.graph_objs as go
fig = go.FigureWidget()
fig.add_scatter(y=[2, 1, 4, 3])
fig # does not show anything!
# %%
display(fig) # but this
# %%
from ipywidgets import widgets
button = widgets.Button(description="foo")

def on_button_clicked(b):
    fig.data[0].y = [1, 2, 3, 4]

button.on_click(on_button_clicked)
button # test the change

Edit2: It seems that this behaviour is only in colab. But it seems to work in e.g. vscode notebooks. I did not try it in jupyter lab. Versions used plotly==6.1.2 anywidget==0.9.18

turbotimon avatar Mar 20 '25 21:03 turbotimon

Oh interesting @turbotimon I'm seeing the same behavior in colab. This must be a difference in how they handle displaying widgets. I think for now using the display function in colab seems like a good workaround.

marthacryan avatar Jun 18 '25 17:06 marthacryan