Variable Inspector.ipynb
248 lines
| 7.2 KiB
| text/plain
|
TextLexer
|
r14352 | { | |
"metadata": { | |||
"name": "" | |||
}, | |||
"nbformat": 3, | |||
"nbformat_minor": 0, | |||
"worksheets": [ | |||
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "heading", | |||
"level": 1, | |||
"metadata": {}, | |||
"source": [ | |||
"Build the Variable Inspector" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
"from IPython.html import widgets\n", | |||
"from IPython.display import display\n", | |||
"import re" | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
"prompt_number": 1 | |||
}, | |||
{ | |||
"cell_type": "heading", | |||
"level": 3, | |||
"metadata": {}, | |||
"source": [ | |||
"Create Variable Inspector Controls" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
|
r14413 | "_popout = widgets.ContainerWidget(default_view_name=\"ModalView\")\n", | |
"_popout.description = \"Variable Inspector\"\n", | |||
"_popout.button_text = _popout.description\n", | |||
"_popout.vbox()\n", | |||
|
r14352 | "\n", | |
|
r14413 | "_modal_header_execs_label = widgets.StringWidget(parent=_popout, default_view_name=\"LabelView\")\n", | |
|
r14352 | "_modal_header_execs_label.execs = 0\n", | |
"\n", | |||
|
r14413 | "_modal_body = widgets.ContainerWidget(parent=_popout)\n", | |
"_modal_body.flex1()\n", | |||
"_modal_body.set_css('overflow-y', 'scroll')\n", | |||
|
r14352 | "_modal_body_label = widgets.StringWidget(parent=_modal_body, default_view_name=\"LabelView\")\n", | |
"_modal_body_label.value = 'Not hooked'\n", | |||
"\n", | |||
|
r14413 | "_modal_footer = widgets.ContainerWidget(parent=_popout)\n", | |
|
r14352 | "_var_filter = widgets.SelectionWidget(values=['Public', 'Private', 'IPython'], parent=_modal_footer, value='Public', default_view_name='ToggleButtonsView')\n", | |
"\n", | |||
|
r14413 | "display(_popout)\n", | |
|
r14352 | "\n", | |
"_modal_footer.add_class('modal-footer')\n" | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
|
r14413 | "prompt_number": 2 | |
|
r14352 | }, | |
{ | |||
"cell_type": "heading", | |||
"level": 3, | |||
"metadata": {}, | |||
"source": [ | |||
"Method that Fills the Inspector" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
"_ipython_input = re.compile('_i[0-9]*')\n", | |||
"\n", | |||
"def _fill_inspector():\n", | |||
" \n", | |||
" # Apply filter to variable names.\n", | |||
" names = []\n", | |||
" for name in sorted(_ipython.user_ns):\n", | |||
" \n", | |||
" match = _ipython_input.match(name)\n", | |||
" is_ipython = (match is not None and match.group() == name) or \\\n", | |||
" name == '_dh' or \\\n", | |||
" name == '_ih' or \\\n", | |||
" name == '_ii' or \\\n", | |||
" name == '_iii' or \\\n", | |||
" name == '_oh' or \\\n", | |||
" name == '_sh' or \\\n", | |||
" name == 'get_ipython' or \\\n", | |||
" name == 'In' or \\\n", | |||
" name == 'Out' or \\\n", | |||
" name == 'exit' or \\\n", | |||
" name == 'help' or \\\n", | |||
" name == 'quit' or \\\n", | |||
" name == '_' or \\\n", | |||
" name == '__' or \\\n", | |||
" name == '___'\n", | |||
" \n", | |||
" is_private = name.startswith('_')\n", | |||
" is_public = not is_private\n", | |||
" \n", | |||
" var_filter = _var_filter.value\n", | |||
" if var_filter == 'IPython' and is_ipython:\n", | |||
" names.append(name)\n", | |||
" elif var_filter == 'Private' and (is_private and not is_ipython):\n", | |||
" names.append(name)\n", | |||
" elif var_filter == 'Public' and (is_public and not is_ipython):\n", | |||
" names.append(name)\n", | |||
" \n", | |||
" # Render each name and it's value.\n", | |||
" variable_list_html = \"\"\"\n", | |||
"<table class=\"table table-bordered table-striped\" style=\"width: 100%; overflow: hidden; table-layout:fixed;\">\n", | |||
" <tr><th>Name</th><th>Type</th><th>Value</th>\"\"\"\n", | |||
" for name in names:\n", | |||
" var_value = _ipython.user_ns[name]\n", | |||
" var_type = type(var_value)\n", | |||
" var_small_value = str(var_value)[:100].replace(\"&\", \"&\").replace(\"<\", \"<\")\n", | |||
" \n", | |||
" if str(var_value) != var_small_value:\n", | |||
" var_small_value += '<br><div class=\"label label-info\">...</div>'\n", | |||
" \n", | |||
" row = \"\"\"\n", | |||
"<tr style='overflow: hidden;'>\n", | |||
" <td style='width: 30%; overflow: hidden;'>{name}</td>\n", | |||
" <td style='width: 30%; overflow: hidden;'>{type}</td>\n", | |||
" <td style='width: 40%; overflow: hidden;'>{small_value}</td>\n", | |||
"</tr>\n", | |||
"\"\"\".format(name=name, type=var_type.__name__, small_value=var_small_value, value=str(var_value))\n", | |||
" variable_list_html += row + '\\n'\n", | |||
" variable_list_html += '</table>'\n", | |||
" _modal_body_label.value = variable_list_html\n", | |||
" \n", | |||
" " | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
|
r14413 | "prompt_number": 3 | |
|
r14352 | }, | |
{ | |||
"cell_type": "heading", | |||
"level": 3, | |||
"metadata": {}, | |||
"source": [ | |||
"Hook Cell Execute and Filter Change" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
"_ipython = get_ipython()\n", | |||
"\n", | |||
"try:\n", | |||
" del _ipython._post_execute[handle_cell_executed]\n", | |||
"except:\n", | |||
" pass\n", | |||
"\n", | |||
"def _handle_cell_executed():\n", | |||
" _modal_header_execs_label.execs += 1\n", | |||
" _modal_header_execs_label.value = '%d cell executions captured' % _modal_header_execs_label.execs\n", | |||
" _fill_inspector()\n", | |||
"_ipython.register_post_execute(_handle_cell_executed)\n", | |||
"\n", | |||
"def _handle_filter_changed():\n", | |||
" _fill_inspector()\n", | |||
"_var_filter.on_trait_change(_handle_filter_changed, 'value')" | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
|
r14413 | "prompt_number": 4 | |
|
r14352 | }, | |
{ | |||
"cell_type": "heading", | |||
"level": 1, | |||
"metadata": {}, | |||
"source": [ | |||
"Test" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
"a = 5" | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
|
r14413 | "prompt_number": 5 | |
|
r14352 | }, | |
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
"b = 3.0" | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
|
r14413 | "prompt_number": 6 | |
|
r14352 | }, | |
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
"c = a * b" | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
|
r14413 | "prompt_number": 7 | |
|
r14352 | }, | |
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
"d = \"String\"" | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
|
r14413 | "prompt_number": 8 | |
|
r14352 | }, | |
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
"del b" | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
|
r14413 | "prompt_number": 9 | |
|
r14352 | } | |
], | |||
"metadata": {} | |||
} | |||
] | |||
} |