11from collections import deque
22import json
33import os
4+ import uuid
45
56# TODO: protected imports?
67from IPython .html import widgets
910
1011import plotly
1112
13+ # Load JS widget code
14+ # No officially recommended way to do this in any other way
15+ # http://mail.scipy.org/pipermail/ipython-dev/2014-April/013835.html
16+ directory = os .path .dirname (os .path .realpath (__file__ ))
17+ js_widget_file = os .path .join (directory , 'graphWidget.js' )
18+ with open (js_widget_file ) as f :
19+ js_widget_code = f .read ()
20+
21+ display (Javascript (js_widget_code ))
22+
1223__all__ = None
1324
25+
1426class Graph (widgets .DOMWidget ):
1527 """An interactive Plotly graph widget for use in IPython
1628 Notebooks.
@@ -32,13 +44,6 @@ def __init__(self, graph_url, **kwargs):
3244 --------
3345 GraphWidget('https://plot.ly/~chris/3375')
3446 """
35- directory = os .path .dirname (os .path .realpath (__file__ ))
36- js_widget_file = os .path .join (directory , 'graphWidget.js' )
37- with open (js_widget_file ) as f :
38- js_widget_code = f .read ()
39-
40- display (Javascript (js_widget_code ))
41-
4247 super (Graph , self ).__init__ (** kwargs )
4348
4449 # TODO: Validate graph_url
@@ -95,6 +100,7 @@ def _handle_outgoing_message(self, message):
95100 self ._clientMessages .append (message )
96101 else :
97102 message ['graphId' ] = self ._graphId
103+ message ['uid' ] = str (uuid .uuid4 ())
98104 self ._message = json .dumps (message )
99105
100106 def on_click (self , callback , remove = False ):
0 commit comments