7
7
from IPython.utils.traitlets import Unicode
8
8
from IPython.display import Javascript, display
9
9
10
+ import plotly.plotly.plotly as py
10
11
from plotly import utils, tools
11
12
from plotly.graph_objs import Figure
12
13
from pkg_resources import resource_string
@@ -35,6 +36,11 @@ class GraphWidget(widgets.DOMWidget):
35
36
_view_name = Unicode('GraphView', sync=True)
36
37
_message = Unicode(sync=True)
37
38
_graph_url = Unicode(sync=True)
39
+ _new_url = Unicode(sync=True)
40
+ _filename = ''
41
+ _flags = {
42
+ 'save_pending': False
43
+ }
38
44
39
45
# TODO: URL for offline enterprise
40
46
def __init__(self, graph_url='https://plot.ly/~playground/7', **kwargs):
@@ -70,6 +76,10 @@ def __init__(self, graph_url='https://plot.ly/~playground/7', **kwargs):
70
76
# so we'll just cue up messages until they're ready to be sent
71
77
self._clientMessages = deque()
72
78
79
+ @property
80
+ def url(self):
81
+ return self._new_url or ''
82
+
73
83
def _handle_msg(self, message):
74
84
"""Handle a msg from the front-end.
75
85
@@ -96,6 +106,17 @@ def _handle_msg(self, message):
96
106
97
107
self._event_handlers[content['event']](self, message)
98
108
109
+ if content.get('event', '') == 'getAttributes':
110
+ self._attributes = content.get('response', {})
111
+
112
+ # there might be a save pending, use the plotly module to save
113
+ if self._flags['save_pending']:
114
+ self._flags['save_pending'] = False
115
+ url = py.plot(self._attributes, auto_open=False,
116
+ filename=self._filename, validate=False)
117
+ self._new_url = url
118
+ self._fade_to('slow', 1)
119
+
99
120
def _handle_registration(self, event_type, callback, remove):
100
121
self._event_handlers[event_type].register_callback(callback,
101
122
remove=remove)
@@ -671,6 +692,20 @@ def reorder_traces(self, current_indices, new_indices=None):
671
692
message['newIndices'] = new_indices
672
693
self._handle_outgoing_message(message)
673
694
695
+ def save(self, ignore_defaults=False, filename=''):
696
+ """
697
+ Save a copy of the current state of the widget in plotly.
698
+
699
+ :param (bool) ignore_defaults: Auto-fill in unspecified figure keys?
700
+ :param (str) filename: Name of the file on plotly.
701
+
702
+ """
703
+ self._flags['save_pending'] = True
704
+ self._filename = filename
705
+ message = {'task': 'getAttributes', 'ignoreDefaults': ignore_defaults}
706
+ self._handle_outgoing_message(message)
707
+ self._fade_to('slow', 0.1)
708
+
674
709
def extend_traces(self, update, indices=(0,), max_points=None):
675
710
""" Append data points to existing traces in the Plotly graph.
676
711
@@ -812,3 +847,11 @@ def extend_traces(self, update, indices=(0,), max_points=None):
812
847
if max_points is not None:
813
848
message['maxPoints'] = max_points
814
849
self._handle_outgoing_message(message)
850
+
851
+ def _fade_to(self, duration, opacity):
852
+ """
853
+ Change the opacity to give a visual signal to users.
854
+
855
+ """
856
+ message = {'fadeTo': True, 'duration': duration, 'opacity': opacity}
857
+ self._handle_outgoing_message(message)
0 commit comments