-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Animations/Frames in Python API #584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 26 commits
cb4552c
b4c3953
54c1386
d19c6ca
efa390e
eb57218
5482b53
b2b8451
a156503
d173cbb
d17a222
49bdc97
4fbdc99
dbb386f
bd2132b
26c3714
0c7bb1c
4acc146
c195e78
6f12ba8
253228f
405e71d
c1727d8
fca8fed
587dfa5
495b015
dd20177
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -836,6 +836,22 @@ def __init__(self, *args, **kwargs): | |
_parent_key='data') | ||
figure_class.__init__ = __init__ | ||
|
||
# TODO better integrate frames into Figure - #604 | ||
def __setitem__(self, key, value, **kwargs): | ||
if key == 'frames': | ||
super(PlotlyDict, self).__setitem__(key, value) | ||
else: | ||
super(figure_class, self).__setitem__(key, value, **kwargs) | ||
figure_class.__setitem__ = __setitem__ | ||
|
||
def _get_valid_attributes(self): | ||
super(figure_class, self)._get_valid_attributes() | ||
# TODO better integrate frames into Figure - #604 | ||
if 'frames' not in self._valid_attributes: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto hear, link it to that same issue ^^ |
||
self._valid_attributes.add('frames') | ||
return self._valid_attributes | ||
figure_class._get_valid_attributes = _get_valid_attributes | ||
|
||
def get_data(self, flatten=False): | ||
""" | ||
Returns the JSON for the plot with non-data elements stripped. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
'ErrorZ': {'object_name': 'error_z', 'base_type': dict}, | ||
'Figure': {'object_name': 'figure', 'base_type': dict}, | ||
'Font': {'object_name': 'font', 'base_type': dict}, | ||
'Frames': {'object_name': 'frames', 'base_type': dict}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary? Did I accidentally leave this in or something ;P? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I added it in. I don't know if it's actually being used though. |
||
'Heatmap': {'object_name': 'heatmap', 'base_type': dict}, | ||
'Histogram': {'object_name': 'histogram', 'base_type': dict}, | ||
'Histogram2d': {'object_name': 'histogram2d', 'base_type': dict}, | ||
|
@@ -87,7 +88,6 @@ def get_graph_reference(): | |
|
||
graph_reference_url = '{}{}?sha1={}'.format(plotly_api_domain, | ||
GRAPH_REFERENCE_PATH, sha1) | ||
|
||
try: | ||
response = requests.get(graph_reference_url, | ||
timeout=GRAPH_REFERENCE_DOWNLOAD_TIMEOUT) | ||
|
@@ -196,11 +196,9 @@ def get_valid_attributes(object_name, parent_object_names=()): | |
# These are for documentation and quick lookups. They're just strings. | ||
valid_attributes = set() | ||
for attributes_dict in attributes.values(): | ||
|
||
for key, val in attributes_dict.items(): | ||
if key not in GRAPH_REFERENCE['defs']['metaKeys']: | ||
valid_attributes.add(key) | ||
|
||
deprecated_attributes = attributes_dict.get('_deprecated', {}) | ||
for key, val in deprecated_attributes.items(): | ||
if key not in GRAPH_REFERENCE['defs']['metaKeys']: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,29 +118,96 @@ class Grid(MutableSequence): | |
py.plot([trace], filename='graph from grid') | ||
``` | ||
""" | ||
def __init__(self, iterable_of_columns): | ||
def __init__(self, columns_or_json, fid=None): | ||
""" | ||
Initialize a grid with an iterable of | ||
`plotly.grid_objs.Column objects | ||
Initialize a grid with an iterable of `plotly.grid_objs.Column` | ||
objects or a json/dict describing a grid. See second usage example | ||
below for the necessary structure of the dict. | ||
|
||
Usage example: | ||
:param (str|bool) fid: should not be accessible to users. Default | ||
is 'None' but if a grid is retrieved via `py.get_grid()` then the | ||
retrieved grid response will contain the fid which will be | ||
necessary to set `self.id` and `self._columns.id` below. | ||
|
||
Example from iterable of columns: | ||
``` | ||
column_1 = Column([1, 2, 3], 'time') | ||
column_2 = Column([4, 2, 5], 'voltage') | ||
grid = Grid([column_1, column_2]) | ||
``` | ||
Example from json grid | ||
``` | ||
grid_json = { | ||
'cols': { | ||
'time': {'data': [1, 2, 3], 'order': 0, 'uid': '4cd7fc'}, | ||
'voltage': {'data': [4, 2, 5], 'order': 1, 'uid': u'2744be'} | ||
} | ||
} | ||
grid = Grid(grid_json) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💥 🎎 :) |
||
``` | ||
""" | ||
|
||
# TODO: verify that columns are actually columns | ||
if isinstance(columns_or_json, dict): | ||
# check that fid is entered | ||
if fid is None: | ||
raise exceptions.PlotlyError( | ||
"If you are manually converting a raw json/dict grid " | ||
"into a Grid instance, you must ensure that make " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐐 |
||
"'fid' is set to your file ID. This looks like " | ||
"'username:187'." | ||
) | ||
# TODO: verify that fid is a correct fid if a string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean validate it? It'd be a whole extra api call to actually check. I think you don't need to worry about it. |
||
self.id = fid | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐄 non-blocking, but I'd go with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, if you call |
||
|
||
# check if 'cols' is a root key | ||
if 'cols' not in columns_or_json: | ||
raise exceptions.PlotlyError( | ||
"'cols' must be a root key in your json grid." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know how I've loosened the warning message, saying that cols must be there, but Is this okay for you? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
) | ||
|
||
# check if 'data', 'order' and 'uid' are not in columns | ||
grid_col_keys = ['data', 'order', 'uid'] | ||
|
||
for column_name in columns_or_json['cols']: | ||
for key in grid_col_keys: | ||
if key not in columns_or_json['cols'][column_name]: | ||
raise exceptions.PlotlyError( | ||
"Each column name of your dictionary must have " | ||
"'data', 'order' and 'uid' as keys." | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 cool. |
||
# collect and sort all orders in case orders do not start | ||
# at zero or there are jump discontinuities between them | ||
all_orders = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @theengineear This method works. 👍 |
||
for column_name in columns_or_json['cols'].keys(): | ||
all_orders.append(columns_or_json['cols'][column_name]['order']) | ||
all_orders.sort() | ||
|
||
# put columns in order in a list | ||
ordered_columns = [] | ||
for order in all_orders: | ||
for column_name in columns_or_json['cols'].keys(): | ||
if columns_or_json['cols'][column_name]['order'] == order: | ||
break | ||
|
||
ordered_columns.append(Column( | ||
columns_or_json['cols'][column_name]['data'], | ||
column_name) | ||
) | ||
self._columns = ordered_columns | ||
|
||
# fill in uids | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎎 |
||
for column in self: | ||
column.id = self.id + ':' + columns_or_json['cols'][column.name]['uid'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐄 we typically use |
||
|
||
column_names = [column.name for column in iterable_of_columns] | ||
duplicate_name = utils.get_first_duplicate(column_names) | ||
if duplicate_name: | ||
err = exceptions.NON_UNIQUE_COLUMN_MESSAGE.format(duplicate_name) | ||
raise exceptions.InputError(err) | ||
else: | ||
column_names = [column.name for column in columns_or_json] | ||
duplicate_name = utils.get_first_duplicate(column_names) | ||
if duplicate_name: | ||
err = exceptions.NON_UNIQUE_COLUMN_MESSAGE.format(duplicate_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
raise exceptions.InputError(err) | ||
|
||
self._columns = list(iterable_of_columns) | ||
self.id = '' | ||
self._columns = list(columns_or_json) | ||
self.id = '' | ||
|
||
def __repr__(self): | ||
return self._columns.__repr__() | ||
|
@@ -187,3 +254,27 @@ def get_column(self, column_name): | |
for column in self._columns: | ||
if column.name == column_name: | ||
return column | ||
|
||
def get_column_reference(self, column_name): | ||
""" | ||
Returns the column reference of given column in the grid by its name. | ||
|
||
Raises an error if the column name is not in the grid. Otherwise, | ||
returns the fid:uid pair, which may be the empty string. | ||
""" | ||
col_names = [] | ||
for column in self._columns: | ||
col_names.append(column.name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐄 this is confusing placement of this little code block. Can you move it into the
|
||
|
||
column_id = None | ||
for column in self._columns: | ||
if column.name == column_name: | ||
column_id = column.id | ||
break | ||
|
||
if column_id is None: | ||
raise exceptions.PlotlyError( | ||
"Whoops, that column name doesn't match any of the column " | ||
"names in your grid. You must pick from {cols}".format(col_names) | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
return column_id |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,8 +162,16 @@ def init_notebook_mode(connected=False): | |
|
||
def _plot_html(figure_or_data, config, validate, default_width, | ||
default_height, global_requirejs): | ||
|
||
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate) | ||
# force no validation if frames is in the call | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
# TODO - add validation for frames in call - #605 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed now 👍 |
||
if 'frames' in figure_or_data: | ||
figure = tools.return_figure_from_figure_or_data( | ||
figure_or_data, False | ||
) | ||
else: | ||
figure = tools.return_figure_from_figure_or_data( | ||
figure_or_data, validate | ||
) | ||
|
||
width = figure.get('layout', {}).get('width', default_width) | ||
height = figure.get('layout', {}).get('height', default_height) | ||
|
@@ -185,6 +193,8 @@ def _plot_html(figure_or_data, config, validate, default_width, | |
plotdivid = uuid.uuid4() | ||
jdata = json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder) | ||
jlayout = json.dumps(figure.get('layout', {}), cls=utils.PlotlyJSONEncoder) | ||
if 'frames' in figure_or_data: | ||
jframes = json.dumps(figure.get('frames', {}), cls=utils.PlotlyJSONEncoder) | ||
|
||
configkeys = ( | ||
'editable', | ||
|
@@ -225,11 +235,30 @@ def _plot_html(figure_or_data, config, validate, default_width, | |
link_text = link_text.replace('plot.ly', link_domain) | ||
config['linkText'] = link_text | ||
|
||
script = 'Plotly.newPlot("{id}", {data}, {layout}, {config})'.format( | ||
id=plotdivid, | ||
data=jdata, | ||
layout=jlayout, | ||
config=jconfig) | ||
if 'frames' in figure_or_data: | ||
script = ''' | ||
Plotly.plot( | ||
'{id}', | ||
{data}, | ||
{layout}, | ||
{config} | ||
).then(function () {add_frames}).then(function(){animate}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That PR has been merged and is on plotly.js/master (unreleased, but coming very soon?) and makes the syntax somewhat simpler. I'd elaborate here, but the PR description explains it fairly clearly. Calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, fantastic. I think Chris mentioned that I'd have to do some rewriting when that gets added. I'll leave it for now, but does that mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, should just be a simplification. Long story short, given
I'm not 100% sure what that means for validation, but on a strictly abstract level, it's conceptually simple. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, sorry, unless you were referring to python arguments instead of data validation… That PR just changes the call signature. Not 100% sure the other concerns, but feel free to ask whatever questions you need and I'll do my best to get you what you need. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'm referring to this
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, got it. Yeah, it should permit frames. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am still receiving the error. Anything I can do to fix? I There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm… not sure. It seems like that's a whitelisting issue on the plotly (not js) side. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, so after playing |
||
'''.format( | ||
id=plotdivid, | ||
data=jdata, | ||
layout=jlayout, | ||
config=jconfig, | ||
add_frames="{" + "return Plotly.addFrames('{id}',{frames}".format( | ||
id=plotdivid, frames=jframes | ||
) + ");}", | ||
animate="{" + "Plotly.animate('{id}');".format(id=plotdivid) + "}" | ||
) | ||
else: | ||
script = 'Plotly.newPlot("{id}", {data}, {layout}, {config})'.format( | ||
id=plotdivid, | ||
data=jdata, | ||
layout=jlayout, | ||
config=jconfig) | ||
|
||
optional_line1 = ('require(["plotly"], function(Plotly) {{ ' | ||
if global_requirejs else '') | ||
|
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,5 +21,8 @@ | |
grid_ops, | ||
meta_ops, | ||
file_ops, | ||
get_config | ||
get_config, | ||
get_grid, | ||
create_animations, | ||
icreate_animations | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a comment about this one. Maybe tie it to a new issue to better-integrate
frames
intoFigure
? As always, pls put the issue numer (or link) in theTODO
comment.