Skip to content

Commit 12642d0

Browse files
committed
PLOT: add play button to Jupyter animation widget [MPY-313]
1 parent 867516c commit 12642d0

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Changelog for Metview's Python interface
33
========================================
44

5+
1.7.1
6+
------------------
7+
- added automatic play and speed controls to animated plots in Jupyter notebooks
8+
9+
510
1.7.0
611
------------------
712
- added animate=True argument to plot() command for animated plots in Jupyter notebooks

metview/bindings.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,11 +1149,35 @@ def animate(*args, **kwargs):
11491149
readout=True,
11501150
)
11511151

1152-
image_widget.layout.visibility = "hidden"
1153-
frame_widget.layout.visibility = "hidden"
1152+
play_widget = widgets.Play(
1153+
value=1,
1154+
min=1,
1155+
max=1,
1156+
step=1,
1157+
interval=500,
1158+
description="Play animation",
1159+
disabled=False,
1160+
)
1161+
1162+
speed_widget = widgets.IntSlider(
1163+
value=3,
1164+
min=1,
1165+
max=20,
1166+
step=1,
1167+
description="Speed",
1168+
disabled=False,
1169+
continuous_update=True,
1170+
readout=True,
1171+
)
1172+
1173+
widgets.jslink((play_widget, "value"), (frame_widget, "value"))
1174+
play_and_speed_widget = widgets.HBox([play_widget, speed_widget])
1175+
controls = widgets.VBox([image_widget, frame_widget, play_and_speed_widget])
1176+
1177+
controls.layout.visibility = "hidden"
11541178
waitl_widget = widgets.Label(value="Generating plots....")
11551179
frame_widget.layout.width = "800px"
1156-
display(image_widget, frame_widget, waitl_widget)
1180+
display(controls, waitl_widget)
11571181

11581182
# plot all frames to a temporary directory owned by Metview to enure cleanup
11591183
tempdirpath = tempfile.mkdtemp(dir=os.environ.get("METVIEW_TMPDIR", None))
@@ -1173,6 +1197,7 @@ def animate(*args, **kwargs):
11731197
files = [os.path.join(tempdirpath, f) for f in sorted(filenames)]
11741198
frame_widget.max = len(files)
11751199
frame_widget.description = "Frame (" + str(len(files)) + ") :"
1200+
play_widget.max = len(files)
11761201

11771202
def plot_frame(frame_index):
11781203
im_file = open(files[frame_index - 1], "rb")
@@ -1186,11 +1211,15 @@ def on_frame_change(change):
11861211
plot_frame(1)
11871212
frame_widget.observe(on_frame_change, names="value")
11881213

1214+
def on_speed_change(change):
1215+
play_widget.interval = 1500 / change["new"]
1216+
1217+
speed_widget.observe(on_speed_change, names="value")
1218+
11891219
# everything is ready now, so hide the 'waiting' label
11901220
# and reveal the plot and the frame slider
11911221
waitl_widget.layout.visibility = "hidden"
1192-
image_widget.layout.visibility = "visible"
1193-
frame_widget.layout.visibility = "visible"
1222+
controls.layout.visibility = "visible"
11941223

11951224

11961225
# On a test system, importing IPython took approx 0.5 seconds, so to avoid that hit

0 commit comments

Comments
 (0)