Skip to content

Commit 2455dc1

Browse files
author
Jon M. Mease
committed
make ipywidgets an optional dependency
If ipywidgets is not installed the FigureWidget is not imported into the plotly.graph_objs module
1 parent bd16d77 commit 2455dc1

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

codegen/__init__.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,21 @@ def perform_codegen():
174174
write_graph_objs_graph_objs(outdir)
175175

176176
# ### Add Figure and FigureWidget ###
177-
root_datatype_pairs = path_to_datatype_import_info[()]
178-
root_datatype_pairs.append(('._figure', 'Figure'))
179-
root_datatype_pairs.append(('._figurewidget', 'FigureWidget'))
177+
root_datatype_imports = path_to_datatype_import_info[()]
178+
root_datatype_imports.append(('._figure', 'Figure'))
179+
180+
optional_figure_widget_import = """
181+
try:
182+
import ipywidgets
183+
from ._figurewidget import FigureWidget
184+
except ImportError:
185+
pass
186+
187+
"""
188+
root_datatype_imports.append(optional_figure_widget_import)
180189

181190
# ### Add deprecations ###
182-
root_datatype_pairs.append(('._deprecations', DEPRECATED_DATATYPES.keys()))
191+
root_datatype_imports.append(('._deprecations', DEPRECATED_DATATYPES.keys()))
183192

184193
# ### Output datatype __init__.py files ###
185194
graph_objs_pkg = opath.join(outdir, 'graph_objs')

codegen/utils.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,40 @@ def format_and_write_source_py(py_source, filepath):
6565
f.write(formatted_source)
6666

6767

68-
def build_from_imports_py(import_pairs):
68+
def build_from_imports_py(imports_info):
6969
"""
7070
Build a string containing a series of `from X import Y` lines
7171
7272
Parameters
7373
----------
74-
import_pairs : list of (str, str or list of str)
75-
List of pairs where first entry is the package to be imported from.
76-
The second entry is either a string of the single name to be
77-
imported, or a list of names to be imported.
74+
imports_info : str or list of (str, str or list of str)
75+
List of import info
76+
If element is a pair first entry is the package to be imported
77+
from and the second entry is either a string of the single name
78+
to be
79+
80+
If element is a string, insert string directly
7881
Returns
7982
-------
8083
str
8184
String containing a series of imports
8285
"""
8386
buffer = StringIO()
84-
for from_pkg, class_name in import_pairs:
85-
if isinstance(class_name, str):
86-
class_name_str = class_name
87-
else:
88-
class_name_str = '(' + ', '.join(class_name) + ')'
87+
for import_info in imports_info:
8988

90-
buffer.write(f"""\
89+
if isinstance(import_info, tuple):
90+
from_pkg, class_name = import_info
91+
if isinstance(class_name, str):
92+
class_name_str = class_name
93+
else:
94+
class_name_str = '(' + ', '.join(class_name) + ')'
95+
96+
buffer.write(f"""\
9197
from {from_pkg} import {class_name_str}\n""")
9298

99+
elif isinstance(import_info, str):
100+
buffer.write(import_info)
101+
93102
return buffer.getvalue()
94103

95104

plotly/graph_objs/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@
6464
from plotly.graph_objs import layout
6565
from ._frame import Frame
6666
from ._figure import Figure
67-
from ._figurewidget import FigureWidget
67+
68+
try:
69+
import ipywidgets
70+
from ._figurewidget import FigureWidget
71+
except ImportError:
72+
pass
73+
6874
from ._deprecations import (
6975
Data, Annotations, Frames, AngularAxis, Annotation, ColorBar, Contours,
7076
ErrorX, ErrorY, ErrorZ, Font, Legend, Line, Margin, Marker, RadialAxis,

0 commit comments

Comments
 (0)