{
"metadata": {
"name": ""
},
"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": [],
"prompt_number": 1
},
{
"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.PopupWidget()\n",
" self._popout.description = \"Variable Inspector\"\n",
" self._popout.button_text = self._popout.description\n",
"\n",
" self._modal_body = widgets.ContainerWidget()\n",
" self._modal_body.set_css('overflow-y', 'scroll')\n",
"\n",
" self._modal_body_label = widgets.HTMLWidget(value = 'Not hooked')\n",
" self._modal_body.children = [self._modal_body_label]\n",
"\n",
" self._modal_footer = widgets.ContainerWidget()\n",
" self._var_filter = widgets.ToggleButtonsWidget(description=\"Method:\", values=['List', 'Details'], value='List')\n",
" self._modal_footer.children = [self._var_filter]\n",
"\n",
" self._popout.children = [\n",
" self._modal_body, \n",
" self._modal_footer,\n",
" ]\n",
" \n",
" self._ipython = ipython\n",
" self._ipython.register_post_execute(self._fill)\n",
" self._var_filter.on_trait_change(self._fill, 'value')\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",
" mode = self._var_filter.value\n",
" if mode == \"List\":\n",
" self._modal_body_label.value = '
'.join(values)\n",
" elif mode == \"Details\":\n",
" self._modal_body_label.value = '
Name | Type | Value |
---|---|---|
' + \\\n", " ' | ||
'.join(['{0} | {1} | {2}'.format(v, type(eval(v)).__name__, str(eval(v))) for v in values]) + \\\n", " ' |