Variable Inspector.ipynb
192 lines
| 5.7 KiB
| text/plain
|
TextLexer
Jonathan Frederic
|
r14352 | { | |
"metadata": { | |||
Jonathan Frederic
|
r15326 | "name": "", | |
"signature": "sha256:7b872a09743460a3ce0358e9010c3b4baf5136b79a2a075964dea2c2fd85c82e" | |||
Jonathan Frederic
|
r14352 | }, | |
"nbformat": 3, | |||
"nbformat_minor": 0, | |||
"worksheets": [ | |||
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "heading", | |||
"level": 1, | |||
"metadata": {}, | |||
"source": [ | |||
Jonathan Frederic
|
r14753 | "Variable Inspector Widget" | |
Jonathan Frederic
|
r14352 | ] | |
}, | |||
{ | |||
Jonathan Frederic
|
r14753 | "cell_type": "heading", | |
"level": 2, | |||
Jonathan Frederic
|
r14352 | "metadata": {}, | |
Jonathan Frederic
|
r14753 | "source": [ | |
MinRK
|
r14798 | "A short example implementation" | |
Jonathan Frederic
|
r14753 | ] | |
Jonathan Frederic
|
r14352 | }, | |
{ | |||
Jonathan Frederic
|
r14753 | "cell_type": "markdown", | |
Jonathan Frederic
|
r14352 | "metadata": {}, | |
"source": [ | |||
Jonathan Frederic
|
r14753 | "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." | |
Jonathan Frederic
|
r14352 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
MinRK
|
r14798 | "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" | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
"prompt_number": 1 | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r14753 | "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", | |||
MinRK
|
r14798 | " self.namespace = NamespaceMagics()\n", | |
Jonathan Frederic
|
r14753 | " self.namespace.shell = ipython.kernel.shell\n", | |
" \n", | |||
MinRK
|
r14798 | " self._popout = widgets.PopupWidget()\n", | |
Jonathan Frederic
|
r14753 | " self._popout.description = \"Variable Inspector\"\n", | |
" self._popout.button_text = self._popout.description\n", | |||
Jonathan Frederic
|
r14352 | "\n", | |
MinRK
|
r14798 | " self._modal_body = widgets.ContainerWidget()\n", | |
Jonathan Frederic
|
r14753 | " self._modal_body.set_css('overflow-y', 'scroll')\n", | |
Jonathan Frederic
|
r14352 | "\n", | |
MinRK
|
r14798 | " self._modal_body_label = widgets.HTMLWidget(value = 'Not hooked')\n", | |
Jonathan Frederic
|
r14753 | " self._modal_body.children = [self._modal_body_label]\n", | |
Jonathan Frederic
|
r14352 | "\n", | |
Jonathan Frederic
|
r14753 | " self._popout.children = [\n", | |
" self._modal_body, \n", | |||
" ]\n", | |||
" \n", | |||
" self._ipython = ipython\n", | |||
" self._ipython.register_post_execute(self._fill)\n", | |||
"\n", | |||
" def close(self):\n", | |||
" \"\"\"Close and remove hooks.\"\"\"\n", | |||
" if not self.closed:\n", | |||
" del self._ipython._post_execute[self._fill]\n", | |||
" self._popout.close()\n", | |||
" 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", | |||
Jonathan Frederic
|
r15326 | " 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", | |||
Jonathan Frederic
|
r14352 | "\n", | |
Jonathan Frederic
|
r14753 | " def _ipython_display_(self):\n", | |
" \"\"\"Called when display() or pyout is used to display the Variable \n", | |||
" Inspector.\"\"\"\n", | |||
" self._popout._ipython_display_()\n", | |||
" self._popout.add_class('vbox')\n", | |||
" self._modal_body.add_class('box-flex1')\n" | |||
Jonathan Frederic
|
r14352 | ], | |
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
MinRK
|
r14798 | "prompt_number": 2 | |
Jonathan Frederic
|
r14352 | }, | |
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r14753 | "inspector = VariableInspectorWindow(get_ipython())\n", | |
"inspector" | |||
Jonathan Frederic
|
r14352 | ], | |
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
MinRK
|
r14798 | "prompt_number": 3 | |
Jonathan Frederic
|
r14352 | }, | |
{ | |||
"cell_type": "heading", | |||
Jonathan Frederic
|
r14753 | "level": 1, | |
Jonathan Frederic
|
r14352 | "metadata": {}, | |
"source": [ | |||
Jonathan Frederic
|
r14753 | "Test" | |
Jonathan Frederic
|
r14352 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r14753 | "a = 5" | |
Jonathan Frederic
|
r14352 | ], | |
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
MinRK
|
r14798 | "prompt_number": 4 | |
Jonathan Frederic
|
r14352 | }, | |
{ | |||
Jonathan Frederic
|
r14753 | "cell_type": "code", | |
"collapsed": false, | |||
"input": [ | |||
"b = 3.0" | |||
], | |||
"language": "python", | |||
Jonathan Frederic
|
r14352 | "metadata": {}, | |
Jonathan Frederic
|
r14753 | "outputs": [], | |
MinRK
|
r14798 | "prompt_number": 5 | |
Jonathan Frederic
|
r14352 | }, | |
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r14753 | "c = a * b" | |
Jonathan Frederic
|
r14352 | ], | |
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
MinRK
|
r14798 | "prompt_number": 6 | |
Jonathan Frederic
|
r14352 | }, | |
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r14753 | "d = \"String\"" | |
Jonathan Frederic
|
r14352 | ], | |
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
MinRK
|
r14798 | "prompt_number": 7 | |
Jonathan Frederic
|
r14352 | }, | |
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r14753 | "del b" | |
Jonathan Frederic
|
r14352 | ], | |
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
MinRK
|
r14798 | "prompt_number": 8 | |
Jonathan Frederic
|
r14352 | } | |
], | |||
"metadata": {} | |||
} | |||
] | |||
} |