88from IPython .display import Javascript , display
99
1010from plotly import utils
11+ import plotly .plotly .plotly as py
1112from pkg_resources import resource_string
1213
1314# Load JS widget code
@@ -28,6 +29,11 @@ class GraphWidget(widgets.DOMWidget):
2829 _view_name = Unicode ('GraphView' , sync = True )
2930 _message = Unicode (sync = True )
3031 _graph_url = Unicode (sync = True )
32+ _new_url = Unicode (sync = True )
33+ _filename = ''
34+ _flags = {
35+ 'save_pending' : False
36+ }
3137
3238 # TODO: URL for offline enterprise
3339 def __init__ (self , graph_url = 'https://plot.ly/~playground/7' , ** kwargs ):
@@ -88,6 +94,13 @@ def _handle_msg(self, message):
8894 message = content ['message' ]['ranges' ]
8995
9096 self ._event_handlers [content ['event' ]](self , message )
97+ if content .get ('event' , '' ) == 'getAttributes' :
98+ self ._attributes = content .get ('response' , {})
99+ if self ._flags ['save_pending' ]:
100+ self ._flags ['save_pending' ] = False
101+ url = py .plot (self ._attributes , auto_open = False ,
102+ filename = self ._filename , validate = False )
103+ self ._new_url = url
91104
92105 def _handle_registration (self , event_type , callback , remove ):
93106 self ._event_handlers [event_type ].register_callback (callback ,
@@ -605,3 +618,15 @@ def reorder_traces(self, current_indices, new_indices=None):
605618 message ['newIndices' ] = new_indices
606619 self ._handle_outgoing_message (message )
607620
621+ def save (self , ignore_defaults = False , filename = '' ):
622+ """
623+ Save a copy of the current state of the widget in plotly.
624+
625+ :param (bool) ignore_defaults: Auto-fill in unspecified figure keys?
626+ :param (str) filename: Name of the file on plotly.
627+
628+ """
629+ self ._flags ['save_pending' ] = True
630+ self ._filename = filename
631+ message = {'task' : 'getAttributes' , 'ignoreDefaults' : ignore_defaults }
632+ self ._handle_outgoing_message (message )
0 commit comments