Skip to content

Commit 8db46b5

Browse files
committed
duck typed, generic src key and objects
1 parent a487e47 commit 8db46b5

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

plotly/graph_objs/graph_objs.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,10 @@ class PlotlyDict(dict):
299299
def __init__(self, *args, **kwargs):
300300
class_name = self.__class__.__name__
301301

302-
for src in ('xsrc', 'ysrc'):
303-
if src in kwargs and isinstance(kwargs[src], Column):
304-
kwargs[src] = self._assign_id_to_src(src, kwargs[src])
302+
for key in kwargs:
303+
if utils.is_source_key(key):
304+
#if src in kwargs and isinstance(kwargs[src], Column):
305+
kwargs[key] = self._assign_id_to_src(key, kwargs[key])
305306

306307
super(PlotlyDict, self).__init__(*args, **kwargs)
307308
if issubclass(NAME_TO_CLASS[class_name], PlotlyTrace):
@@ -314,20 +315,43 @@ def __init__(self, *args, **kwargs):
314315
"a user interface.")
315316

316317
def __setitem__(self, key, value):
318+
317319
if key in ('xsrc', 'ysrc'):
318320
value = self._assign_id_to_src(key, value)
319321

320322
return super(PlotlyDict, self).__setitem__(key, value)
321323

322324
def _assign_id_to_src(self, src_name, src_value):
323-
if isinstance(src_value, Column):
324-
if src_value.id == '':
325+
if isinstance(src_value, basestring):
326+
src_id = src_value
327+
else:
328+
try:
329+
src_id = src_value.id
330+
except:
331+
err = ("{} does not have an `id` property. "
332+
"{} needs to be assigned to either an "
333+
"object with an `id` (like a "
334+
"plotly.grid_objs.Column) or a string. "
335+
"The `id` is a unique identifier "
336+
"assigned by the Plotly webserver "
337+
"to this grid column.")
338+
src_value_str = str(src_value)
339+
err = err.format(src_name, src_value_str)
340+
raise exceptions.InputError(err)
341+
342+
if src_id == '':
343+
if isinstance(src_value, Column):
325344
err = exceptions.COLUMN_NOT_YET_UPLOADED_MESSAGE
326345
err.format(column_name=src_value.name, reference=src_name)
327346
raise exceptions.InputError(err)
328347
else:
329-
src_value = src_value.id
330-
return src_value
348+
err = ("{} should be a unique identifier "
349+
"string assigned by the Plotly "
350+
"server to this to this grid column, "
351+
"not an empty string.".format(src_name))
352+
raise exceptions.InputError(err)
353+
354+
return src_id
331355

332356
def update(self, dict1=None, **dict2):
333357
"""Update current dict with dict1 and then dict2.

plotly/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os.path
1111
import sys
1212
import threading
13+
import re
1314

1415
### incase people are using threading, we lock file reads
1516
lock = threading.Lock()
@@ -183,3 +184,12 @@ def get_first_duplicate(items):
183184
else:
184185
return item
185186
return None
187+
188+
189+
### source key
190+
def is_source_key(key):
191+
src_regex = re.compile(r'.+src$')
192+
if src_regex.match(key) is not None:
193+
return True
194+
else:
195+
return False

0 commit comments

Comments
 (0)