Last active
January 19, 2019 13:54
-
-
Save eriknw/49a18cab776bbbf4567000c2b591bf41 to your computer and use it in GitHub Desktop.
Jupyter Notebook "goto" demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"tags": [ | |
"top" | |
] | |
}, | |
"outputs": [], | |
"source": [ | |
"goto('bottom', time=14)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
}, | |
"outputs": [], | |
"source": [ | |
"import IPython\n", | |
"\n", | |
"# BTW, this is a quick hack to see if the user is running in IPython\n", | |
"import ipykernel\n", | |
"try:\n", | |
" ipykernel.get_connection_info()\n", | |
" RUNNING_KERNEL = True\n", | |
"except Exception:\n", | |
" RUNNING_KERNEL = False\n", | |
"\n", | |
"_IP = None # global interactive shell\n", | |
"\n", | |
"\n", | |
"class GotoException(Exception):\n", | |
" def __init__(self, *args, **kwargs):\n", | |
" self.args = args\n", | |
" self.kwargs = kwargs\n", | |
"\n", | |
"\n", | |
"def _goto_exception_handler(ip, etype, value, tb, tb_offset=None):\n", | |
" _goto_cell(*value.args, **value.kwargs)\n", | |
"\n", | |
"\n", | |
"def goto(*tags, scroll=True, then=None, time=0.5):\n", | |
" global _IP\n", | |
" if _IP is None:\n", | |
" _IP = IPython.core.interactiveshell.InteractiveShell.instance()\n", | |
" _IP.set_custom_exc((GotoException, ), _goto_exception_handler)\n", | |
" raise GotoException(*tags, scroll=scroll, then=then, time=time)\n", | |
"\n", | |
"\n", | |
"def _goto_cell(*tags, next_cell=False, scroll=True, then=None, time=None):\n", | |
" if len(tags) == 1 and isinstance(tags[0], (list, tuple)):\n", | |
" tags = tags[0]\n", | |
" if not tags:\n", | |
" return\n", | |
" if time is None:\n", | |
" time = 0.5\n", | |
" time = int(1000 * time)\n", | |
" filter_text = ' && '.join(\n", | |
" \"cell.metadata['tags'] !== undefined && cell.metadata['tags'].indexOf('%s') > -1\"\n", | |
" % k for k in tags)\n", | |
" _IP.run_cell_magic('javascript', '', \"\"\"\n", | |
" (function(){\n", | |
" var matching_cells = IPython.notebook.get_cells().filter(\n", | |
" function (cell) {return %s;}\n", | |
" );\n", | |
" if (matching_cells.length > 0){\n", | |
" var first_cell = matching_cells[0];\n", | |
" var first_cell_index = IPython.notebook.find_cell_index(first_cell);\n", | |
" var next_cell = first_cell;\n", | |
" if (%s){\n", | |
" next_cell = IPython.notebook.get_next_cell(next_cell);\n", | |
" };\n", | |
" if (%s){\n", | |
" IPython.notebook.scroll_to_cell(first_cell_index, %s);\n", | |
" };\n", | |
" next_cell.focus_cell();\n", | |
" };\n", | |
" })();\n", | |
" \"\"\" % (filter_text, str(bool(next_cell)).lower(),\n", | |
" str(bool(scroll)).lower(), time))\n", | |
" if then is not None:\n", | |
" tags = then.get('tags', [])\n", | |
" scroll = then.get('scroll', True)\n", | |
" next_cell = then.get('next_cell', False)\n", | |
" time = then.get('time')\n", | |
" then = then.get('then')\n", | |
" _goto_cell(tags, scroll=scroll, next_cell=next_cell, then=then, time=time)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"goto('two')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
}, | |
"outputs": [], | |
"source": [ | |
"goto('one', time=2, then={'tags': 'two', 'time': 4, 'then': {'tags': 'three', 'time': 6}})" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"tags": [ | |
"two" | |
] | |
}, | |
"outputs": [], | |
"source": [ | |
"goto('bottom')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"tags": [ | |
"x" | |
] | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"tags": [ | |
"x", | |
"y" | |
] | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
}, | |
"outputs": [], | |
"source": [ | |
"it = iter(range(5))\n", | |
"val = next(it)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"tags": [ | |
"loop_begin" | |
] | |
}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"#do stuff" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
}, | |
"outputs": [], | |
"source": [ | |
"# dmore more stuff\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"val" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
}, | |
"outputs": [], | |
"source": [ | |
"try:\n", | |
" val = next(it)\n", | |
"except Exception:\n", | |
" goto('loopend')\n", | |
"else:\n", | |
" goto('loop_begin')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"tags": [ | |
"loopend" | |
] | |
}, | |
"outputs": [], | |
"source": [ | |
"val" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"tags": [ | |
"three" | |
] | |
}, | |
"outputs": [], | |
"source": [ | |
"goto('x', 'y')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"tags": [ | |
"one" | |
] | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"tags": [ | |
"bottom" | |
] | |
}, | |
"outputs": [], | |
"source": [ | |
"goto('top', time=5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"celltoolbar": "Tags", | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (c) 2018 Erik Welch | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
a. Redistributions of source code must retain the above copyright notice, | |
this list of conditions and the following disclaimer. | |
b. Redistributions in binary form must reproduce the above copyright | |
notice, this list of conditions and the following disclaimer in the | |
documentation and/or other materials provided with the distribution. | |
c. Neither the name of goto nor the names of its contributors | |
may be used to endorse or promote products derived from this software | |
without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR | |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | |
DAMAGE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment