From baddbb71a4f2ab7e0b1e2d018078b875b51cb513 2014-01-16 10:57:00 From: Jonathan Frederic Date: 2014-01-16 10:57:00 Subject: [PATCH] Added Variable Inspector example --- diff --git a/examples/widgets/Variable Inspector.ipynb b/examples/widgets/Variable Inspector.ipynb new file mode 100644 index 0000000..fa0dfe6 --- /dev/null +++ b/examples/widgets/Variable Inspector.ipynb @@ -0,0 +1,354 @@ +{ + "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": [ + "Custom SidePanel View" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%%javascript\n", + "\n", + "require([\"notebook/js/widget\"], function(){\n", + "\n", + " // Define the FilePickerView\n", + " var SidePanelView = IPython.WidgetView.extend({\n", + " \n", + " render: function(){ \n", + " var table_div = $('
', {id: 'var_inspect'})\n", + " .addClass('hbox');\n", + " var body_div = $('
')\n", + " .css('width', '80%')\n", + " .css('height', '100%')\n", + " .appendTo(table_div);\n", + " this.panel_div = $('
')\n", + " .css('width', '20%')\n", + " .css('height', '100%')\n", + " .appendTo(table_div);\n", + " \n", + " var body = $('body');\n", + " var site = body.find('#site');\n", + " site.detach();\n", + " body.find('#var_inspect').remove();\n", + " body.append(table_div);\n", + " site.appendTo(body_div);\n", + " },\n", + "\n", + " display_child: function(view) {\n", + " this.panel_div.append(view.$el);\n", + " },\n", + " });\n", + " \n", + " // Register the DatePickerView with the widget manager.\n", + " IPython.widget_manager.register_widget_view('SidePanelView', SidePanelView);\n", + "});" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "javascript": [ + "\n", + "require([\"notebook/js/widget\"], function(){\n", + "\n", + " // Define the FilePickerView\n", + " var SidePanelView = IPython.WidgetView.extend({\n", + " \n", + " render: function(){ \n", + " var table_div = $('
', {id: 'var_inspect'})\n", + " .addClass('hbox');\n", + " var body_div = $('
')\n", + " .css('width', '80%')\n", + " .css('height', '100%')\n", + " .appendTo(table_div);\n", + " this.panel_div = $('
')\n", + " .css('width', '20%')\n", + " .css('height', '100%')\n", + " .appendTo(table_div);\n", + " \n", + " var body = $('body');\n", + " var site = body.find('#site');\n", + " site.detach();\n", + " body.find('#var_inspect').remove();\n", + " body.append(table_div);\n", + " site.appendTo(body_div);\n", + " },\n", + "\n", + " display_child: function(view) {\n", + " this.panel_div.append(view.$el);\n", + " },\n", + " });\n", + " \n", + " // Register the DatePickerView with the widget manager.\n", + " IPython.widget_manager.register_widget_view('SidePanelView', SidePanelView);\n", + "});" + ], + "metadata": {}, + "output_type": "display_data", + "text": [ + "" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Create Variable Inspector Controls" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "_side_panel = widgets.ContainerWidget(default_view_name=\"SidePanelView\")\n", + "\n", + "_modal_div = widgets.ContainerWidget(parent=_side_panel)\n", + "_modal_div.set_css({'padding-top': '60px',\n", + " 'padding-right': '40px',\n", + " 'padding-left': '10px',})\n", + "\n", + "_modal_header = widgets.ContainerWidget(parent=_modal_div)\n", + "_modal_header_label = widgets.StringWidget(parent=_modal_header, default_view_name=\"LabelView\")\n", + "_modal_header_label.value = '

Variable Inspector

'\n", + "_modal_header_execs_label = widgets.StringWidget(parent=_modal_header, default_view_name=\"LabelView\")\n", + "_modal_header_execs_label.execs = 0\n", + "\n", + "_modal_body = widgets.ContainerWidget(parent=_modal_div)\n", + "_modal_body.vbox()\n", + "\n", + "_modal_body_label = widgets.StringWidget(parent=_modal_body, default_view_name=\"LabelView\")\n", + "_modal_body_label.value = 'Not hooked'\n", + "\n", + "_modal_footer = widgets.ContainerWidget(parent=_modal_div)\n", + "_modal_footer.vbox()\n", + "_var_filter = widgets.SelectionWidget(values=['Public', 'Private', 'IPython'], parent=_modal_footer, value='Public', default_view_name='ToggleButtonsView')\n", + "\n", + "display(_side_panel)\n", + "\n", + "_modal_header.add_class('modal-header')\n", + "_modal_body.add_class('modal-body')\n", + "_modal_footer.add_class('modal-footer')\n" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 3 + }, + { + "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", + "\n", + " \"\"\"\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 += '
...
'\n", + " \n", + " row = \"\"\"\n", + "\n", + " \n", + " \n", + " \n", + "\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 += '
NameTypeValue
{name}{type}{small_value}
'\n", + " _modal_body_label.value = variable_list_html\n", + " \n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 4 + }, + { + "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": [], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Test" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a = 5" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "b = 3.0" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "c = a * b" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "d = \"String\"" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "del b" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 10 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file