forked from plotly/plotly.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_objs.py
2036 lines (1583 loc) · 74.8 KB
/
graph_objs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
graph_objs
==========
A module that understands plotly language and can manage the json
structures. This module defines two base classes: PlotlyList and PlotlyDict.
The former inherits from `list` and the latter inherits from `dict`. and is
A third structure, PlotlyTrace, is also considered a base class for all
subclassing 'trace' objects like Scatter, Box, Bar, etc. It is also not meant
to instantiated by users.
Goals of this module:
---------------------
* A dict/list with the same entries as a PlotlyDict/PlotlyList should look
exactly the same once a call is made to plot.
* Only mutate object structure when users ASK for it. (some magic now...)
* It should always be possible to get a dict/list JSON representation from a
graph_objs object and it should always be possible to make a graph_objs object
from a dict/list JSON representation.
"""
from __future__ import absolute_import
import copy
import re
import warnings
from collections import OrderedDict
import six
from plotly import exceptions, graph_reference
from plotly.graph_objs import graph_objs_tools
_subplot_regex = re.compile(r'(?P<digits>\d+$)')
class PlotlyBase(object):
"""
Base object for PlotlyList and PlotlyDict.
"""
_name = None
_parent = None
_parent_key = None
def _get_path(self):
"""
Get a tuple of the str keys and int indices for this object's path.
:return: (tuple)
"""
path = []
parents = self._get_parents()
parents.reverse()
children = [self] + parents[:-1]
for parent, child in zip(parents, children):
path.append(child._parent_key)
path.reverse()
return tuple(path)
def _get_parents(self):
"""
Get a list of all the parent objects above this one.
:return: (list[PlotlyBase])
"""
parents = []
parent = self._parent
while parent is not None:
parents.append(parent)
parent = parent._parent
parents.reverse()
return parents
def _get_parent_object_names(self):
"""
Get a list of the names of the parent objects above this one.
:return: (list[str])
"""
parents = self._get_parents()
return [parent._name for parent in parents]
def _get_class_name(self):
"""For convenience. See `graph_reference.object_name_to_class_name`."""
return graph_reference.object_name_to_class_name(self._name)
def help(self, return_help=False):
"""
Print a help string for this object.
:param (bool) return_help: Return help string instead of prining?
:return: (None|str) Optionally can return help string.
"""
object_name = self._name
path = self._get_path()
parent_object_names = self._get_parent_object_names()
help_string = graph_objs_tools.get_help(object_name, path,
parent_object_names)
if return_help:
return help_string
print(help_string)
def to_graph_objs(self, **kwargs):
"""Everything is cast into graph_objs. Here for backwards compat."""
pass
def validate(self):
"""Everything is *always* validated now. Keep for backwards compat."""
pass
class PlotlyList(list, PlotlyBase):
"""
Base class for list-like Plotly objects.
"""
_name = None
def __init__(self, *args, **kwargs):
_raise = kwargs.get('_raise', True)
if self._name is None:
self.__dict__['_name'] = kwargs.pop('_name', None)
self.__dict__['_parent'] = kwargs.get('_parent')
self.__dict__['_parent_key'] = kwargs.get('_parent_key')
if self._name is None:
raise exceptions.PlotlyError(
"PlotlyList is a base class. It's shouldn't be instantiated."
)
if args and isinstance(args[0], dict):
note = (
"Just like a `list`, `{name}` must be instantiated "
"with a *single* collection.\n"
"In other words these are OK:\n"
">>> {name}()\n"
">>> {name}([])\n"
">>> {name}([dict()])\n"
">>> {name}([dict(), dict()])\n"
"However, these don't make sense:\n"
">>> {name}(dict())\n"
">>> {name}(dict(), dict())"
.format(name=self._get_class_name())
)
raise exceptions.PlotlyListEntryError(self, [0], notes=[note])
super(PlotlyList, self).__init__()
for index, value in enumerate(list(*args)):
value = self._value_to_graph_object(index, value, _raise=_raise)
if isinstance(value, PlotlyBase):
self.append(value)
def __setitem__(self, index, value, _raise=True):
"""Override to enforce validation."""
if not isinstance(index, int):
if _raise:
index_type = type(index)
raise TypeError('Index must be int, not {}'.format(index_type))
return
if index >= len(self):
raise IndexError(index)
value = self._value_to_graph_object(index, value, _raise=_raise)
if isinstance(value, (PlotlyDict, PlotlyList)):
super(PlotlyList, self).__setitem__(index, value)
def __setattr__(self, key, value):
raise exceptions.PlotlyError('Setting attributes on a PlotlyList is '
'not allowed')
def __iadd__(self, other):
"""Defines the `+=` operator, which we map to extend."""
self.extend(other)
return self
def __copy__(self):
# TODO: https://github.com/plotly/python-api/issues/291
return GraphObjectFactory.create(self._name, _parent=self._parent,
_parent_key=self._parent_key, *self)
def __deepcopy__(self, memodict={}):
# TODO: https://github.com/plotly/python-api/issues/291
return self.__copy__()
def _value_to_graph_object(self, index, value, _raise=True):
"""
Attempt to change the given value into a graph object.
If _raise is False, this won't raise. If the entry can't be converted,
`None` is returned, meaning the caller should ignore the value or
discard it as a failed conversion.
:param (dict) value: A dict to be converted into a graph object.
:param (bool) _raise: If False, ignore bad values instead of raising.
:return: (PlotlyBase|None) The graph object or possibly `None`.
"""
if not isinstance(value, dict):
if _raise:
path = self._get_path() + (index, )
raise exceptions.PlotlyListEntryError(self, path)
else:
return
items = graph_reference.ARRAYS[self._name]['items']
for i, item in enumerate(items, 1):
try:
return GraphObjectFactory.create(item, _raise=_raise,
_parent=self,
_parent_key=index, **value)
except exceptions.PlotlyGraphObjectError:
if i == len(items) and _raise:
raise
def append(self, value):
"""Override to enforce validation."""
index = len(self) # used for error messages
value = self._value_to_graph_object(index, value)
super(PlotlyList, self).append(value)
def extend(self, iterable):
"""Override to enforce validation."""
for value in iterable:
index = len(self)
value = self._value_to_graph_object(index, value)
super(PlotlyList, self).append(value)
def insert(self, index, value):
"""Override to enforce validation."""
value = self._value_to_graph_object(index, value)
super(PlotlyList, self).insert(index, value)
def update(self, changes, make_copies=False):
"""
Update current list with changed_list, which must be iterable.
:param (dict|list[dict]) changes:
:param (bool) make_copies:
Because mutable objects contain references to their values, updating
multiple items in a list will cause the items to all reference the same
original set of objects. To change this behavior add
`make_copies=True` which makes deep copies of the update items and
therefore break references.
"""
if isinstance(changes, dict):
changes = [changes]
for index in range(len(self)):
try:
update = changes[index % len(changes)]
except ZeroDivisionError:
pass
else:
if make_copies:
self[index].update(copy.deepcopy(update))
else:
self[index].update(update)
def strip_style(self):
"""Strip style by calling `stip_style` on children items."""
for plotly_dict in self:
plotly_dict.strip_style()
def get_data(self, flatten=False):
"""
Returns the JSON for the plot with non-data elements stripped.
:param (bool) flatten: {'a': {'b': ''}} --> {'a.b': ''}
:returns: (dict|list) Depending on (flat|unflat)
"""
l = list()
for plotly_dict in self:
l += [plotly_dict.get_data(flatten=flatten)]
del_indicies = [index for index, item in enumerate(self)
if len(item) == 0]
del_ct = 0
for index in del_indicies:
del self[index - del_ct]
del_ct += 1
if flatten:
d = {}
for i, e in enumerate(l):
for k, v in e.items():
key = "{0}.{1}".format(i, k)
d[key] = v
return d
else:
return l
def get_ordered(self, **kwargs):
"""All children are already validated. Just use get_ordered on them."""
return [child.get_ordered() for child in self]
def to_string(self, level=0, indent=4, eol='\n',
pretty=True, max_chars=80):
"""Get formatted string by calling `to_string` on children items."""
if not len(self):
return "{name}()".format(name=self._get_class_name())
string = "{name}([{eol}{indent}".format(
name=self._get_class_name(),
eol=eol,
indent=' ' * indent * (level + 1))
for index, entry in enumerate(self):
string += entry.to_string(level=level+1,
indent=indent,
eol=eol,
pretty=pretty,
max_chars=max_chars)
if index < len(self) - 1:
string += ",{eol}{indent}".format(
eol=eol,
indent=' ' * indent * (level + 1))
string += (
"{eol}{indent}])").format(eol=eol, indent=' ' * indent * level)
return string
def force_clean(self, **kwargs):
"""Remove empty/None values by calling `force_clean()` on children."""
for entry in self:
entry.force_clean()
del_indicies = [index for index, item in enumerate(self)
if len(item) == 0]
del_ct = 0
for index in del_indicies:
del self[index - del_ct]
del_ct += 1
class PlotlyDict(dict, PlotlyBase):
"""
Base class for dict-like Plotly objects.
"""
_name = None
_parent_key = None
_valid_attributes = None
_deprecated_attributes = None
_subplot_attributes = None
def __init__(self, *args, **kwargs):
_raise = kwargs.pop('_raise', True)
if self._name is None:
self.__dict__['_name'] = kwargs.pop('_name', None)
self.__dict__['_parent'] = kwargs.pop('_parent', None)
self.__dict__['_parent_key'] = kwargs.pop('_parent_key', None)
if self._name is None:
raise exceptions.PlotlyError(
"PlotlyDict is a base class. It's shouldn't be instantiated."
)
super(PlotlyDict, self).__init__()
if self._name in graph_reference.TRACE_NAMES:
self['type'] = self._name
# force key-value pairs to go through validation
d = {key: val for key, val in dict(*args, **kwargs).items()}
for key, val in d.items():
self.__setitem__(key, val, _raise=_raise)
def __dir__(self):
"""Dynamically return the existing and possible attributes."""
return sorted(list(self._get_valid_attributes()))
def __getitem__(self, key):
"""Calls __missing__ when key is not found. May mutate object."""
if key not in self:
self.__missing__(key)
return super(PlotlyDict, self).__getitem__(key)
def __setattr__(self, key, value):
"""Maps __setattr__ onto __setitem__"""
self.__setitem__(key, value)
def __setitem__(self, key, value, _raise=True):
"""Validates/Converts values which should be Graph Objects."""
if not isinstance(key, six.string_types):
if _raise:
raise TypeError('Key must be string, not {}'.format(type(key)))
return
if key.endswith('src'):
if key in self._get_valid_attributes():
value = graph_objs_tools.assign_id_to_src(key, value)
return super(PlotlyDict, self).__setitem__(key, value)
subplot_key = self._get_subplot_key(key)
if subplot_key is not None:
value = self._value_to_graph_object(subplot_key, value,
_raise=_raise)
if isinstance(value, (PlotlyDict, PlotlyList)):
return super(PlotlyDict, self).__setitem__(key, value)
if key not in self._get_valid_attributes():
if key in self._get_deprecated_attributes():
warnings.warn(
"Oops! '{attribute}' has been deprecated in "
"'{object_name}'\nThis may still work, but you should "
"update your code when possible.\n\n"
"Run `.help('{attribute}')` for more information."
.format(attribute=key, object_name=self._name)
)
# this means deprecated attrs get set *as-is*!
return super(PlotlyDict, self).__setitem__(key, value)
else:
if _raise:
path = self._get_path() + (key, )
raise exceptions.PlotlyDictKeyError(self, path)
return
if self._get_attribute_role(key) == 'object':
value = self._value_to_graph_object(key, value, _raise=_raise)
if not isinstance(value, (PlotlyDict, PlotlyList)):
return
super(PlotlyDict, self).__setitem__(key, value)
def __getattr__(self, key):
"""Python only calls this when key is missing!"""
try:
return self.__getitem__(key)
except KeyError:
raise AttributeError(key)
def __copy__(self):
# TODO: https://github.com/plotly/python-api/issues/291
return GraphObjectFactory.create(self._name, _parent=self.parent,
_parent_key=self._parent_key, **self)
def __deepcopy__(self, memodict={}):
# TODO: https://github.com/plotly/python-api/issues/291
return self.__copy__()
def __missing__(self, key):
"""Mimics defaultdict. This is called from __getitem__ when key DNE."""
if key in self._get_valid_attributes():
if self._get_attribute_role(key) == 'object':
value = GraphObjectFactory.create(key, _parent=self,
_parent_key=key)
return super(PlotlyDict, self).__setitem__(key, value)
subplot_key = self._get_subplot_key(key)
if subplot_key is not None:
value = GraphObjectFactory.create(subplot_key, _parent=self,
_parent_key=key)
super(PlotlyDict, self).__setitem__(key, value)
def _get_attribute_role(self, key, value=None):
"""See `graph_reference.get_role`."""
object_name = self._name
parent_object_names = self._get_parent_object_names()
return graph_reference.get_role(
object_name, key, value=value,
parent_object_names=parent_object_names
)
def _get_valid_attributes(self):
"""See `graph_reference.get_valid_attributes`."""
if self._valid_attributes is None:
parent_object_names = self._get_parent_object_names()
valid_attributes = graph_reference.get_valid_attributes(
self._name, parent_object_names
)
self.__dict__['_valid_attributes'] = valid_attributes
return self._valid_attributes
def _get_deprecated_attributes(self):
"""See `graph_reference.get_deprecated_attributes`."""
if self._deprecated_attributes is None:
parent_object_names = self._get_parent_object_names()
deprecated_attributes = graph_reference.get_deprecated_attributes(
self._name, parent_object_names
)
self.__dict__['_deprecated_attributes'] = deprecated_attributes
return self._deprecated_attributes
def _get_subplot_attributes(self):
"""See `graph_reference.get_subplot_attributes`."""
if self._subplot_attributes is None:
parent_object_names = self._get_parent_object_names()
subplot_attributes = graph_reference.get_subplot_attributes(
self._name, parent_object_names
)
self.__dict__['_subplot_attributes'] = subplot_attributes
return self._subplot_attributes
def _get_subplot_key(self, key):
"""Some keys can have appended integers, this handles that."""
match = _subplot_regex.search(key)
if match:
root_key = key[:match.start()]
if (root_key in self._get_subplot_attributes() and
not match.group('digits').startswith('0')):
return root_key
def _value_to_graph_object(self, key, value, _raise=True):
"""
Attempt to convert value to graph object.
:param (str|unicode) key: Should be an object_name from GRAPH_REFERENCE
:param (dict) value: This will fail if it's not a dict.
:param (bool) _raise: Flag to prevent inappropriate erring.
:return: (PlotlyList|PlotlyDict|None) `None` if `_raise` and failure.
"""
if key in graph_reference.ARRAYS:
val_types = (list, )
else:
val_types = (dict, )
if not isinstance(value, val_types):
if _raise:
path = self._get_path() + (key, )
raise exceptions.PlotlyDictValueError(self, path)
else:
return
# this can be `None` when `_raise == False`
return GraphObjectFactory.create(key, value, _raise=_raise,
_parent=self, _parent_key=key)
def help(self, attribute=None, return_help=False):
"""
Print help string for this object or an attribute of this object.
:param (str) attribute: A valid attribute string for this object.
:param (bool) return_help: Return help_string instead of printing it?
:return: (None|str)
"""
if not attribute:
return super(PlotlyDict, self).help(return_help=return_help)
object_name = self._name
path = self._get_path()
parent_object_names = self._get_parent_object_names()
help_string = graph_objs_tools.get_help(object_name, path,
parent_object_names, attribute)
if return_help:
return help_string
print(help_string)
def update(self, dict1=None, **dict2):
"""
Update current dict with dict1 and then dict2.
This recursively updates the structure of the original dictionary-like
object with the new entries in the second and third objects. This
allows users to update with large, nested structures.
Note, because the dict2 packs up all the keyword arguments, you can
specify the changes as a list of keyword arguments.
Examples:
# update with dict
obj = Layout(title='my title', xaxis=XAxis(range=[0,1], domain=[0,1]))
update_dict = dict(title='new title', xaxis=dict(domain=[0,.8]))
obj.update(update_dict)
obj
{'title': 'new title', 'xaxis': {'range': [0,1], 'domain': [0,.8]}}
# update with list of keyword arguments
obj = Layout(title='my title', xaxis=XAxis(range=[0,1], domain=[0,1]))
obj.update(title='new title', xaxis=dict(domain=[0,.8]))
obj
{'title': 'new title', 'xaxis': {'range': [0,1], 'domain': [0,.8]}}
This 'fully' supports duck-typing in that the call signature is
identical, however this differs slightly from the normal update
method provided by Python's dictionaries.
"""
if dict1 is not None:
for key, val in list(dict1.items()):
if key in self:
if isinstance(self[key], (PlotlyDict, PlotlyList)):
self[key].update(val)
else:
self[key] = val
else:
self[key] = val
if len(dict2):
for key, val in list(dict2.items()):
if key in self:
if isinstance(self[key], (PlotlyDict, PlotlyList)):
self[key].update(val)
else:
self[key] = val
else:
self[key] = val
def strip_style(self):
"""
Recursively strip style from the current representation.
All PlotlyDicts and PlotlyLists are guaranteed to survive the
stripping process, though they made be left empty. This is allowable.
Keys that will be stripped in this process are tagged with
`'type': 'style'` in graph_objs_meta.json. Note that a key tagged as
style, but with an array as a value may still be considered data.
"""
keys = list(self.keys())
for key in keys:
if isinstance(self[key], (PlotlyDict, PlotlyList)):
self[key].strip_style()
else:
if self._get_attribute_role(key, value=self[key]) == 'style':
del self[key]
# this is for backwards compat when we updated graph reference.
elif self._name == 'layout' and key == 'autosize':
del self[key]
def get_data(self, flatten=False):
"""Returns the JSON for the plot with non-data elements stripped."""
d = dict()
for key, val in list(self.items()):
if isinstance(val, (PlotlyDict, PlotlyList)):
sub_data = val.get_data(flatten=flatten)
if flatten:
for sub_key, sub_val in sub_data.items():
key_string = "{0}.{1}".format(key, sub_key)
d[key_string] = sub_val
else:
d[key] = sub_data
else:
if self._get_attribute_role(key, value=val) == 'data':
d[key] = val
# we use the name to help make data frames
if self._name in graph_reference.TRACE_NAMES and key == 'name':
d[key] = val
keys = list(d.keys())
for key in keys:
if isinstance(d[key], (dict, list)):
if len(d[key]) == 0:
del d[key]
return d
def get_ordered(self, **kwargs):
"""Return a predictable, OrderedDict version of self."""
keys = sorted(self.keys(), key=graph_objs_tools.sort_keys)
ordered = OrderedDict()
for key in keys:
if isinstance(self[key], PlotlyBase):
ordered[key] = self[key].get_ordered()
else:
ordered[key] = self[key]
return ordered
def to_string(self, level=0, indent=4, eol='\n',
pretty=True, max_chars=80):
"""
Returns a formatted string showing graph_obj constructors.
:param (int) level: The number of indentations to start with.
:param (int) indent: The indentation amount.
:param (str) eol: The end of line character(s).
:param (bool) pretty: Curtail long list output with a '..' ?
:param (int) max_chars: The max characters per line.
Example:
print(obj.to_string())
"""
if not len(self):
return "{name}()".format(name=self._get_class_name())
string = "{name}(".format(name=self._get_class_name())
if self._name in graph_reference.TRACE_NAMES:
keys = [key for key in self.keys() if key != 'type']
else:
keys = self.keys()
keys = sorted(keys, key=graph_objs_tools.sort_keys)
num_keys = len(keys)
for index, key in enumerate(keys, 1):
string += "{eol}{indent}{key}=".format(
eol=eol,
indent=' ' * indent * (level+1),
key=key)
if isinstance(self[key], PlotlyBase):
string += self[key].to_string(level=level+1,
indent=indent,
eol=eol,
pretty=pretty,
max_chars=max_chars)
else:
if pretty: # curtail representation if too many chars
max_len = (max_chars -
indent*(level + 1) -
len(key + "=") -
len(eol))
if index < num_keys:
max_len -= len(',') # remember the comma!
if isinstance(self[key], list):
s = "[]"
for iii, entry in enumerate(self[key], 1):
if iii < len(self[key]):
s_sub = graph_objs_tools.curtail_val_repr(
entry,
max_chars=max_len - len(s),
add_delim=True
)
else:
s_sub = graph_objs_tools.curtail_val_repr(
entry,
max_chars=max_len - len(s),
add_delim=False
)
s = s[:-1] + s_sub + s[-1]
if len(s) == max_len:
break
string += s
else:
string += graph_objs_tools.curtail_val_repr(
self[key], max_len)
else: # they want it all!
string += repr(self[key])
if index < num_keys:
string += ","
string += "{eol}{indent})".format(eol=eol, indent=' ' * indent * level)
return string
def force_clean(self, **kwargs):
"""Recursively remove empty/None values."""
keys = list(self.keys())
for key in keys:
try:
self[key].force_clean()
except AttributeError:
pass
if isinstance(self[key], (dict, list)):
if len(self[key]) == 0:
del self[key] # clears empty collections!
elif self[key] is None:
del self[key]
class GraphObjectFactory(object):
"""GraphObject creation in this module should run through this factory."""
@staticmethod
def create(object_name, *args, **kwargs):
"""
Create a graph object from the OBJECTS dict by name, args, and kwargs.
:param (str) object_name: A valid object name from OBJECTS.
:param args: Arguments to pass to class constructor.
:param kwargs: Keyword arguments to pass to class constructor.
:return: (PlotlyList|PlotlyDict) The instantiated graph object.
"""
is_array = object_name in graph_reference.ARRAYS
is_object = object_name in graph_reference.OBJECTS
if not (is_array or is_object):
raise exceptions.PlotlyError(
"'{}' is not a valid object name.".format(object_name)
)
# We patch Figure and Data, so they actually require the subclass.
class_name = graph_reference.OBJECT_NAME_TO_CLASS_NAME.get(object_name)
if class_name in ['Figure', 'Data', 'Frames']:
return globals()[class_name](*args, **kwargs)
else:
kwargs['_name'] = object_name
if is_array:
return PlotlyList(*args, **kwargs)
else:
return PlotlyDict(*args, **kwargs)
# AUTO-GENERATED BELOW. DO NOT EDIT! See makefile.
class AngularAxis(PlotlyDict):
"""
Valid attributes for 'angularaxis' at path [] under parents ():
['categoryarray', 'categoryarraysrc', 'categoryorder', 'color',
'direction', 'domain', 'dtick', 'endpadding', 'exponentformat',
'gridcolor', 'gridwidth', 'hoverformat', 'layer', 'linecolor',
'linewidth', 'nticks', 'period', 'range', 'rotation',
'separatethousands', 'showexponent', 'showgrid', 'showline',
'showticklabels', 'showtickprefix', 'showticksuffix', 'thetaunit',
'tick0', 'tickangle', 'tickcolor', 'tickfont', 'tickformat',
'tickformatstops', 'ticklen', 'tickmode', 'tickorientation',
'tickprefix', 'ticks', 'ticksuffix', 'ticktext', 'ticktextsrc',
'tickvals', 'tickvalssrc', 'tickwidth', 'type', 'visible']
Run `<angularaxis-object>.help('attribute')` on any of the above.
'<angularaxis-object>' is the object at []
"""
_name = 'angularaxis'
class Annotation(PlotlyDict):
"""
Valid attributes for 'annotation' at path [] under parents ():
['align', 'arrowcolor', 'arrowhead', 'arrowside', 'arrowsize',
'arrowwidth', 'ax', 'axref', 'ay', 'ayref', 'bgcolor', 'bordercolor',
'borderpad', 'borderwidth', 'captureevents', 'clicktoshow', 'font',
'height', 'hoverlabel', 'hovertext', 'opacity', 'ref', 'showarrow',
'standoff', 'startarrowhead', 'startarrowsize', 'startstandoff',
'text', 'textangle', 'valign', 'visible', 'width', 'x', 'xanchor',
'xclick', 'xref', 'xshift', 'y', 'yanchor', 'yclick', 'yref', 'yshift',
'z']
Run `<annotation-object>.help('attribute')` on any of the above.
'<annotation-object>' is the object at []
"""
_name = 'annotation'
class Annotations(PlotlyList):
"""
Valid items for 'annotations' at path [] under parents ():
['Annotation']
"""
_name = 'annotations'
class Area(PlotlyDict):
"""
Valid attributes for 'area' at path [] under parents ():
['customdata', 'customdatasrc', 'hoverinfo', 'hoverinfosrc',
'hoverlabel', 'ids', 'idssrc', 'legendgroup', 'marker', 'name',
'opacity', 'r', 'rsrc', 'selectedpoints', 'showlegend', 'stream', 't',
'tsrc', 'type', 'uid', 'visible']
Run `<area-object>.help('attribute')` on any of the above.
'<area-object>' is the object at []
"""
_name = 'area'
class Bar(PlotlyDict):
"""
Valid attributes for 'bar' at path [] under parents ():
['bardir', 'base', 'basesrc', 'cliponaxis', 'constraintext',
'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y',
'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertext', 'hovertextsrc',
'ids', 'idssrc', 'insidetextfont', 'legendgroup', 'marker', 'name',
'offset', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont',
'r', 'rsrc', 'selected', 'selectedpoints', 'showlegend', 'stream', 't',
'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc',
'tsrc', 'type', 'uid', 'unselected', 'visible', 'width', 'widthsrc',
'x', 'x0', 'xaxis', 'xcalendar', 'xsrc', 'y', 'y0', 'yaxis',
'ycalendar', 'ysrc']
Run `<bar-object>.help('attribute')` on any of the above.
'<bar-object>' is the object at []
"""
_name = 'bar'
class Box(PlotlyDict):
"""
Valid attributes for 'box' at path [] under parents ():
['boxmean', 'boxpoints', 'customdata', 'customdatasrc', 'fillcolor',
'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'ids', 'idssrc',
'jitter', 'legendgroup', 'line', 'marker', 'name', 'notched',
'notchwidth', 'opacity', 'orientation', 'pointpos', 'selected',
'selectedpoints', 'showlegend', 'stream', 'text', 'textsrc', 'type',
'uid', 'unselected', 'visible', 'whiskerwidth', 'x', 'x0', 'xaxis',
'xcalendar', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'ysrc']
Run `<box-object>.help('attribute')` on any of the above.
'<box-object>' is the object at []
"""
_name = 'box'
class Candlestick(PlotlyDict):
"""
Valid attributes for 'candlestick' at path [] under parents ():
['close', 'closesrc', 'customdata', 'customdatasrc', 'decreasing',
'high', 'highsrc', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'ids',
'idssrc', 'increasing', 'legendgroup', 'line', 'low', 'lowsrc', 'name',
'opacity', 'open', 'opensrc', 'selectedpoints', 'showlegend', 'stream',
'text', 'textsrc', 'type', 'uid', 'visible', 'whiskerwidth', 'x',
'xaxis', 'xcalendar', 'xsrc', 'yaxis']
Run `<candlestick-object>.help('attribute')` on any of the above.
'<candlestick-object>' is the object at []
"""
_name = 'candlestick'
class Carpet(PlotlyDict):
"""
Valid attributes for 'carpet' at path [] under parents ():
['a', 'a0', 'aaxis', 'asrc', 'b', 'b0', 'baxis', 'bsrc', 'carpet',
'cheaterslope', 'color', 'customdata', 'customdatasrc', 'da', 'db',
'font', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'ids', 'idssrc',
'legendgroup', 'name', 'opacity', 'selectedpoints', 'showlegend',
'stream', 'type', 'uid', 'visible', 'x', 'xaxis', 'xsrc', 'y', 'yaxis',
'ysrc']
Run `<carpet-object>.help('attribute')` on any of the above.
'<carpet-object>' is the object at []
"""
_name = 'carpet'
class Choropleth(PlotlyDict):
"""
Valid attributes for 'choropleth' at path [] under parents ():
['autocolorscale', 'colorbar', 'colorscale', 'customdata',
'customdatasrc', 'geo', 'hoverinfo', 'hoverinfosrc', 'hoverlabel',
'ids', 'idssrc', 'legendgroup', 'locationmode', 'locations',
'locationssrc', 'marker', 'name', 'opacity', 'reversescale',
'selected', 'selectedpoints', 'showlegend', 'showscale', 'stream',
'text', 'textsrc', 'type', 'uid', 'unselected', 'visible', 'z',
'zauto', 'zmax', 'zmin', 'zsrc']
Run `<choropleth-object>.help('attribute')` on any of the above.
'<choropleth-object>' is the object at []
"""
_name = 'choropleth'
class ColorBar(PlotlyDict):
"""
Valid attributes for 'colorbar' at path [] under parents ():
['bgcolor', 'bordercolor', 'borderwidth', 'dtick', 'exponentformat',
'len', 'lenmode', 'nticks', 'outlinecolor', 'outlinewidth',
'separatethousands', 'showexponent', 'showticklabels',
'showtickprefix', 'showticksuffix', 'thickness', 'thicknessmode',
'tick0', 'tickangle', 'tickcolor', 'tickfont', 'tickformat',
'tickformatstops', 'ticklen', 'tickmode', 'tickprefix', 'ticks',
'ticksuffix', 'ticktext', 'ticktextsrc', 'tickvals', 'tickvalssrc',
'tickwidth', 'title', 'titlefont', 'titleside', 'x', 'xanchor', 'xpad',
'y', 'yanchor', 'ypad']
Run `<colorbar-object>.help('attribute')` on any of the above.
'<colorbar-object>' is the object at []
"""
_name = 'colorbar'
class Contour(PlotlyDict):
"""
Valid attributes for 'contour' at path [] under parents ():
['autocolorscale', 'autocontour', 'colorbar', 'colorscale',
'connectgaps', 'contours', 'customdata', 'customdatasrc', 'dx', 'dy',
'fillcolor', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'ids',
'idssrc', 'legendgroup', 'line', 'name', 'ncontours', 'opacity',
'reversescale', 'selectedpoints', 'showlegend', 'showscale', 'stream',
'text', 'textsrc', 'transpose', 'type', 'uid', 'visible', 'x', 'x0',
'xaxis', 'xcalendar', 'xsrc', 'xtype', 'y', 'y0', 'yaxis', 'ycalendar',
'ysrc', 'ytype', 'z', 'zauto', 'zhoverformat', 'zmax', 'zmin', 'zsrc']