11from collections import deque
22import json
33import os
4- import random
5- import string
4+ import uuid
65
76# TODO: protected imports?
87from IPython .html import widgets
1110
1211import plotly
1312
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+
1423__all__ = None
1524
25+
1626class Graph (widgets .DOMWidget ):
1727 """An interactive Plotly graph widget for use in IPython
1828 Notebooks.
1929 """
2030 _view_name = Unicode ('GraphView' , sync = True )
2131 _message = Unicode (sync = True )
2232 _graph_url = Unicode (sync = True )
33+ _plotly_domain = Unicode (
34+ sync = True , default_value = plotly .plotly .get_config ()['plotly_domain' ]
35+ )
2336
2437 def __init__ (self , graph_url , ** kwargs ):
2538 """Initialize a plotly graph object.
@@ -31,13 +44,6 @@ def __init__(self, graph_url, **kwargs):
3144 --------
3245 GraphWidget('https://plot.ly/~chris/3375')
3346 """
34- directory = os .path .dirname (os .path .realpath (__file__ ))
35- js_widget_file = os .path .join (directory , 'graphWidget.js' )
36- with open (js_widget_file ) as f :
37- js_widget_code = f .read ()
38-
39- display (Javascript (js_widget_code ))
40-
4147 super (Graph , self ).__init__ (** kwargs )
4248
4349 # TODO: Validate graph_url
@@ -89,13 +95,12 @@ def _handle_registration(self, event_type, callback, remove):
8995 self ._handle_outgoing_message (message )
9096
9197 def _handle_outgoing_message (self , message ):
92- message ['plotlyDomain' ] = plotly .plotly .get_config ()['plotly_domain' ]
93- message ['taskID' ] = '' .join ([random .choice (string .ascii_letters )
94- for _ in range (20 )])
98+ message ['plotlyDomain' ] = self ._plotly_domain
9599 if self ._graphId == '' :
96100 self ._clientMessages .append (message )
97101 else :
98102 message ['graphId' ] = self ._graphId
103+ message ['uid' ] = str (uuid .uuid4 ())
99104 self ._message = json .dumps (message )
100105
101106 def on_click (self , callback , remove = False ):
0 commit comments