Skip to content

Commit 13e7f99

Browse files
committed
PLOT: add use_widget argument to setoutput [MPY-322]
1 parent 0de9707 commit 13e7f99

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

metview/bindings.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,11 +1080,15 @@ def merge(*args):
10801080
class Plot:
10811081
def __init__(self):
10821082
self.plot_to_jupyter = False
1083+
self.use_widget = True
10831084
self.jupyter_args = {}
10841085

10851086
def __call__(self, *args, **kwargs):
10861087
if self.plot_to_jupyter: # pragma: no cover
1087-
return plot_to_notebook(args, **kwargs)
1088+
if self.use_widget:
1089+
return plot_to_notebook(args, **kwargs)
1090+
else:
1091+
return plot_to_notebook_return_image(args, **kwargs)
10881092
else:
10891093
map_outputs = {
10901094
"png": png_output,
@@ -1204,6 +1208,21 @@ def plot_frame(frame_index):
12041208
image_widget.layout.visibility = "visible"
12051209

12061210

1211+
def plot_to_notebook_return_image(*args, **kwargs): # pragma: no cover
1212+
1213+
from IPython.display import Image
1214+
1215+
f, tmp = tempfile.mkstemp(".png")
1216+
os.close(f)
1217+
base, ext = os.path.splitext(tmp)
1218+
plot.jupyter_args.update(output_name=base, output_name_first_page_number="off")
1219+
met_setoutput(png_output(plot.jupyter_args))
1220+
met_plot(*args)
1221+
image = Image(tmp)
1222+
os.unlink(tmp)
1223+
return image
1224+
1225+
12071226
# On a test system, importing IPython took approx 0.5 seconds, so to avoid that hit
12081227
# under most circumstances, we only import it when the user asks for Jupyter
12091228
# functionality. Since this occurs within a function, we need a little trickery to
@@ -1221,6 +1240,9 @@ def setoutput(*args, **kwargs):
12211240
# test whether we're in the Jupyter environment
12221241
if get_ipython() is not None:
12231242
plot.plot_to_jupyter = True
1243+
plot.use_widget = kwargs.get("use_widget", True)
1244+
if "use_widget" in kwargs:
1245+
del kwargs["use_widget"]
12241246
plot.jupyter_args = kwargs
12251247
else:
12261248
print(

0 commit comments

Comments
 (0)