From c55f2a992684af1021ffe5df22f9617d964bf451 Mon Sep 17 00:00:00 2001 From: Stathis Fotiadis Date: Tue, 14 Mar 2017 16:33:35 +0100 Subject: [PATCH] Patch to display plotly graph widgets on notebook > v4.2 Found here https://github.com/plotly/plotly.py/pull/641/commits/79a64266bf5a704e55fb724889d234243e9306a5 --- plotly/package_data/graphWidget.js | 25 +++++++++++++++++++++---- plotly/widgets/graph_widget.py | 7 +++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/plotly/package_data/graphWidget.js b/plotly/package_data/graphWidget.js index 4395d6e2b1a..4d6c5b4bd87 100644 --- a/plotly/package_data/graphWidget.js +++ b/plotly/package_data/graphWidget.js @@ -5,15 +5,28 @@ window.genUID = function() { }); }; -var IPYTHON_VERSION = '3'; +// var IPYTHON_VERSION = '3'; + +// The following 8 lines of code are taken from https://github.com/quantopian/qgrid/. +if (IPython.version[0] === '4' && parseInt(IPython.version[2]) >= 2) { + var path = 'jupyter-js-widgets'; +} else { + var path = 'widgets/js/widget'; + if (IPython.version[0] !== '3') { + path = 'nbextensions/widgets/' + path; + } +} + +define('GraphViewModule', [path], function (widget) { + var IPYTHON_VERSION = '3'; -require(["widgets/js/widget", "widgets/js/manager"], function (widget, manager) { +// require(["widgets/js/widget", "widgets/js/manager"], function (widget, manager) { if (!('DOMWidgetView' in widget)) { // we're in IPython2, things moved a bit from 2 --> 3. // construct the expected IPython3 widget API IPYTHON_VERSION = '2'; - manager = {WidgetManager: widget}; + // manager = {WidgetManager: widget}; widget = {DOMWidgetView: IPython.DOMWidgetView}; } @@ -159,7 +172,11 @@ require(["widgets/js/widget", "widgets/js/manager"], function (widget, manager) }); // Register the GraphView with the widget manager. - manager.WidgetManager.register_widget_view('GraphView', GraphView); + // manager.WidgetManager.register_widget_view('GraphView', GraphView); + + return { + GraphView: GraphView + } }); diff --git a/plotly/widgets/graph_widget.py b/plotly/widgets/graph_widget.py index f7eb0a86084..d31b1596038 100644 --- a/plotly/widgets/graph_widget.py +++ b/plotly/widgets/graph_widget.py @@ -9,8 +9,10 @@ from requests.compat import json as _json # TODO: protected imports? -from IPython.html import widgets -from IPython.utils.traitlets import Unicode +# from IPython.html import widgets +# from IPython.utils.traitlets import Unicode +import ipywidgets as widgets +from traitlets import Unicode from IPython.display import Javascript, display import plotly.plotly.plotly as py @@ -33,6 +35,7 @@ class GraphWidget(widgets.DOMWidget): Notebooks. """ _view_name = Unicode('GraphView', sync=True) + _view_module = Unicode('GraphViewModule', sync=True) _message = Unicode(sync=True) _graph_url = Unicode(sync=True) _new_url = Unicode(sync=True)