Variable Inspector.ipynb
240 lines
| 6.2 KiB
| text/plain
|
TextLexer
Jonathan Frederic
|
r14352 | { | ||
Min RK
|
r18669 | "cells": [ | ||
{ | ||||
"cell_type": "markdown", | ||||
"metadata": {}, | ||||
"source": [ | ||||
"# Variable Inspector Widget" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "markdown", | ||||
"metadata": {}, | ||||
"source": [ | ||||
"## A short example implementation" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "markdown", | ||||
"metadata": {}, | ||||
"source": [ | ||||
"This notebook demonstrates how one can use the widgets already built-in to IPython to create a working variable inspector much like the ones seen in popular commercial scientific computing environments." | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"from IPython.html import widgets # Loads the Widget framework.\n", | ||||
"from IPython.core.magics.namespace import NamespaceMagics # Used to query namespace.\n", | ||||
"\n", | ||||
"# For this example, hide these names, just to avoid polluting the namespace further\n", | ||||
"get_ipython().user_ns_hidden['widgets'] = widgets\n", | ||||
"get_ipython().user_ns_hidden['NamespaceMagics'] = NamespaceMagics" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"class VariableInspectorWindow(object):\n", | ||||
" instance = None\n", | ||||
" \n", | ||||
" def __init__(self, ipython):\n", | ||||
" \"\"\"Public constructor.\"\"\"\n", | ||||
" if VariableInspectorWindow.instance is not None:\n", | ||||
" raise Exception(\"\"\"Only one instance of the Variable Inspector can exist at a \n", | ||||
" time. Call close() on the active instance before creating a new instance.\n", | ||||
" If you have lost the handle to the active instance, you can re-obtain it\n", | ||||
" via `VariableInspectorWindow.instance`.\"\"\")\n", | ||||
" \n", | ||||
" VariableInspectorWindow.instance = self\n", | ||||
" self.closed = False\n", | ||||
" self.namespace = NamespaceMagics()\n", | ||||
" self.namespace.shell = ipython.kernel.shell\n", | ||||
" \n", | ||||
Jonathan Frederic
|
r19766 | " self._box = widgets.Box()\n", | ||
" self._box._dom_classes = ['inspector']\n", | ||||
" self._box.background_color = '#fff'\n", | ||||
" self._box.border_color = '#ccc'\n", | ||||
" self._box.border_width = 1\n", | ||||
" self._box.border_radius = 5\n", | ||||
Min RK
|
r18669 | "\n", | ||
" self._modal_body = widgets.VBox()\n", | ||||
" self._modal_body.overflow_y = 'scroll'\n", | ||||
"\n", | ||||
" self._modal_body_label = widgets.HTML(value = 'Not hooked')\n", | ||||
" self._modal_body.children = [self._modal_body_label]\n", | ||||
"\n", | ||||
Jonathan Frederic
|
r19766 | " self._box.children = [\n", | ||
Min RK
|
r18669 | " self._modal_body, \n", | ||
" ]\n", | ||||
" \n", | ||||
" self._ipython = ipython\n", | ||||
" self._ipython.events.register('post_run_cell', self._fill)\n", | ||||
" \n", | ||||
" def close(self):\n", | ||||
" \"\"\"Close and remove hooks.\"\"\"\n", | ||||
" if not self.closed:\n", | ||||
" self._ipython.events.unregister('post_run_cell', self._fill)\n", | ||||
Jonathan Frederic
|
r19766 | " self._box.close()\n", | ||
Min RK
|
r18669 | " self.closed = True\n", | ||
" VariableInspectorWindow.instance = None\n", | ||||
"\n", | ||||
" def _fill(self):\n", | ||||
" \"\"\"Fill self with variable information.\"\"\"\n", | ||||
" values = self.namespace.who_ls()\n", | ||||
" self._modal_body_label.value = '<table class=\"table table-bordered table-striped\"><tr><th>Name</th><th>Type</th><th>Value</th></tr><tr><td>' + \\\n", | ||||
" '</td></tr><tr><td>'.join(['{0}</td><td>{1}</td><td>{2}'.format(v, type(eval(v)).__name__, str(eval(v))) for v in values]) + \\\n", | ||||
" '</td></tr></table>'\n", | ||||
"\n", | ||||
" def _ipython_display_(self):\n", | ||||
" \"\"\"Called when display() or pyout is used to display the Variable \n", | ||||
" Inspector.\"\"\"\n", | ||||
Jonathan Frederic
|
r19766 | " self._box._ipython_display_()\n" | ||
Min RK
|
r18669 | ] | ||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"inspector = VariableInspectorWindow(get_ipython())\n", | ||||
"inspector" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "markdown", | ||||
"metadata": {}, | ||||
"source": [ | ||||
Jonathan Frederic
|
r19766 | "Pop the inspector out of the widget area using Javascript. To close the inspector, click the close button on the widget area that it was spawned from." | ||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"%%javascript\n", | ||||
"$('div.inspector')\n", | ||||
" .detach()\n", | ||||
" .prependTo($('body'))\n", | ||||
" .css({\n", | ||||
" 'z-index': 999, \n", | ||||
" position: 'fixed',\n", | ||||
" 'box-shadow': '5px 5px 12px -3px black',\n", | ||||
" opacity: 0.9\n", | ||||
" })\n", | ||||
" .draggable();" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "markdown", | ||||
"metadata": {}, | ||||
"source": [ | ||||
Min RK
|
r18669 | "# Test" | ||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"a = 5" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"b = 3.0" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"c = a * b" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"d = \"String\"" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"del b" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": null, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"inspector.close()" | ||||
] | ||||
} | ||||
], | ||||
Jonathan Frederic
|
r14352 | "metadata": { | ||
Jonathan Frederic
|
r17726 | "kernelspec": { | ||
Min RK
|
r20278 | "display_name": "Python 3", | ||
"language": "python", | ||||
"name": "python3" | ||||
Jonathan Frederic
|
r19766 | }, | ||
"language_info": { | ||||
Jonathan Frederic
|
r17726 | "codemirror_mode": { | ||
Jonathan Frederic
|
r19766 | "name": "ipython", | ||
Min RK
|
r20278 | "version": 3 | ||
Jonathan Frederic
|
r17726 | }, | ||
Jonathan Frederic
|
r19766 | "file_extension": ".py", | ||
"mimetype": "text/x-python", | ||||
"name": "python", | ||||
"nbconvert_exporter": "python", | ||||
Min RK
|
r20278 | "pygments_lexer": "ipython3", | ||
Jonathan Frederic
|
r20541 | "version": "3.4.0" | ||
Jonathan Frederic
|
r19766 | } | ||
Jonathan Frederic
|
r14352 | }, | ||
Min RK
|
r18669 | "nbformat": 4, | ||
"nbformat_minor": 0 | ||||
Min RK
|
r20278 | } | ||