Show More
@@ -0,0 +1,354 b'' | |||||
|
1 | { | |||
|
2 | "metadata": { | |||
|
3 | "name": "" | |||
|
4 | }, | |||
|
5 | "nbformat": 3, | |||
|
6 | "nbformat_minor": 0, | |||
|
7 | "worksheets": [ | |||
|
8 | { | |||
|
9 | "cells": [ | |||
|
10 | { | |||
|
11 | "cell_type": "heading", | |||
|
12 | "level": 1, | |||
|
13 | "metadata": {}, | |||
|
14 | "source": [ | |||
|
15 | "Build the Variable Inspector" | |||
|
16 | ] | |||
|
17 | }, | |||
|
18 | { | |||
|
19 | "cell_type": "code", | |||
|
20 | "collapsed": false, | |||
|
21 | "input": [ | |||
|
22 | "from IPython.html import widgets\n", | |||
|
23 | "from IPython.display import display\n", | |||
|
24 | "import re" | |||
|
25 | ], | |||
|
26 | "language": "python", | |||
|
27 | "metadata": {}, | |||
|
28 | "outputs": [], | |||
|
29 | "prompt_number": 1 | |||
|
30 | }, | |||
|
31 | { | |||
|
32 | "cell_type": "heading", | |||
|
33 | "level": 3, | |||
|
34 | "metadata": {}, | |||
|
35 | "source": [ | |||
|
36 | "Custom SidePanel View" | |||
|
37 | ] | |||
|
38 | }, | |||
|
39 | { | |||
|
40 | "cell_type": "code", | |||
|
41 | "collapsed": false, | |||
|
42 | "input": [ | |||
|
43 | "%%javascript\n", | |||
|
44 | "\n", | |||
|
45 | "require([\"notebook/js/widget\"], function(){\n", | |||
|
46 | "\n", | |||
|
47 | " // Define the FilePickerView\n", | |||
|
48 | " var SidePanelView = IPython.WidgetView.extend({\n", | |||
|
49 | " \n", | |||
|
50 | " render: function(){ \n", | |||
|
51 | " var table_div = $('<div />', {id: 'var_inspect'})\n", | |||
|
52 | " .addClass('hbox');\n", | |||
|
53 | " var body_div = $('<div />')\n", | |||
|
54 | " .css('width', '80%')\n", | |||
|
55 | " .css('height', '100%')\n", | |||
|
56 | " .appendTo(table_div);\n", | |||
|
57 | " this.panel_div = $('<div />')\n", | |||
|
58 | " .css('width', '20%')\n", | |||
|
59 | " .css('height', '100%')\n", | |||
|
60 | " .appendTo(table_div);\n", | |||
|
61 | " \n", | |||
|
62 | " var body = $('body');\n", | |||
|
63 | " var site = body.find('#site');\n", | |||
|
64 | " site.detach();\n", | |||
|
65 | " body.find('#var_inspect').remove();\n", | |||
|
66 | " body.append(table_div);\n", | |||
|
67 | " site.appendTo(body_div);\n", | |||
|
68 | " },\n", | |||
|
69 | "\n", | |||
|
70 | " display_child: function(view) {\n", | |||
|
71 | " this.panel_div.append(view.$el);\n", | |||
|
72 | " },\n", | |||
|
73 | " });\n", | |||
|
74 | " \n", | |||
|
75 | " // Register the DatePickerView with the widget manager.\n", | |||
|
76 | " IPython.widget_manager.register_widget_view('SidePanelView', SidePanelView);\n", | |||
|
77 | "});" | |||
|
78 | ], | |||
|
79 | "language": "python", | |||
|
80 | "metadata": {}, | |||
|
81 | "outputs": [ | |||
|
82 | { | |||
|
83 | "javascript": [ | |||
|
84 | "\n", | |||
|
85 | "require([\"notebook/js/widget\"], function(){\n", | |||
|
86 | "\n", | |||
|
87 | " // Define the FilePickerView\n", | |||
|
88 | " var SidePanelView = IPython.WidgetView.extend({\n", | |||
|
89 | " \n", | |||
|
90 | " render: function(){ \n", | |||
|
91 | " var table_div = $('<div />', {id: 'var_inspect'})\n", | |||
|
92 | " .addClass('hbox');\n", | |||
|
93 | " var body_div = $('<div />')\n", | |||
|
94 | " .css('width', '80%')\n", | |||
|
95 | " .css('height', '100%')\n", | |||
|
96 | " .appendTo(table_div);\n", | |||
|
97 | " this.panel_div = $('<div />')\n", | |||
|
98 | " .css('width', '20%')\n", | |||
|
99 | " .css('height', '100%')\n", | |||
|
100 | " .appendTo(table_div);\n", | |||
|
101 | " \n", | |||
|
102 | " var body = $('body');\n", | |||
|
103 | " var site = body.find('#site');\n", | |||
|
104 | " site.detach();\n", | |||
|
105 | " body.find('#var_inspect').remove();\n", | |||
|
106 | " body.append(table_div);\n", | |||
|
107 | " site.appendTo(body_div);\n", | |||
|
108 | " },\n", | |||
|
109 | "\n", | |||
|
110 | " display_child: function(view) {\n", | |||
|
111 | " this.panel_div.append(view.$el);\n", | |||
|
112 | " },\n", | |||
|
113 | " });\n", | |||
|
114 | " \n", | |||
|
115 | " // Register the DatePickerView with the widget manager.\n", | |||
|
116 | " IPython.widget_manager.register_widget_view('SidePanelView', SidePanelView);\n", | |||
|
117 | "});" | |||
|
118 | ], | |||
|
119 | "metadata": {}, | |||
|
120 | "output_type": "display_data", | |||
|
121 | "text": [ | |||
|
122 | "<IPython.core.display.Javascript at 0x7f4e70015050>" | |||
|
123 | ] | |||
|
124 | } | |||
|
125 | ], | |||
|
126 | "prompt_number": 2 | |||
|
127 | }, | |||
|
128 | { | |||
|
129 | "cell_type": "heading", | |||
|
130 | "level": 3, | |||
|
131 | "metadata": {}, | |||
|
132 | "source": [ | |||
|
133 | "Create Variable Inspector Controls" | |||
|
134 | ] | |||
|
135 | }, | |||
|
136 | { | |||
|
137 | "cell_type": "code", | |||
|
138 | "collapsed": false, | |||
|
139 | "input": [ | |||
|
140 | "_side_panel = widgets.ContainerWidget(default_view_name=\"SidePanelView\")\n", | |||
|
141 | "\n", | |||
|
142 | "_modal_div = widgets.ContainerWidget(parent=_side_panel)\n", | |||
|
143 | "_modal_div.set_css({'padding-top': '60px',\n", | |||
|
144 | " 'padding-right': '40px',\n", | |||
|
145 | " 'padding-left': '10px',})\n", | |||
|
146 | "\n", | |||
|
147 | "_modal_header = widgets.ContainerWidget(parent=_modal_div)\n", | |||
|
148 | "_modal_header_label = widgets.StringWidget(parent=_modal_header, default_view_name=\"LabelView\")\n", | |||
|
149 | "_modal_header_label.value = '<h3>Variable Inspector</h3>'\n", | |||
|
150 | "_modal_header_execs_label = widgets.StringWidget(parent=_modal_header, default_view_name=\"LabelView\")\n", | |||
|
151 | "_modal_header_execs_label.execs = 0\n", | |||
|
152 | "\n", | |||
|
153 | "_modal_body = widgets.ContainerWidget(parent=_modal_div)\n", | |||
|
154 | "_modal_body.vbox()\n", | |||
|
155 | "\n", | |||
|
156 | "_modal_body_label = widgets.StringWidget(parent=_modal_body, default_view_name=\"LabelView\")\n", | |||
|
157 | "_modal_body_label.value = 'Not hooked'\n", | |||
|
158 | "\n", | |||
|
159 | "_modal_footer = widgets.ContainerWidget(parent=_modal_div)\n", | |||
|
160 | "_modal_footer.vbox()\n", | |||
|
161 | "_var_filter = widgets.SelectionWidget(values=['Public', 'Private', 'IPython'], parent=_modal_footer, value='Public', default_view_name='ToggleButtonsView')\n", | |||
|
162 | "\n", | |||
|
163 | "display(_side_panel)\n", | |||
|
164 | "\n", | |||
|
165 | "_modal_header.add_class('modal-header')\n", | |||
|
166 | "_modal_body.add_class('modal-body')\n", | |||
|
167 | "_modal_footer.add_class('modal-footer')\n" | |||
|
168 | ], | |||
|
169 | "language": "python", | |||
|
170 | "metadata": {}, | |||
|
171 | "outputs": [], | |||
|
172 | "prompt_number": 3 | |||
|
173 | }, | |||
|
174 | { | |||
|
175 | "cell_type": "heading", | |||
|
176 | "level": 3, | |||
|
177 | "metadata": {}, | |||
|
178 | "source": [ | |||
|
179 | "Method that Fills the Inspector" | |||
|
180 | ] | |||
|
181 | }, | |||
|
182 | { | |||
|
183 | "cell_type": "code", | |||
|
184 | "collapsed": false, | |||
|
185 | "input": [ | |||
|
186 | "_ipython_input = re.compile('_i[0-9]*')\n", | |||
|
187 | "\n", | |||
|
188 | "def _fill_inspector():\n", | |||
|
189 | " \n", | |||
|
190 | " # Apply filter to variable names.\n", | |||
|
191 | " names = []\n", | |||
|
192 | " for name in sorted(_ipython.user_ns):\n", | |||
|
193 | " \n", | |||
|
194 | " match = _ipython_input.match(name)\n", | |||
|
195 | " is_ipython = (match is not None and match.group() == name) or \\\n", | |||
|
196 | " name == '_dh' or \\\n", | |||
|
197 | " name == '_ih' or \\\n", | |||
|
198 | " name == '_ii' or \\\n", | |||
|
199 | " name == '_iii' or \\\n", | |||
|
200 | " name == '_oh' or \\\n", | |||
|
201 | " name == '_sh' or \\\n", | |||
|
202 | " name == 'get_ipython' or \\\n", | |||
|
203 | " name == 'In' or \\\n", | |||
|
204 | " name == 'Out' or \\\n", | |||
|
205 | " name == 'exit' or \\\n", | |||
|
206 | " name == 'help' or \\\n", | |||
|
207 | " name == 'quit' or \\\n", | |||
|
208 | " name == '_' or \\\n", | |||
|
209 | " name == '__' or \\\n", | |||
|
210 | " name == '___'\n", | |||
|
211 | " \n", | |||
|
212 | " is_private = name.startswith('_')\n", | |||
|
213 | " is_public = not is_private\n", | |||
|
214 | " \n", | |||
|
215 | " var_filter = _var_filter.value\n", | |||
|
216 | " if var_filter == 'IPython' and is_ipython:\n", | |||
|
217 | " names.append(name)\n", | |||
|
218 | " elif var_filter == 'Private' and (is_private and not is_ipython):\n", | |||
|
219 | " names.append(name)\n", | |||
|
220 | " elif var_filter == 'Public' and (is_public and not is_ipython):\n", | |||
|
221 | " names.append(name)\n", | |||
|
222 | " \n", | |||
|
223 | " # Render each name and it's value.\n", | |||
|
224 | " variable_list_html = \"\"\"\n", | |||
|
225 | "<table class=\"table table-bordered table-striped\" style=\"width: 100%; overflow: hidden; table-layout:fixed;\">\n", | |||
|
226 | " <tr><th>Name</th><th>Type</th><th>Value</th>\"\"\"\n", | |||
|
227 | " for name in names:\n", | |||
|
228 | " var_value = _ipython.user_ns[name]\n", | |||
|
229 | " var_type = type(var_value)\n", | |||
|
230 | " var_small_value = str(var_value)[:100].replace(\"&\", \"&\").replace(\"<\", \"<\")\n", | |||
|
231 | " \n", | |||
|
232 | " if str(var_value) != var_small_value:\n", | |||
|
233 | " var_small_value += '<br><div class=\"label label-info\">...</div>'\n", | |||
|
234 | " \n", | |||
|
235 | " row = \"\"\"\n", | |||
|
236 | "<tr style='overflow: hidden;'>\n", | |||
|
237 | " <td style='width: 30%; overflow: hidden;'>{name}</td>\n", | |||
|
238 | " <td style='width: 30%; overflow: hidden;'>{type}</td>\n", | |||
|
239 | " <td style='width: 40%; overflow: hidden;'>{small_value}</td>\n", | |||
|
240 | "</tr>\n", | |||
|
241 | "\"\"\".format(name=name, type=var_type.__name__, small_value=var_small_value, value=str(var_value))\n", | |||
|
242 | " variable_list_html += row + '\\n'\n", | |||
|
243 | " variable_list_html += '</table>'\n", | |||
|
244 | " _modal_body_label.value = variable_list_html\n", | |||
|
245 | " \n", | |||
|
246 | " " | |||
|
247 | ], | |||
|
248 | "language": "python", | |||
|
249 | "metadata": {}, | |||
|
250 | "outputs": [], | |||
|
251 | "prompt_number": 4 | |||
|
252 | }, | |||
|
253 | { | |||
|
254 | "cell_type": "heading", | |||
|
255 | "level": 3, | |||
|
256 | "metadata": {}, | |||
|
257 | "source": [ | |||
|
258 | "Hook Cell Execute and Filter Change" | |||
|
259 | ] | |||
|
260 | }, | |||
|
261 | { | |||
|
262 | "cell_type": "code", | |||
|
263 | "collapsed": false, | |||
|
264 | "input": [ | |||
|
265 | "_ipython = get_ipython()\n", | |||
|
266 | "\n", | |||
|
267 | "try:\n", | |||
|
268 | " del _ipython._post_execute[handle_cell_executed]\n", | |||
|
269 | "except:\n", | |||
|
270 | " pass\n", | |||
|
271 | "\n", | |||
|
272 | "def _handle_cell_executed():\n", | |||
|
273 | " _modal_header_execs_label.execs += 1\n", | |||
|
274 | " _modal_header_execs_label.value = '%d cell executions captured' % _modal_header_execs_label.execs\n", | |||
|
275 | " _fill_inspector()\n", | |||
|
276 | "_ipython.register_post_execute(_handle_cell_executed)\n", | |||
|
277 | "\n", | |||
|
278 | "def _handle_filter_changed():\n", | |||
|
279 | " _fill_inspector()\n", | |||
|
280 | "_var_filter.on_trait_change(_handle_filter_changed, 'value')" | |||
|
281 | ], | |||
|
282 | "language": "python", | |||
|
283 | "metadata": {}, | |||
|
284 | "outputs": [], | |||
|
285 | "prompt_number": 5 | |||
|
286 | }, | |||
|
287 | { | |||
|
288 | "cell_type": "heading", | |||
|
289 | "level": 1, | |||
|
290 | "metadata": {}, | |||
|
291 | "source": [ | |||
|
292 | "Test" | |||
|
293 | ] | |||
|
294 | }, | |||
|
295 | { | |||
|
296 | "cell_type": "code", | |||
|
297 | "collapsed": false, | |||
|
298 | "input": [ | |||
|
299 | "a = 5" | |||
|
300 | ], | |||
|
301 | "language": "python", | |||
|
302 | "metadata": {}, | |||
|
303 | "outputs": [], | |||
|
304 | "prompt_number": 6 | |||
|
305 | }, | |||
|
306 | { | |||
|
307 | "cell_type": "code", | |||
|
308 | "collapsed": false, | |||
|
309 | "input": [ | |||
|
310 | "b = 3.0" | |||
|
311 | ], | |||
|
312 | "language": "python", | |||
|
313 | "metadata": {}, | |||
|
314 | "outputs": [], | |||
|
315 | "prompt_number": 7 | |||
|
316 | }, | |||
|
317 | { | |||
|
318 | "cell_type": "code", | |||
|
319 | "collapsed": false, | |||
|
320 | "input": [ | |||
|
321 | "c = a * b" | |||
|
322 | ], | |||
|
323 | "language": "python", | |||
|
324 | "metadata": {}, | |||
|
325 | "outputs": [], | |||
|
326 | "prompt_number": 8 | |||
|
327 | }, | |||
|
328 | { | |||
|
329 | "cell_type": "code", | |||
|
330 | "collapsed": false, | |||
|
331 | "input": [ | |||
|
332 | "d = \"String\"" | |||
|
333 | ], | |||
|
334 | "language": "python", | |||
|
335 | "metadata": {}, | |||
|
336 | "outputs": [], | |||
|
337 | "prompt_number": 9 | |||
|
338 | }, | |||
|
339 | { | |||
|
340 | "cell_type": "code", | |||
|
341 | "collapsed": false, | |||
|
342 | "input": [ | |||
|
343 | "del b" | |||
|
344 | ], | |||
|
345 | "language": "python", | |||
|
346 | "metadata": {}, | |||
|
347 | "outputs": [], | |||
|
348 | "prompt_number": 10 | |||
|
349 | } | |||
|
350 | ], | |||
|
351 | "metadata": {} | |||
|
352 | } | |||
|
353 | ] | |||
|
354 | } No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now