<s> """<STR_LIT>""" <EOL> import json <EOL> import uuid <EOL> from collections import deque <EOL> from pkg_resources import resource_string <EOL> from IPython . html import widgets <EOL> from IPython . utils . traitlets import Unicode <EOL> from IPython . display import Javascript , display <EOL> import plotly . plotly . plotly as py <EOL> from plotly import utils , tools <EOL> from plotly . graph_objs import Figure <EOL> js_widget_code = resource_string ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) . decode ( '<STR_LIT:utf-8>' ) <EOL> display ( Javascript ( js_widget_code ) ) <EOL> __all__ = None <EOL> class GraphWidget ( widgets . DOMWidget ) : <EOL> """<STR_LIT>""" <EOL> _view_name = Unicode ( '<STR_LIT>' , sync = True ) <EOL> _message = Unicode ( sync = True ) <EOL> _graph_url = Unicode ( sync = True ) <EOL> _new_url = Unicode ( sync = True ) <EOL> _filename = '<STR_LIT>' <EOL> _flags = { <EOL> '<STR_LIT>' : False <EOL> } <EOL> def __init__ ( self , graph_url = '<STR_LIT>' , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( GraphWidget , self ) . __init__ ( ** kwargs ) <EOL> self . _graph_url = graph_url <EOL> self . _listener_set = set ( ) <EOL> self . _event_handlers = { <EOL> '<STR_LIT>' : widgets . CallbackDispatcher ( ) , <EOL> '<STR_LIT>' : widgets . CallbackDispatcher ( ) , <EOL> '<STR_LIT>' : widgets . CallbackDispatcher ( ) <EOL> } <EOL> self . _graphId = '<STR_LIT>' <EOL> self . on_msg ( self . _handle_msg ) <EOL> self . _clientMessages = deque ( ) <EOL> @ property <EOL> def url ( self ) : <EOL> return self . _new_url or '<STR_LIT>' <EOL> def _handle_msg ( self , message ) : <EOL> """<STR_LIT>""" <EOL> content = message [ '<STR_LIT:content>' ] [ '<STR_LIT:data>' ] [ '<STR_LIT:content>' ] <EOL> if content . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' : <EOL> self . _graphId = content [ '<STR_LIT>' ] <EOL> while self . _clientMessages : <EOL> _message = self . _clientMessages . popleft ( ) <EOL> _message [ '<STR_LIT>' ] = self . _graphId <EOL> _message = json . dumps ( _message ) <EOL> self . _message = _message <EOL> if content . get ( '<STR_LIT>' , '<STR_LIT>' ) in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if content [ '<STR_LIT>' ] == '<STR_LIT>' or content [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> message = content [ '<STR_LIT:message>' ] [ '<STR_LIT>' ] <EOL> elif content [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> message = content [ '<STR_LIT:message>' ] [ '<STR_LIT>' ] <EOL> self . _event_handlers [ content [ '<STR_LIT>' ] ] ( self , message ) <EOL> if content . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' : <EOL> self . _attributes = content . get ( '<STR_LIT>' , { } ) <EOL> if self . _flags [ '<STR_LIT>' ] : <EOL> self . _flags [ '<STR_LIT>' ] = False <EOL> url = py . plot ( self . _attributes , auto_open = False , <EOL> filename = self . _filename , validate = False ) <EOL> self . _new_url = url <EOL> self . _fade_to ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> def _handle_registration ( self , event_type , callback , remove ) : <EOL> self . _event_handlers [ event_type ] . register_callback ( callback , <EOL> remove = remove ) <EOL> event_callbacks = self . _event_handlers [ event_type ] . callbacks <EOL> if ( len ( event_callbacks ) and event_type not in self . _listener_set ) : <EOL> self . _listener_set . add ( event_type ) <EOL> message = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : list ( self . _listener_set ) } <EOL> self . _handle_outgoing_message ( message ) <EOL> def _handle_outgoing_message ( self , message ) : <EOL> if self . _graphId == '<STR_LIT>' : <EOL> self . _clientMessages . append ( message ) <EOL> else : <EOL> message [ '<STR_LIT>' ] = self . _graphId <EOL> message [ '<STR_LIT>' ] = str ( uuid . uuid4 ( ) ) <EOL> self . _message = json . dumps ( message , cls = utils . PlotlyJSONEncoder ) <EOL> def on_click ( self , callback , remove = False ) : <EOL> """<STR_LIT>""" <EOL> self . _handle_registration ( '<STR_LIT>' , callback , remove ) <EOL> def on_hover ( self , callback , remove = False ) : <EOL> """<STR_LIT>""" <EOL> self . _handle_registration ( '<STR_LIT>' , callback , remove ) <EOL> def on_zoom ( self , callback , remove = False ) : <EOL> """<STR_LIT>""" <EOL> self . _handle_registration ( '<STR_LIT>' , callback , remove ) <EOL> def plot ( self , figure_or_data , validate = True ) : <EOL> """<STR_LIT>""" <EOL> if figure_or_data == { } or figure_or_data == Figure ( ) : <EOL> validate = False <EOL> figure = tools . return_figure_from_figure_or_data ( figure_or_data , <EOL> validate ) <EOL> message = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:data>' : figure . get ( '<STR_LIT:data>' , [ ] ) , <EOL> '<STR_LIT>' : figure . get ( '<STR_LIT>' , { } ) , <EOL> '<STR_LIT>' : self . _graphId <EOL> } <EOL> self . _handle_outgoing_message ( message ) <EOL> def restyle ( self , update , indices = None ) : <EOL> """<STR_LIT>""" <EOL> message = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : update , <EOL> '<STR_LIT>' : self . _graphId <EOL> } <EOL> if indices : <EOL> message [ '<STR_LIT>' ] = indices <EOL> self . _handle_outgoing_message ( message ) <EOL> def relayout ( self , layout ) : <EOL> """<STR_LIT>""" <EOL> message = { <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : layout , '<STR_LIT>' : self . _graphId <EOL> } <EOL> self . _handle_outgoing_message ( message ) <EOL> def hover ( self , * hover_objs ) : <EOL> """<STR_LIT>""" <EOL> if len ( hover_objs ) == <NUM_LIT:1> : <EOL> hover_objs = hover_objs [ <NUM_LIT:0> ] <EOL> message = { <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : hover_objs , '<STR_LIT>' : self . _graphId <EOL> } <EOL> self . _handle_outgoing_message ( message ) <EOL> def add_traces ( self , traces , new_indices = None ) : <EOL> """<STR_LIT>""" <EOL> message = { <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : traces , '<STR_LIT>' : self . _graphId <EOL> } <EOL> if new_indices is not None : <EOL> message [ '<STR_LIT>' ] = new_indices <EOL> self . _handle_outgoing_message ( message ) <EOL> def delete_traces ( self , indices ) : <EOL> """<STR_LIT>""" <EOL> message = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : indices , <EOL> '<STR_LIT>' : self . _graphId <EOL> } <EOL> self . _handle_outgoing_message ( message ) <EOL> def reorder_traces ( self , current_indices , new_indices = None ) : <EOL> """<STR_LIT>""" <EOL> message = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : current_indices , <EOL> '<STR_LIT>' : self . _graphId <EOL> } <EOL> if new_indices is not None : <EOL> message [ '<STR_LIT>' ] = new_indices <EOL> self . _handle_outgoing_message ( message ) <EOL> def save ( self , ignore_defaults = False , filename = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . _flags [ '<STR_LIT>' ] = True <EOL> self . _filename = filename <EOL> message = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : ignore_defaults } <EOL> self . _handle_outgoing_message ( message ) <EOL> self . _fade_to ( '<STR_LIT>' , <NUM_LIT:0.1> ) <EOL> def extend_traces ( self , update , indices = ( <NUM_LIT:0> , ) , max_points = None ) : <EOL> """<STR_LIT>""" <EOL> message = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : update , <EOL> '<STR_LIT>' : self . _graphId , <EOL> '<STR_LIT>' : indices <EOL> } <EOL> if max_points is not None : <EOL> message [ '<STR_LIT>' ] = max_points <EOL> self . _handle_outgoing_message ( message ) <EOL> def _fade_to ( self , duration , opacity ) : <EOL> """<STR_LIT>""" <EOL> message = { '<STR_LIT>' : True , '<STR_LIT>' : duration , '<STR_LIT>' : opacity } <EOL> self . _handle_outgoing_message ( message ) </s>
