Skip to content

Commit 3b6c7fa

Browse files
committed
PLOT: tidy imports when animating in notebooks [MPY-317]
1 parent 61484ac commit 3b6c7fa

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

metview/bindings.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,8 +1114,6 @@ def plot_to_notebook(*args, **kwargs): # pragma: no cover
11141114

11151115
animation_mode = kwargs.get("animate", "auto") # True, False or "auto"
11161116

1117-
import ipywidgets as widgets
1118-
11191117
# create all the widgets first so that the 'waiting' label is at the bottom
11201118
image_widget = widgets.Image(
11211119
format="png"
@@ -1219,10 +1217,7 @@ def plot_frame(frame_index):
12191217
def setoutput(*args, **kwargs):
12201218
if "jupyter" in args: # pragma: no cover
12211219
try:
1222-
global Image
1223-
global get_ipython
1224-
IPython = __import__("IPython", globals(), locals())
1225-
Image = IPython.display.Image
1220+
import IPython
12261221
get_ipython = IPython.get_ipython
12271222
except ImportError as imperr:
12281223
print("Could not import IPython module - plotting to Jupyter will not work")
@@ -1237,6 +1232,14 @@ def setoutput(*args, **kwargs):
12371232
"ERROR: setoutput('jupyter') was set, but we are not in a Jupyter environment"
12381233
)
12391234
raise (Exception("Could not set output to jupyter"))
1235+
1236+
try:
1237+
global widgets
1238+
widgets = __import__("ipywidgets", globals(), locals())
1239+
except ImportError as imperr:
1240+
print("Could not import ipywidgets module - plotting to Jupyter will not work")
1241+
raise imperr
1242+
12401243
else:
12411244
plot.plot_to_jupyter = False
12421245
met_setoutput(*args)

0 commit comments

Comments
 (0)