Skip to content

Instantly share code, notes, and snippets.

@minrk
Created February 19, 2013 03:21
Show Gist options
  • Save minrk/4982809 to your computer and use it in GitHub Desktop.
Save minrk/4982809 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"gist_id": "4982809",
"name": "SharingNotebooks"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Quickly sharing notebooks as gists"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This requires the `jist` ruby gem, or any other gist CLI.\n",
"Get it with:\n",
" \n",
" gem install jist\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, you can simply publish gists by invoking the command-line:\n",
" \n",
" !jist [NotebookName.ipynb]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or, you can install a button on the toolbar with the following code in\n",
"your `custom.js` (courtesy of IRC user epifanio):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%javascript\n",
"IPython.toolbar.add_buttons_group([\n",
"{\n",
" 'label' : 'Share Notebook',\n",
" 'icon' : 'ui-icon-share', \n",
" 'callback': function(){\n",
" IPython.notebook.insert_cell_at_bottom('code');\n",
" var code = 'lines = !jist -p ' + IPython.notebook.notebook_name + '.ipynb' +\n",
" '\\nprint lines[0].replace(\"https://gist.github.com\", \"http://nbviewer.ipython.org\")';\n",
" \n",
" IPython.notebook.get_cell(-1).set_text(code);\n",
" IPython.notebook.get_cell(-1).execute();\n",
" }\n",
"}]);"
],
"language": "python",
"metadata": {},
"outputs": [
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Which, when clicked, publishes the current notebook as a gist,\n",
"and adds a link to the resulting notebook on nbviewer to the end of the notebook."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"lines = !jist -p SharingNotebooks.ipynb\n",
"print lines[0].replace(\"https://gist.github.com\", \"http://nbviewer.ipython.org\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"http://nbviewer.ipython.org/4982809\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"gist extension"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There is also an IPython extension, which does a bit more work.\n",
"You can install it with:\n",
" \n",
" %install_ext https://raw.github.com/minrk/ipython_extensions/master/gist.py\n",
"\n",
"and then when you load it, you will get the button, and a link in the toolbar to the current notebook on nbviewer:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext gist"
],
"language": "python",
"metadata": {},
"outputs": [
{
"javascript": [
"\n",
"\n",
"/*\n",
"Add the contents of this file to your custom.js\n",
"for it to always be on.\n",
"*/\n",
"\n",
"\n",
"IPython.ext_update_gist_link = function(gist_id) {\n",
" \n",
" IPython.notebook.metadata.gist_id = gist_id;\n",
" var toolbar = IPython.toolbar.element;\n",
" var link = toolbar.find(\"a#nbviewer\");\n",
" if ( ! link.length ) {\n",
" link = $('<a id=\"nbviewer\" target=\"_blank\"/>');\n",
" toolbar.append(\n",
" $('<span id=\"nbviewer_span\"/>').append(link)\n",
" );\n",
" }\n",
" \n",
" link.attr(\"href\", \"http://nbviewer.ipython.org/\" + gist_id);\n",
" link.text(\"http://nbviewer.ipython.org/\" + gist_id);\n",
"};\n",
"\n",
"IPython.ext_handle_gist_output = function(output_type, content) {\n",
" if (output_type != 'stream' || content['name'] != 'stdout') {\n",
" return;\n",
" }\n",
" var gist_id = jQuery.trim(content['data']);\n",
" if (! gist_id.match(/[A-Za-z0-9]+/g)) {\n",
" alert(\"Gist seems to have failed: \" + gist_id);\n",
" return;\n",
" }\n",
" IPython.ext_update_gist_link(gist_id);\n",
"};\n",
"\n",
"IPython.ext_gist_notebook = function () {\n",
" var gist_id = IPython.notebook.metadata.gist_id || null;\n",
" var cmd = '_nbname = \"' + IPython.notebook.notebook_name + '.ipynb\"';\n",
" cmd = cmd + '\\nlines = !jist -p'\n",
" if (gist_id) {\n",
" cmd = cmd + ' -u ' + gist_id;\n",
" }\n",
" cmd = cmd + ' $_nbname';\n",
" cmd = cmd + '\\nprint lines[0].replace(\"https://gist.github.com\", \"\").replace(\"/\",\"\")';\n",
" IPython.notebook.kernel.execute(cmd, {'output' : IPython.ext_handle_gist_output});\n",
"};\n",
"\n",
"setTimeout(function() {\n",
" if ($(\"#gist_notebook\").length == 0) {\n",
" IPython.toolbar.add_buttons_group([\n",
" {\n",
" 'label' : 'Share Notebook as gist',\n",
" 'icon' : 'ui-icon-share',\n",
" 'callback': IPython.ext_gist_notebook,\n",
" 'id' : 'gist_notebook'\n",
" },\n",
" ])\n",
" }\n",
"\n",
" if (IPython.notebook.metadata.gist_id) {\n",
" IPython.ext_update_gist_link(IPython.notebook.metadata.gist_id);\n",
" }\n",
"}, 1000);\n",
"\n"
],
"output_type": "display_data",
"text": [
"<IPython.core.display.Javascript at 0x107594f50>"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One thing the extension does that the above samples do not is store the gist id\n",
"in the notebook metadata. So each time you publish a notebook,\n",
"it will keep updating the same gist, rather than creating a new one.\n",
"\n",
"You can also get this same functionality (minus the `%gist` magic)\n",
"without the extension, simply by adding the contents of [gist.js](https://github.com/minrk/ipython_extensions/blob/master/gist.js)\n",
"to your custom.js. If you are feeling super lazy, you can actually add it to your current profile\n",
"from within a notebook, with:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"!mkdir -p $(ipython locate profile {get_ipython().profile})/static/js\n",
"!curl https://raw.github.com/minrk/ipython_extensions/master/gist.js >> $(ipython locate profile {get_ipython().profile})/static/js/custom.js"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" % Total % Received % Xferd Average Speed Time Time Time Current\r\n",
" Dload Upload Total Spent Left Speed\r\n",
"\r",
" 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\r",
" 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\r",
"100 1933 100 1933 0 0 3418 0 --:--:-- --:--:-- --:--:-- 8746\r\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"!cat $(ipython locate profile {get_ipython().profile})/static/js/custom.js"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"/*\r\n",
"Add the contents of this file to your custom.js\r\n",
"for it to always be on.\r\n",
"*/\r\n",
"\r\n",
"\r\n",
"IPython.ext_update_gist_link = function(gist_id) {\r\n",
" \r\n",
" IPython.notebook.metadata.gist_id = gist_id;\r\n",
" var toolbar = IPython.toolbar.element;\r\n",
" var link = toolbar.find(\"a#nbviewer\");\r\n",
" if ( ! link.length ) {\r\n",
" link = $('<a id=\"nbviewer\" target=\"_blank\"/>');\r\n",
" toolbar.append(\r\n",
" $('<span id=\"nbviewer_span\"/>').append(link)\r\n",
" );\r\n",
" }\r\n",
" \r\n",
" link.attr(\"href\", \"http://nbviewer.ipython.org/\" + gist_id);\r\n",
" link.text(\"http://nbviewer.ipython.org/\" + gist_id);\r\n",
"};\r\n",
"\r\n",
"IPython.ext_handle_gist_output = function(output_type, content) {\r\n",
" if (output_type != 'stream' || content['name'] != 'stdout') {\r\n",
" return;\r\n",
" }\r\n",
" var gist_id = jQuery.trim(content['data']);\r\n",
" if (! gist_id.match(/[A-Za-z0-9]+/g)) {\r\n",
" alert(\"Gist seems to have failed: \" + gist_id);\r\n",
" return;\r\n",
" }\r\n",
" IPython.ext_update_gist_link(gist_id);\r\n",
"};\r\n",
"\r\n",
"IPython.ext_gist_notebook = function () {\r\n",
" var gist_id = IPython.notebook.metadata.gist_id || null;\r\n",
" var cmd = '_nbname = \"' + IPython.notebook.notebook_name + '.ipynb\"';\r\n",
" cmd = cmd + '\\nlines = !jist -p'\r\n",
" if (gist_id) {\r\n",
" cmd = cmd + ' -u ' + gist_id;\r\n",
" }\r\n",
" cmd = cmd + ' $_nbname';\r\n",
" cmd = cmd + '\\nprint lines[0].replace(\"https://gist.github.com\", \"\").replace(\"/\",\"\")';\r\n",
" IPython.notebook.kernel.execute(cmd, {'output' : IPython.ext_handle_gist_output});\r\n",
"};\r\n",
"\r\n",
"setTimeout(function() {\r\n",
" if ($(\"#gist_notebook\").length == 0) {\r\n",
" IPython.toolbar.add_buttons_group([\r\n",
" {\r\n",
" 'label' : 'Share Notebook as gist',\r\n",
" 'icon' : 'ui-icon-share',\r\n",
" 'callback': IPython.ext_gist_notebook,\r\n",
" 'id' : 'gist_notebook'\r\n",
" },\r\n",
" ])\r\n",
" }\r\n",
"\r\n",
" if (IPython.notebook.metadata.gist_id) {\r\n",
" IPython.ext_update_gist_link(IPython.notebook.metadata.gist_id);\r\n",
" }\r\n",
"}, 1000);\r\n",
"\r\n"
]
}
],
"prompt_number": 15
}
],
"metadata": {}
}
]
}
@Carreau
Copy link

Carreau commented Feb 19, 2013

I would probably use $.getScript('/static/.js') to load the extension as a separate file (then you just have a bunch of getScript in your custom.js) and listen for the 'notebook.loaded' instead of a timeout.

And this
IPython.notebook.get_cell(-1).set_text(code);
IPython.notebook.get_cell(-1).execute();

can probably directly execute on the kernel without going though a cell.

@slink
Copy link

slink commented Jun 5, 2014

@nicktimko
Copy link

The link GH provides when viewing gist.py (as nice HTML) is https://github.com/minrk/ipython_extensions/raw/master/extensions/gist.py which redirects to the raw.githubusercontent.com domain. Maybe the former is slightly less fragile?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment