I often run long-running cells in my IPython notebook. I\'d like the notebook to automatically beep or play a sound when the cell is finished executing. Is there some way to
Based on @krassowski's answer, here's my solution to this. The main difference is that @krassowski's runs after every every "long" cell execution (where you define what "long" means); I prefer to explicitly say when I want the sound played, so I've wrapped this into a line / cell magic (on GitHub and copied below):
"""
Adds a magic to IPython which will play a given sound when a cell finishes running.
Requires Python 3.6+.
Put this file in, e.g., ~/.ipython/profile_default/startup to load this magic on startup.
Usage:
``
%notify [-u/--url URL] [command]
``
Examples
``
%notify # no command needed
%notify run_long_command()
%notify -u https://www.example.com/sound.wav run_long_command()
``
There's also a cell magic version (don't put commands on the first line if using this).
``
%%notify [-u/--url URL]
command1()
command2()
...
``
To always play your preferred audio file, just change the default below.
"""
from typing import Optional
from IPython import get_ipython
from IPython.display import Audio, display
from IPython.core.magic import line_cell_magic, Magics, magics_class
from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring
class _InvisibleAudio(Audio):
"""
An invisible (`display: none`) `Audio` element which removes itself when finished playing.
Taken from https://stackoverflow.com/a/50648266.
"""
def _repr_html_(self) -> str:
audio = super()._repr_html_()
audio = audio.replace(
"