Repository: plotly/plotly.py

Issue:
qgrid - plotly conflict
I'm using plotly offline in a jupyter notebook. I'm coming across an error that i can't seem to debug.

Here is my code:

import qgrid
import Quandl
import plotly
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot

aapl = Quandl.get('yahoo/aapl')
qgrid.nbinstall(overwrite=True)
# works ok

qgrid.show_grid(aapl)

init_notebook_mode()
# no longer works ok

qgrid.show_grid(aapl)

The error message is
"
Javascript error adding output!
TypeError: Cannot read property 's' of undefined
See your browser Javascript console for more details.
"



Discussion:
I am getting javascript type error : require not a function, for custom widget, on import and initialization of plotly in offline mode

I do not understand JS but I think following lines of code in offline.py causing errors:

```
display(HTML('<script type="text/javascript">' +
             # ipython's includes `require` as a global, which
             # conflicts with plotly.js. so, unrequire it.
             'require=requirejs=define=undefined;' +
             '</script>' +
             '<script type="text/javascript">' +
             get_plotlyjs() +
             '</script>'))
```

I saw this too, same error. Ideas?

@djchou what are you trying to do with both qgrid and plotly? 

One displays tables well, the other displays graphs well.  It makes sense to have both in the same notebook

See https://github.com/plotly/plotly.js/issues/90 for more info.

Agreed @djchou. I was just wondering if you were building a UI/library that used both or just your own personal notebook :)

We are building a visualization widget for Ipython at https://github.com/jupyter-incubator/sparkmagic/tree/master/remotespark/datawidgets in case you want to take a look!

The intention is to decouple it from the sparkmagic project and put the widget into its own repo :+1: 

This issue would be perfect for a community contributor.

In this [block](https://github.com/plotly/plotly.py/blob/master/plotly/offline/offline.py#L64-L71), we need to remove the `require=requirejs=define=undefined;` hack and import plotly.js using the Require.js `require` as done in this plotly-notebook-js [block](https://github.com/plotly/plotly-notebook-js/blob/bc209899e9ca97f0bfe59d288007810889ec1171/src/index.js#L121-L123).


Resolving patch:
diff --git a/plotly/offline/offline.py b/plotly/offline/offline.py
--- a/plotly/offline/offline.py
+++ b/plotly/offline/offline.py
@@ -61,14 +61,14 @@ def init_notebook_mode():
 
     global __PLOTLY_OFFLINE_INITIALIZED
     if not __PLOTLY_OFFLINE_INITIALIZED:
-        display(HTML('<script type="text/javascript">' +
-                     # ipython's includes `require` as a global, which
-                     # conflicts with plotly.js. so, unrequire it.
-                     'require=requirejs=define=undefined;' +
-                     '</script>' +
-                     '<script type="text/javascript">' +
+        display(HTML("<script type='text/javascript'>" +
+                     "define('plotly', function(require, exports, module) {" +
                      get_plotlyjs() +
-                     '</script>'))
+                     "});" +
+                     "require(['plotly'], function(Plotly) {" +
+                     "window.Plotly = Plotly;" +
+                     "});" +
+                     "</script>"))
     __PLOTLY_OFFLINE_INITIALIZED = True
 
 


Test patch: