You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
202 lines
5.7 KiB
202 lines
5.7 KiB
{
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"codemirror_mode": {
|
|
"name": "python",
|
|
"version": 2
|
|
},
|
|
"display_name": "Python 2",
|
|
"language": "python",
|
|
"name": "python2"
|
|
},
|
|
"name": "",
|
|
"signature": "sha256:474731659fb14b86672d1dafb2b497fa280082ab40a8a82fe2cde1b6d9b88a6e"
|
|
},
|
|
"nbformat": 3,
|
|
"nbformat_minor": 0,
|
|
"worksheets": [
|
|
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "heading",
|
|
"level": 1,
|
|
"metadata": {},
|
|
"source": [
|
|
"Variable Inspector Widget"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "heading",
|
|
"level": 2,
|
|
"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",
|
|
"collapsed": false,
|
|
"input": [
|
|
"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": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"collapsed": false,
|
|
"input": [
|
|
"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",
|
|
" self._popout = widgets.Popup()\n",
|
|
" self._popout.description = \"Variable Inspector\"\n",
|
|
" self._popout.button_text = self._popout.description\n",
|
|
"\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",
|
|
" self._popout.children = [\n",
|
|
" 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",
|
|
" 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",
|
|
" 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",
|
|
" self._popout._ipython_display_()\n"
|
|
],
|
|
"language": "python",
|
|
"metadata": {},
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"collapsed": false,
|
|
"input": [
|
|
"inspector = VariableInspectorWindow(get_ipython())\n",
|
|
"inspector"
|
|
],
|
|
"language": "python",
|
|
"metadata": {},
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "heading",
|
|
"level": 1,
|
|
"metadata": {},
|
|
"source": [
|
|
"Test"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"collapsed": false,
|
|
"input": [
|
|
"a = 5"
|
|
],
|
|
"language": "python",
|
|
"metadata": {},
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"collapsed": false,
|
|
"input": [
|
|
"b = 3.0"
|
|
],
|
|
"language": "python",
|
|
"metadata": {},
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"collapsed": false,
|
|
"input": [
|
|
"c = a * b"
|
|
],
|
|
"language": "python",
|
|
"metadata": {},
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"collapsed": false,
|
|
"input": [
|
|
"d = \"String\""
|
|
],
|
|
"language": "python",
|
|
"metadata": {},
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"collapsed": false,
|
|
"input": [
|
|
"del b"
|
|
],
|
|
"language": "python",
|
|
"metadata": {},
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"collapsed": false,
|
|
"input": [
|
|
"inspector.close()"
|
|
],
|
|
"language": "python",
|
|
"metadata": {},
|
|
"outputs": []
|
|
}
|
|
],
|
|
"metadata": {}
|
|
}
|
|
]
|
|
} |