Skip to content

Instantly share code, notes, and snippets.

@krassowski
Created March 17, 2019 03:20
Show Gist options
  • Save krassowski/78dce5aef7cec33b8b303f6ddd225402 to your computer and use it in GitHub Desktop.
Save krassowski/78dce5aef7cec33b8b303f6ddd225402 to your computer and use it in GitHub Desktop.
Beep after long code cell execution in Jupyter Python
from time import time
from IPython import get_ipython
from IPython.display import Audio, display
class Beeper:
def __init__(self, threshold, **audio_kwargs):
self.threshold = threshold
self.start_time = None # time in sec, or None
self.audio = audio_kwargs
def pre_execute(self):
if not self.start_time:
self.start_time = time()
def post_execute(self):
end_time = time()
if self.start_time and end_time - self.start_time > self.threshold:
audio = Audio(**self.audio, autoplay=True)
display(audio)
self.start_time = None
beeper = Beeper(5, filename='beep-07.wav')
ipython = get_ipython()
ipython.events.register('pre_execute', beeper.pre_execute)
ipython.events.register('post_execute', beeper.post_execute)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment