Skip to content

Instantly share code, notes, and snippets.

@lmarti
Last active September 23, 2015 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmarti/74fca8c115b092d5920b to your computer and use it in GitHub Desktop.
Save lmarti/74fca8c115b092d5920b to your computer and use it in GitHub Desktop.
IPython magic to get modal notification in a notebook.
{
"metadata": {
"name": "",
"signature": "sha256:904cc20b78d0ec5dade39bcf72cb7dd3043f9da6992e6ba87d88dfa5ed2165b4"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"An IPython magic for modal notifications"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I have been doing some hard-core processing using IPython Notebook. Because of that, I needed to check from time to time if it was finished or not. This led me to think that it would be nice to have a 'big and loud' on-screen notification.\n",
"\n",
"I remembered that IPython Notebook 2.0 uses [Bootstrap](http://getboostrap.com/) and decided to make a code a line *magic* show a model windows with a message of my choice. This would make a great nbextension... but I will leave this for my next afternoon hack."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.html import widgets\n",
"from IPython.display import display\n",
"import string\n",
"import random"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Creating the notification function"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def do_notify(line):\n",
" suffix = ''.join(random.choice(string.digits) for _ in range(6)) # a suffix to avoid clashes\n",
" modal = widgets.HTMLWidget(value =\n",
" '''\n",
" <div class=\"modal fade\" id=\"notif_modal_'''+suffix+'''\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"myModalLabel\" aria-hidden=\"false\">\n",
" <div class=\"modal-dialog\">\n",
" <div class=\"modal-content\">\n",
" <div class=\"modal-header\">\n",
" <button type=\"button\" class=\"close\" data-dismiss=\"modal\"><span aria-hidden=\"false\">&times;</span></button>\n",
" <h4 class=\"modal-title\" id=\"myModalLabel\">Attention!</h4>\n",
" </div>\n",
" <div class=\"modal-body\">'''\n",
" +line+\n",
" '''</div>\n",
" <div class=\"modal-footer\">\n",
" <button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Close</button>\n",
" </div>\n",
" </div>\n",
" </div>\n",
" </div>\n",
" <script type=\"text/javascript\">\n",
" $(\"#notif_modal_'''+suffix+'''\").modal('show');\n",
" </script>\n",
" ''')\n",
" display(modal)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Registering the functionas a line magic"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ip = get_ipython()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ip.register_magic_function(do_notify, 'line', 'notify')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Testing the magic"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import time"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('Philosopher sleeping...')\n",
"time.sleep(5)\n",
"print('Up and hungry...')\n",
"%notify Dinner is ready!"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Philosopher sleeping...\n",
"Up and hungry..."
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"time.sleep(5)\n",
"%notify Let's have a coffee"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment