8000 Merge pull request #181 from plotly/widgets-get-attributes · pferate/python-api@c102140 · GitHub
[go: up one dir, main page]

Skip to content

Commit c102140

Browse files
committed
Merge pull request plotly#181 from plotly/widgets-get-attributes
Widgets save graph
2 parents c225d6f + 9f5d32d commit c102140

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

circle/setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ for version in ${PLOTLY_PYTHON_VERSIONS[@]}; do
3636
# handle funkiness around python 2.6
3737
if [ ${version:0:3} == '2.6' ]
3838
then
39-
pip install simplejson ordereddict ||
39+
pip install -e '.[PY2.6]' ||
4040
error_exit "${LINENO}: can't install extras for Python ${version}"
4141
pip install -r ${PLOTLY_OPTIONAL_REQUIREMENTS_FILE_2_6} ||
4242
error_exit "${LINENO}: can't install optional for Python ${version}"

plotly/widgets/graphWidget.js

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plotly/widgets/graph_widget.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from IPython.utils.traitlets import Unicode
88
from IPython.display import Javascript, display
99

10+
import plotly.plotly.plotly as py
1011
from plotly import utils, tools
1112
from plotly.graph_objs import Figure
1213
from pkg_resources import resource_string
@@ -35,6 +36,11 @@ class GraphWidget(widgets.DOMWidget):
3536
_view_name = Unicode('GraphView', sync=True)
3637
_message = Unicode(sync=True)
3738
_graph_url = Unicode(sync=True)
39+
_new_url = Unicode(sync=True)
40+
_filename = ''
41+
_flags = {
42+
'save_pending': False
43+
}
3844

3945
# TODO: URL for offline enterprise
4046
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):
7076
# so we'll just cue up messages until they're ready to be sent
7177
self._clientMessages = deque()
7278

79+
@property
80+
def url(self):
81+
return self._new_url or ''
82+
7383
def _handle_msg(self, message):
7484
"""Handle a msg from the front-end.
7585
@@ -96,6 +106,17 @@ def _handle_msg(self, message):
96106

97107
self._event_handlers[content['event']](self, message)
98108

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+
99120
def _handle_registration(self, event_type, callback, remove):
100121
self._event_handlers[event_type].register_callback(callback,
101122
remove=remove)
@@ -671,6 +692,20 @@ def reorder_traces(self, current_indices, new_indices=None):
671692
message['newIndices'] = new_indices
672693
self._handle_outgoing_message(message)
673694

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+
674709
def extend_traces(self, update, indices=(0,), max_points=None):
675710
""" Append data points to existing traces in the Plotly graph.
676711
@@ -812,3 +847,11 @@ def extend_traces(self, update, indices=(0,), max_points=None):
812847
if max_points is not None:
813848
message['maxPoints'] = max_points
814849
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)

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@ def readme():
4242
'plotly/matplotlylib/mplexporter/renderers'],
4343
package_data={'plotly': ['graph_reference/*.json', 'widgets/*.js']},
4444
install_requires=['requests', 'six', 'pytz'],
45-
extras_require={"PY2.6": ['simplejson', 'ordereddict']},
45+
extras_require={"PY2.6": ['simplejson', 'ordereddict',
46+
'requests[security]']},
4647
zip_safe=False)

0 commit comments

Comments
 (0)
0