COLAB
I mixed the solutions I found in some places 1 2 3 4
a) Create a global exception handler that beeps on errors
b) Create a simple function that you place at the end of the long-running cell (some other approaches on links)
You can change the sounds to anything you like.
Note: The sounds inside the Exception Handler and the beep_completed() are very different and with reason. The first is short and non-annoying and the second is long and pleasant (in case you are away from computer so you clearly hear that the task is completed). In any case you can replace them.
Note: There is a line that only applies to Colab. If you can provide the one for Jupyter I will gladly update the answer.
# This line is specific for Colab (please provide alternative for Jupyter)
from google.colab import output
from IPython.core.ultratb import AutoFormattedTB
# Catch any Exception, play error sound and re-raise the Exception
#-------------------------------------------------
# initialize the formatter for making the tracebacks into strings
itb = AutoFormattedTB(mode = 'Plain', tb_offset = 1)
# this function will be called on exceptions in any cell
def custom_exc(shell, etype, evalue, tb, tb_offset=None):
# still show the error within the notebook, don't just swallow it
shell.showtraceback((etype, evalue, tb), tb_offset=tb_offset)
# Play an audio beep. Any audio URL will do.
output.eval_js('new Audio("http://soundbible.com/grab.php?id=419&type=wav").play()')
# # grab the traceback and make it into a list of strings
# stb = itb.structured_traceback(etype, evalue, tb)
# sstb = itb.stb2text(stb)
# print (sstb) # <--- this is the variable with the traceback string
# print ("sending mail")
# send_mail_to_myself(sstb)
# this registers a custom exception handler for the whole current notebook
get_ipython().set_custom_exc((Exception,), custom_exc)
#------------------------------------------
# Function to play a sound (to put at the end of a long job)
def beep_completed():
#url_sound="http://soundbible.com/grab.php?id=1795&type=mp3";
output.eval_js('new Audio("http://soundbible.com/grab.php?id=1795&type=mp3").play()')
# Just play it with
beep_completed()
Jupyter
Comment out as needed. If you want to use a local file, you have to download it first and maybe adjust the path.
from IPython.display import Audio, display
# ----- error sound --------
def play_sound_error(self, etype, value, tb, tb_offset=None):
self.showtraceback((etype, value, tb), tb_offset=tb_offset)
v1="http://soundbible.com/grab.php?id=419&type=wav" # Short Error Beep sound
v2="https://wav-sounds.com/wp-content/uploads/2017/09/Various-02.wav" # Short Baby cry
display(Audio(url=v1, autoplay=True))
v1="../sound_error_beep.wav" # Short Error Beep sound
v2="../sound_baby_cry.wav" # Short Baby cry
display(Audio(filename=v1, autoplay=True))
# ----- atach it to all Exceptions
get_ipython().set_custom_exc((Exception,), play_sound_error)
# ----- success sound --------
def play_sound_success():
v1='http://soundbible.com/grab.php?id=1795&type=wav'
#display(Audio(url=v1, autoplay=True))
display(Audio(filename='../sound_success.wav', autoplay=True))