Skip to content

Commit 619dec5

Browse files
committed
Add FigureWidget import support back
1 parent 6a64e35 commit 619dec5

File tree

7 files changed

+177
-98
lines changed

7 files changed

+177
-98
lines changed

packages/python/plotly/codegen/__init__.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
import os.path as opath
34
import shutil
45
import subprocess
@@ -264,25 +265,33 @@ def perform_codegen():
264265
for dep_clas in DEPRECATED_DATATYPES:
265266
root_datatype_imports.append(f"._deprecations.{dep_clas}")
266267

267-
root_alls = []
268-
269-
# TODO: come back to FigureWidget
270-
optional_figure_widget_import = """
271-
from .._validate import validate
268+
validate_import = "from .._validate import validate\n"
269+
optional_figure_widget_import = f"""
270+
if sys.version_info < (3, 7):
271+
try:
272+
import ipywidgets
273+
from distutils.version import LooseVersion
274+
if LooseVersion(ipywidgets.__version__) >= LooseVersion('7.0.0'):
275+
from ..graph_objs._figurewidget import FigureWidget
276+
del LooseVersion
277+
del ipywidgets
278+
except ImportError:
279+
pass
280+
else:
281+
orig_getattr = __getattr__
282+
def __getattr__(import_name):
283+
if import_name == "FigureWidget":
284+
try:
285+
import ipywidgets
286+
from distutils.version import LooseVersion
287+
if LooseVersion(ipywidgets.__version__) >= LooseVersion('7.0.0'):
288+
from ..graph_objs._figurewidget import FigureWidget
289+
return FigureWidget
290+
except ImportError:
291+
pass
292+
293+
return orig_getattr(import_name)
272294
"""
273-
# optional_figure_widget_import = f"""
274-
#
275-
# try:
276-
# import ipywidgets
277-
# from distutils.version import LooseVersion
278-
# if LooseVersion(ipywidgets.__version__) >= LooseVersion('7.0.0'):
279-
# from ._figurewidget import FigureWidget
280-
# __all__.append("FigureWidget")
281-
# del LooseVersion
282-
# del ipywidgets
283-
# except ImportError:
284-
# pass
285-
# """
286295
# ### __all__ ###
287296
for path_parts, class_names in alls.items():
288297
if path_parts and class_names:
@@ -296,7 +305,7 @@ def perform_codegen():
296305
rel_classes = datatype_rel_class_imports[path_parts]
297306
rel_modules = datatype_rel_module_imports.get(path_parts, [])
298307
if path_parts == ():
299-
init_extra = optional_figure_widget_import
308+
init_extra = validate_import + optional_figure_widget_import
300309
else:
301310
init_extra = ""
302311
write_init_py(graph_objs_pkg, path_parts, rel_modules, rel_classes, init_extra)
@@ -312,9 +321,10 @@ def perform_codegen():
312321
]
313322

314323
graph_objects_init_source = build_from_imports_py(
315-
graph_objects_rel_modules, graph_objects_rel_classes,
324+
graph_objects_rel_modules, graph_objects_rel_classes, init_extra=optional_figure_widget_import
316325
)
317-
graph_objects_path = opath.join(outdir, "graph_objects.py")
326+
graph_objects_path = opath.join(outdir, "graph_objects", "__init__.py")
327+
os.makedirs(opath.join(outdir, "graph_objects"), exist_ok=True)
318328
with open(graph_objects_path, "wt") as f:
319329
f.write(graph_objects_init_source)
320330

packages/python/plotly/codegen/datatypes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,13 @@ def _subplot_re_match(self, prop):
155155

156156
child_datatype_nodes = node.child_datatypes
157157
subtype_nodes = child_datatype_nodes
158-
valid_props = sorted([node.name_property for node in subtype_nodes + literal_nodes])
158+
valid_props_list = sorted([node.name_property for node in subtype_nodes + literal_nodes])
159159
buffer.write(f"""
160160
# class properties
161161
# --------------------
162162
_parent_path_str = '{node.parent_path_str}'
163163
_path_str = '{node.path_str}'
164-
_valid_props = {{"{'", "'.join(valid_props)}"}}
164+
_valid_props = {{"{'", "'.join(valid_props_list)}"}}
165165
""")
166166

167167
# ### Property definitions ###
@@ -319,7 +319,7 @@ def __init__(self"""
319319
buffer.write(f"""
320320
# Override _valid_props for instance so that instance can mutate set
321321
# to support subplot properties (e.g. xaxis2)
322-
self._valid_props = {{"{'", "'.join(valid_props)}"}}
322+
self._valid_props = {{"{'", "'.join(valid_props_list)}"}}
323323
""")
324324

325325
buffer.write(f"""

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,7 +1937,8 @@ def _initialize_layout_template(self):
19371937
if self._layout_obj._props.get('template', None) is None:
19381938
if pio.templates.default is not None:
19391939
with validate(False):
1940-
template_dict = pio.templates[pio.templates.default].to_plotly_json()
1940+
# Assume default template is already validated
1941+
template_dict = pio.templates[pio.templates.default]
19411942
self._layout_obj.template = template_dict
19421943
else:
19431944
self._layout_obj.template = None
@@ -3460,7 +3461,6 @@ def __setitem__(self, prop, value):
34603461
prop = prop[0]
34613462

34623463
if validate._should_validate:
3463-
# ### Validate prop ###
34643464
if prop not in self._valid_props:
34653465
self._raise_on_invalid_property_error(prop)
34663466

@@ -3481,8 +3481,18 @@ def __setitem__(self, prop, value):
34813481
else:
34823482
# Make sure properties dict is initialized
34833483
self._init_props()
3484+
if isinstance(value, BasePlotlyType):
3485+
# Extract json from graph objects
3486+
value = value.to_plotly_json()
3487+
34843488
self._props[prop] = value
34853489

3490+
# Remove any already constructed graph object so that it will be
3491+
# reconstructed on property access
3492+
self._compound_props.pop(prop, None)
3493+
self._compound_array_props.pop(prop, None)
3494+
3495+
34863496
# Handle non-scalar case
34873497
# ----------------------
34883498
# e.g. ('foo', 1), ()

packages/python/plotly/plotly/graph_objects.py renamed to packages/python/plotly/plotly/graph_objects/__init__.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,33 @@
257257
"..graph_objs.Histogram2dcontour",
258258
],
259259
)
260+
261+
262+
if sys.version_info < (3, 7):
263+
try:
264+
import ipywidgets
265+
from distutils.version import LooseVersion
266+
267+
if LooseVersion(ipywidgets.__version__) >= LooseVersion("7.0.0"):
268+
from ..graph_objs._figurewidget import FigureWidget
269+
del LooseVersion
270+
del ipywidgets
271+
except ImportError:
272+
pass
273+
else:
274+
orig_getattr = __getattr__
275+
276+
def __getattr__(import_name):
277+
if import_name == "FigureWidget":
278+
try:
279+
import ipywidgets
280+
from distutils.version import LooseVersion
281+
282+
if LooseVersion(ipywidgets.__version__) >= LooseVersion("7.0.0"):
283+
from ..graph_objs._figurewidget import FigureWidget
284+
285+
return FigureWidget
286+
except ImportError:
287+
pass
288+
289+
return orig_getattr(import_name)

packages/python/plotly/plotly/graph_objs/__init__.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,33 @@
258258
],
259259
)
260260

261-
262261
from .._validate import validate
262+
263+
if sys.version_info < (3, 7):
264+
try:
265+
import ipywidgets
266+
from distutils.version import LooseVersion
267+
268+
if LooseVersion(ipywidgets.__version__) >= LooseVersion("7.0.0"):
269+
from ..graph_objs._figurewidget import FigureWidget
270+
del LooseVersion
271+
del ipywidgets
272+
except ImportError:
273+
pass
274+
else:
275+
orig_getattr = __getattr__
276+
277+
def __getattr__(import_name):
278+
if import_name == "FigureWidget":
279+
try:
280+
import ipywidgets
281+
from distutils.version import LooseVersion
282+
283+
if LooseVersion(ipywidgets.__version__) >= LooseVersion("7.0.0"):
284+
from ..graph_objs._figurewidget import FigureWidget
285+
286+
return FigureWidget
287+
except ImportError:
288+
pass
289+
290+
return orig_getattr(import_name)

packages/python/plotly/plotly/graph_objs/_layout.py

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5520,88 +5520,88 @@ def __init__(
55205520
# Override _valid_props for instance so that instance can mutate set
55215521
# to support subplot properties (e.g. xaxis2)
55225522
self._valid_props = {
5523-
"sliders",
5524-
"colorway",
5525-
"title",
5526-
"ternary",
5527-
"margin",
5528-
"extendsunburstcolors",
5529-
"modebar",
5530-
"mapbox",
5531-
"updatemenus",
5532-
"treemapcolorway",
5533-
"updatemenudefaults",
5534-
"annotationdefaults",
5535-
"images",
5536-
"dragmode",
5537-
"geo",
5538-
"spikedistance",
5539-
"waterfallgroupgap",
5540-
"separators",
5541-
"scene",
5542-
"hoverlabel",
5543-
"funnelgap",
5544-
"titlefont",
5545-
"uniformtext",
5546-
"autosize",
5547-
"paper_bgcolor",
5548-
"hoverdistance",
5549-
"violinmode",
5550-
"width",
5551-
"extendfunnelareacolors",
5552-
"sliderdefaults",
5553-
"funnelgroupgap",
5554-
"waterfallgap",
5555-
"datarevision",
55565523
"angularaxis",
5557-
"legend",
5558-
"plot_bgcolor",
5559-
"sunburstcolorway",
5524+
"annotationdefaults",
55605525
"annotations",
5561-
"metasrc",
5562-
"font",
5563-
"barmode",
5564-
"yaxis",
5526+
"autosize",
55655527
"bargap",
5566-
"orientation",
5567-
"hidesources",
5568-
"extendpiecolors",
5569-
"violingap",
5570-
"template",
5571-
"hovermode",
5572-
"violingroupgap",
5573-
"xaxis",
5574-
"uirevision",
5575-
"extendtreemapcolors",
5576-
"transition",
5577-
"polar",
5578-
"editrevision",
5579-
"boxgroupgap",
5580-
"grid",
5528+
"bargroupgap",
5529+
"barmode",
55815530
"barnorm",
5582-
"funnelareacolorway",
5583-
"imagedefaults",
5584-
"piecolorway",
5585-
"shapes",
5586-
"clickmode",
5531+
"boxgap",
5532+
"boxgroupgap",
55875533
"boxmode",
5588-
"showlegend",
5589-
"direction",
55905534
"calendar",
5535+
"clickmode",
5536+
"coloraxis",
5537+
"colorscale",
5538+
"colorway",
5539+
"datarevision",
5540+
"direction",
5541+
"dragmode",
5542+
"editrevision",
5543+
"extendfunnelareacolors",
5544+
"extendpiecolors",
5545+
"extendsunburstcolors",
5546+
"extendtreemapcolors",
5547+
"font",
5548+
"funnelareacolorway",
5549+
"funnelgap",
5550+
"funnelgroupgap",
5551+
"funnelmode",
5552+
"geo",
5553+
"grid",
5554+
"height",
5555+
"hiddenlabels",
55915556
"hiddenlabelssrc",
5557+
"hidesources",
5558+
"hoverdistance",
5559+
"hoverlabel",
5560+
"hovermode",
5561+
"imagedefaults",
5562+
"images",
5563+
"legend",
5564+
"mapbox",
5565+
"margin",
55925566
"meta",
5593-
"waterfallmode",
5567+
"metasrc",
5568+
"modebar",
5569+
"orientation",
5570+
"paper_bgcolor",
5571+
"piecolorway",
5572+
"plot_bgcolor",
5573+
"polar",
5574+
"radialaxis",
5575+
"scene",
55945576
"selectdirection",
5595-
"boxgap",
55965577
"selectionrevision",
5597-
"coloraxis",
5598-
"radialaxis",
5599-
"height",
5600-
"colorscale",
5601-
"hiddenlabels",
5578+
"separators",
56025579
"shapedefaults",
5603-
"bargroupgap",
5604-
"funnelmode",
5580+
"shapes",
5581+
"showlegend",
5582+
"sliderdefaults",
5583+
"sliders",
5584+
"spikedistance",
5585+
"sunburstcolorway",
5586+
"template",
5587+
"ternary",
5588+
"title",
5589+
"titlefont",
5590+
"transition",
5591+
"treemapcolorway",
5592+
"uirevision",
5593+
"uniformtext",
5594+
"updatemenudefaults",
5595+
"updatemenus",
5596+
"violingap",
5597+
"violingroupgap",
5598+
"violinmode",
5599+
"waterfallgap",
5600+
"waterfallgroupgap",
5601+
"waterfallmode",
5602+
"width",
5603+
"xaxis",
5604+
"yaxis",
56055605
}
56065606

56075607
# Validate arg

packages/python/plotly/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ def run(self):
475475
"plotly.figure_factory",
476476
"plotly.data",
477477
"plotly.express",
478+
"plotly.graph_objects",
478479
"_plotly_utils",
479480
"_plotly_utils.colors",
480481
"_plotly_future_",

0 commit comments

Comments
 (0)