plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
16.3k stars 2.55k forks source link

FigureWidget doesn't update in Jupyter Notebook #1404

Open ubershmekel opened 5 years ago

ubershmekel commented 5 years ago

This snippet in a Python cell produces a chart, but the chart does not update to get the new title and new data.

import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

init_notebook_mode()
data = [go.Bar(
            x=['giraffes', 'orangutans', 'monkeys'],
            y=[20, 14, 23]
    )]
layout = {'yaxis': {'range': [0, 25]}}
fig = go.FigureWidget(data, layout)
iplot(fig)

# The following code is ignored.
fig.data[0].y = (5,10,1)
fig.layout.title = "New title"

Plotly version: 3.5.0 Server Information: You are using Jupyter notebook. The version of the notebook server is: 5.7.4 The server is running on this version of Python: Python 3.7.1 (default, Dec 14 2018, 13:28:58) [Clang 4.0.1 (tags/RELEASE_401/final)]

Current Kernel Information: Python 3.7.1 (default, Dec 14 2018, 13:28:58) Type 'copyright', 'credits' or 'license' for more information IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

jonmmease commented 5 years ago

Hi @ubershmekel ,

For the visual display of the FigureWidget to update you need to allow it to display itself as an ipywidget by making the fig variable the last statement in a notebook cell. In particular, you can't use iplot. For example:

import plotly.graph_objs as go

data = [go.Bar(
            x=['giraffes', 'orangutans', 'monkeys'],
            y=[20, 14, 23]
    )]
layout = {'yaxis': {'range': [0, 25]}}
fig = go.FigureWidget(data, layout)
fig

newplot

# The following code will update the plot above
fig.data[0].y = (5,10,1)
fig.layout.title = "New title"

Does that work for you?

ubershmekel commented 5 years ago

@jonmmease When I do that - nothing is displayed after the cell completes.

jonmmease commented 5 years ago

Ok, then it sounds like there is some kind if installation problem with respect to the FigureWidget.

Are you using the classic Jupyter Notebook or JupyterLab?

If classic, please include the output of

jupyter nbextension list

if JupyterLab, please include the output of

jupyter labextension list

What version of ipywidgets is installed?

Do you see any errors/messages in the browser console? (https://developers.google.com/web/tools/chrome-devtools/console/)

ubershmekel commented 5 years ago
$ jupyter nbextension list
Known nbextensions:
  config dir: /anaconda3/etc/jupyter/nbconfig
    notebook section
      plotlywidget/extension  enabled 
      - Validating: OK
      jupyter-js-widgets/extension  enabled 
      - Validating: OK
$ jupyter labextension list
JupyterLab v0.32.1
No installed extensions

There are errors in the console...

manager-base.js:273 Could not instantiate widget
(anonymous) @ manager-base.js:273
step @ manager-base.js:42
(anonymous) @ manager-base.js:23
rejected @ manager-base.js:15
Promise.then (async)
step @ manager-base.js:16
(anonymous) @ manager-base.js:17
__awaiter @ manager-base.js:13
ManagerBase._make_model @ manager-base.js:257
(anonymous) @ manager-base.js:246
step @ manager-base.js:42
(anonymous) @ manager-base.js:23
(anonymous) @ manager-base.js:17
__awaiter @ manager-base.js:13
ManagerBase.new_model @ manager-base.js:232
ManagerBase.handle_comm_open @ manager-base.js:144
executeBound @ underscore.js:762
(anonymous) @ underscore.js:775
(anonymous) @ underscore.js:122
(anonymous) @ comm.js:89
Promise.then (async)
CommManager.comm_open @ comm.js:85
i @ jquery.min.js:2
Kernel._handle_iopub_message @ kernel.js:1223
Kernel._finish_ws_message @ kernel.js:1015
(anonymous) @ kernel.js:1006
Promise.then (async)
Kernel._handle_ws_message @ kernel.js:1006
i @ jquery.min.js:2
utils.js:119 Error: Could not create a model.
    at promiseRejection (utils.js:119)
promiseRejection @ utils.js:119
Promise.catch (async)
ManagerBase.handle_comm_open @ manager-base.js:149
executeBound @ underscore.js:762
(anonymous) @ underscore.js:775
(anonymous) @ underscore.js:122
(anonymous) @ comm.js:89
Promise.then (async)
CommManager.comm_open @ comm.js:85
i @ jquery.min.js:2
Kernel._handle_iopub_message @ kernel.js:1223
Kernel._finish_ws_message @ kernel.js:1015
(anonymous) @ kernel.js:1006
Promise.then (async)
Kernel._handle_ws_message @ kernel.js:1006
i @ jquery.min.js:2
4kernel.js:1007 Couldn't process kernel message Error: Script error for "plotlywidget"
http://requirejs.org/docs/errors.html#scripterror
    at makeError (require.js?v=951f856e81496aaeec2e71a1c2c0d51f:168)
    at HTMLScriptElement.onScriptError (require.js?v=951f856e81496aaeec2e71a1c2c0d51f:1735)
(anonymous) @ kernel.js:1007
Promise.catch (async)
Kernel._handle_ws_message @ kernel.js:1007
i @ jquery.min.js:2
pytorch-mnist-cnn.ipynb#:1 Uncaught (in promise) Error: Script error for "plotlywidget"
http://requirejs.org/docs/errors.html#scripterror
    at makeError (require.js?v=951f856e81496aaeec2e71a1c2c0d51f:168)
    at HTMLScriptElement.onScriptError (require.js?v=951f856e81496aaeec2e71a1c2c0d51f:1735)
Promise.then (async)
ManagerBase.register_model @ manager-base.js:208
(anonymous) @ manager-base.js:248
step @ manager-base.js:42
(anonymous) @ manager-base.js:23
(anonymous) @ manager-base.js:17
__awaiter @ manager-base.js:13
ManagerBase.new_model @ manager-base.js:232
ManagerBase.handle_comm_open @ manager-base.js:144
executeBound @ underscore.js:762
(anonymous) @ underscore.js:775
(anonymous) @ underscore.js:122
(anonymous) @ comm.js:89
Promise.then (async)
CommManager.comm_open @ comm.js:85
i @ jquery.min.js:2
Kernel._handle_iopub_message @ kernel.js:1223
Kernel._finish_ws_message @ kernel.js:1015
(anonymous) @ kernel.js:1006
Promise.then (async)
Kernel._handle_ws_message @ kernel.js:1006
i @ jquery.min.js:2
5kernel.js:1007 Couldn't process kernel message Error: Script error for "plotlywidget"
http://requirejs.org/docs/errors.html#scripterror
    at makeError (require.js?v=951f856e81496aaeec2e71a1c2c0d51f:168)
    at HTMLScriptElement.onScriptError (require.js?v=951f856e81496aaeec2e71a1c2c0d51f:1735)
(anonymous) @ kernel.js:1007
Promise.catch (async)
Kernel._handle_ws_message @ kernel.js:1007
i @ jquery.min.js:2
pytorch-mnist-cnn.ipynb#:1 Uncaught (in promise) Error: Script error for "plotlywidget"
http://requirejs.org/docs/errors.html#scripterror
    at makeError (require.js?v=951f856e81496aaeec2e71a1c2c0d51f:168)
    at HTMLScriptElement.onScriptError (require.js?v=951f856e81496aaeec2e71a1c2c0d51f:1735)
jonmmease commented 5 years ago

Thanks for adding the additional info.

When you see these errors, are you running the classic notebook or jupyterlab?

What version of ipywidgets do you have installed?

Also, since it looks like you're using conda. Could you double check that you don't have the plotly or ipywidgets packages installed using both conda and pip? (You can check by running pip list and conda list).

ubershmekel commented 5 years ago

You are using Jupyter notebook. The version of the notebook server is: 5.7.4

I believe that means classic notebook.

Indeed there are duplicate ipywidgets and plotly between the pip and conda.

ipywidgets                         7.4.2     
ipywidgets                7.4.2                    py37_0  
plotly                    3.5.0                     <pip>
plotly                             3.5.0     
jonmmease commented 5 years ago

I believe that means classic notebook.

The server between the two is actually identical. The difference is whether you run jupyter notebook or jupyter lab from the command line to start up the server.

Indeed there are duplicate ipywidgets and plotly between the pip and conda.

Alright, let's try this

$ pip uninstall ipywidgets
$ pip uninstall plotly
$ conda uninstall ipywidgets
$ conda uninstall plotly

$ conda install -c plotly plotly=3.5.0
$ conda install "notebook>=5.3" "ipywidgets>=7.2"

If that doesn't help, try uninstall and reinstalling the notebook extension

$ jupyter nbextension uninstall --py --sys-prefix widgetsnbextension
$ jupyter nbextension uninstall --py --sys-prefix plotlywidget
$ jupyter nbextension install --py --sys-prefix widgetsnbextension
$ jupyter nbextension install --py --sys-prefix plotlywidget
$ jupyter nbextension enable --py --sys-prefix widgetsnbextension
$ jupyter nbextension enable --py --sys-prefix plotlywidget
KKAnumalasetty commented 5 years ago

Partial success!!! (working in jupyter lab but not in jupyter notebook) When I follow the below steps, it started working only in jupyter lab but not in jypyter notebook. In my jupyter notebook "Nbextensions" tab, both ploltywidget and jupyter-js-widgets extensions are disabled and says "possibly incompatible". I don't really understand why these are incompatible even though validation says it's OK. (Image attached). Jupyter Notebook error

Nbextension list in Jupyter: jupyter nbextension list

Labextension list in Jupyter lab jupyter labextension list

steps followed:

jupyter nbextension uninstall --py --sys-prefix widgetsnbextension
jupyter nbextension uninstall --py --sys-prefix plotlywidget
jupyter nbextension install --py --sys-prefix widgetsnbextension
jupyter nbextension install --py --sys-prefix plotlywidget
jupyter nbextension enable --py --sys-prefix widgetsnbextension
jupyter nbextension enable --py --sys-prefix plotlywidget

Please help me fix it in jupyter notebook as well.

Additionally, I noticed that plotlywidget and jupyter-js-widget extensions are in an entirely different folder compared to deafult nbextension folder. Is this causing the problem? If yes, could you please help me on how to fix ? What files (file path) should I copy from one location to another location?

Details: I don't have conflicting packages (for any of them listed below) in conda list and pip list.

Python: 3.7 (64-bit)
Plotly:                                                       3.7.0
ipywidgets                                                7.4.2
widgetsnbextension                                 3.4.2
jupyter-nbextensions-configurator          0.4.1
jupyterlab                                                 0.35.3
jupyterlab-server                                       0.2.0
jupyter                                                       1.0.0
fingoldo commented 10 months ago

Hi @ubershmekel ,

For the visual display of the FigureWidget to update you need to allow it to display itself as an ipywidget by making the fig variable the last statement in a notebook cell. In particular, you can't use iplot. For example:

import plotly.graph_objs as go

data = [go.Bar(
            x=['giraffes', 'orangutans', 'monkeys'],
            y=[20, 14, 23]
    )]
layout = {'yaxis': {'range': [0, 25]}}
fig = go.FigureWidget(data, layout)
fig

newplot

# The following code will update the plot above
fig.data[0].y = (5,10,1)
fig.layout.title = "New title"

Does that work for you?

for me it worked like a charm, thanks a lot!