@@ -764,15 +764,23 @@ def create(object_name, *args, **kwargs):
764
764
:return: (PlotlyList|PlotlyDict) The instantiated graph object.
765
765
766
766
"""
767
- if (object_name not in graph_reference .OBJECTS and
768
- object_name not in graph_reference .ARRAYS ):
767
+ is_array = object_name in graph_reference .ARRAYS
768
+ is_object = object_name in graph_reference .OBJECTS
769
+ if not (is_array or is_object ):
769
770
raise exceptions .PlotlyError (
770
771
"'{}' is not a valid object name." .format (object_name )
771
772
)
772
- class_name = graph_reference .string_to_class_name (object_name )
773
- graph_object_class = globals ()[class_name ]
774
773
775
- return graph_object_class (* args , ** kwargs )
774
+ # We patch Figure and Data, so they actually require the subclass.
775
+ class_name = graph_reference .OBJECT_NAME_TO_CLASS_NAME .get (object_name )
776
+ if class_name in ['Figure' , 'Data' ]:
777
+ return globals ()[class_name ](* args , ** kwargs )
778
+ else :
779
+ kwargs ['_name' ] = object_name
780
+ if is_array :
781
+ return PlotlyList (* args , ** kwargs )
782
+ else :
783
+ return PlotlyDict (* args , ** kwargs )
776
784
777
785
778
786
def _add_classes_to_globals (globals ):
@@ -782,12 +790,16 @@ def _add_classes_to_globals(globals):
782
790
:param (dict) globals: The globals() dict from this module.
783
791
784
792
"""
785
- object_names = list (graph_reference .OBJECTS .keys ())
786
- array_names = list (graph_reference .ARRAYS .keys ())
787
- for object_name in object_names + array_names :
793
+ for class_name , class_dict in graph_reference .CLASSES .items ():
794
+ object_name = class_dict ['object_name' ]
795
+ base_type = class_dict ['base_type' ]
796
+
797
+ # This is for backwards compat (e.g., Trace) and future changes.
798
+ if object_name is None :
799
+ globals [class_name ] = base_type
800
+ continue
788
801
789
802
doc = graph_objs_tools .get_help (object_name )
790
- class_name = graph_reference .string_to_class_name (object_name )
791
803
if object_name in graph_reference .ARRAYS :
792
804
class_bases = (PlotlyList , )
793
805
else :
@@ -800,12 +812,6 @@ def _add_classes_to_globals(globals):
800
812
801
813
globals [class_name ] = cls
802
814
803
- for key , val in graph_reference .CLASS_NAMES_TO_OBJECT_NAMES .items ():
804
- if val == object_name :
805
- class_dict .update (__name__ = key )
806
- cls = type (str (key ), class_bases , class_dict )
807
- globals [key ] = cls
808
-
809
815
810
816
def _patch_figure_class (figure_class ):
811
817
@@ -980,8 +986,7 @@ def get_data(self, flatten=False):
980
986
_add_classes_to_globals (globals ())
981
987
_patch_figure_class (globals ()['Figure' ])
982
988
_patch_data_class (globals ()['Data' ])
983
- Trace = dict # for backwards compat.
984
989
985
990
# We don't want to expose this module to users, just the classes.
986
991
# See http://blog.labix.org/2008/06/27/watch-out-for-listdictkeys-in-python-3
987
- __all__ = list (graph_reference .CLASS_NAMES_TO_OBJECT_NAMES .keys ())
992
+ __all__ = list (graph_reference .CLASSES .keys ())
0 commit comments