How to make beeps

I like to set up long-running tasks to play a beep at the end so that I can temporarily turn my attention to another tab or window, yet not miss the task’s conclusion.

I found that you can do this in Jupyter notebooks via defining the following function in a cell:

%%javascript
// Original source -- https://gist.github.com/xem/670dec8e70815842eb95
Jupyter.beep = () => {
    var A=new AudioContext();
    var o = A.createOscillator();
    o.connect(A.destination);
    o.start(0);
    var duration = 500;
    setTimeout(function(){o.stop(); A.close();}, duration);
}

Then, when you want the beep, create a cell that has this in it:

%%javascript
Jupyter.beep();

(I believe you can’t mix Python and Javascript in one cell.)


FYI, it is even easier to play a beep from the shell. If you want a beep after somecommand, do this:

somecommand; tput bel

I have this in my .bashrc file:

alias beep='tput bel'
13 Likes

Thanks for nice tip!

In my case, I use messenger app API to send notification to my phone with extra message like test setting + validation score.

I’m using this package https://github.com/golbin/line-notify since I use Line messenger.

I guess many other messenger apps also support notification API.

2 Likes

For jobs which only take a few minutes, I don’t bother with sending the notification to my phone; a beep is enough for me. For long jobs, yeah, getting a notification on my phone is great.

Here’s how you do it for Telegram (after installing telegram-bot from https://github.com/python-telegram-bot/python-telegram-bot):

import telegram # https://github.com/python-telegram-bot/python-telegram-bot

api_token = 'yourtoken'  # API token for telegram bot, you'll need to get a token
chat_id = 'youruser'  # Chat token to specify your user ID; I forget how I found this

# Initialize bot
bot = telegram.Bot(token=api_token)

# Confirm successfully initialized
print(bot.get_me())

# Send message
bot.send_message(chat_id=chat_id, text='Your job finished!')
8 Likes

For a few minutes wait, I just watch progress bar filling up until it shows results - maybe it’s like watching slot machine.

I guess it’s time to stop this addiction with your beeping trick :slight_smile:

1 Like

Neat tricks! Thanks, both of you!

Great approach! My approach was to copy an mp3 of a beep onto the server, and then use IPython.display.Audio to play it in a cell. Your idea is better since it doesn’t require the copy step.

I’ve used flowmatters for sending notifications to my slack channel (my org uses slack) and it’s been great. Not only does it send notifications, it also sends the error outputs so you can see if a job or run has failed.

Works great for long running jobs on remote machines.

This is some sunday night nerdy territory :joy:

My poor mans beep for jupyter notebook (can annoy little kids)

Audio(np.sin(np.pi*100*np.arange(1000*2)/1000),rate=5000, autoplay=True)

6 Likes

Nice and simple.

LOL! This is my favorite so far.

Low tech is the best tech.

This feels like a “Hodor” moment.

2 Likes

You do need an import to make Asif’s technique work.

Bonus: being Python, it doesn’t need to be in its own isolated javascript cell.

from IPython.display import Audio
def beep():
    return Audio(np.sin(np.pi*100*np.arange(1000*2)/1000),rate=5000, autoplay=True)

Much nicer than my technique. Use Asif’s technique, it’s better than the one I suggested!

1 Like

I like https://github.com/ShopRunner/jupyter-notify which is based on browser notifications. the cool feature about it is that you can put a
%autonotify -a 15
at the top of your notebook so any cell lasting more than 15 seconds will trigger a notification automatically. It definitely increased my productivity, even if i get distracted and go to another browser tab or anything else, i come back to my notebook as soon as jupyter is ready.

2 Likes

If you are executing a Python script from the command line, i.e. not in a Jupyter notebook, you can do

print('\a')

That will print the ASCII BEL symbol, i.e. make a beep. It unfortunately doesn’t make a beep when executed inside a Jupyter notebook.

Or in a bash shell or script, type echo ctrl-vctrl-g for a nice retro way of generating BEL.

1 Like