Show More
@@ -1,203 +1,193 | |||||
1 | { |
|
1 | { | |
2 | "metadata": { |
|
2 | "metadata": { | |
3 | "name": "" |
|
3 | "name": "", | |
|
4 | "signature": "sha256:7b872a09743460a3ce0358e9010c3b4baf5136b79a2a075964dea2c2fd85c82e" | |||
4 | }, |
|
5 | }, | |
5 | "nbformat": 3, |
|
6 | "nbformat": 3, | |
6 | "nbformat_minor": 0, |
|
7 | "nbformat_minor": 0, | |
7 | "worksheets": [ |
|
8 | "worksheets": [ | |
8 | { |
|
9 | { | |
9 | "cells": [ |
|
10 | "cells": [ | |
10 | { |
|
11 | { | |
11 | "cell_type": "heading", |
|
12 | "cell_type": "heading", | |
12 | "level": 1, |
|
13 | "level": 1, | |
13 | "metadata": {}, |
|
14 | "metadata": {}, | |
14 | "source": [ |
|
15 | "source": [ | |
15 | "Variable Inspector Widget" |
|
16 | "Variable Inspector Widget" | |
16 | ] |
|
17 | ] | |
17 | }, |
|
18 | }, | |
18 | { |
|
19 | { | |
19 | "cell_type": "heading", |
|
20 | "cell_type": "heading", | |
20 | "level": 2, |
|
21 | "level": 2, | |
21 | "metadata": {}, |
|
22 | "metadata": {}, | |
22 | "source": [ |
|
23 | "source": [ | |
23 | "A short example implementation" |
|
24 | "A short example implementation" | |
24 | ] |
|
25 | ] | |
25 | }, |
|
26 | }, | |
26 | { |
|
27 | { | |
27 | "cell_type": "markdown", |
|
28 | "cell_type": "markdown", | |
28 | "metadata": {}, |
|
29 | "metadata": {}, | |
29 | "source": [ |
|
30 | "source": [ | |
30 | "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." |
|
31 | "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." | |
31 | ] |
|
32 | ] | |
32 | }, |
|
33 | }, | |
33 | { |
|
34 | { | |
34 | "cell_type": "code", |
|
35 | "cell_type": "code", | |
35 | "collapsed": false, |
|
36 | "collapsed": false, | |
36 | "input": [ |
|
37 | "input": [ | |
37 | "from IPython.html import widgets # Loads the Widget framework.\n", |
|
38 | "from IPython.html import widgets # Loads the Widget framework.\n", | |
38 | "from IPython.core.magics.namespace import NamespaceMagics # Used to query namespace.\n", |
|
39 | "from IPython.core.magics.namespace import NamespaceMagics # Used to query namespace.\n", | |
39 | "\n", |
|
40 | "\n", | |
40 | "# For this example, hide these names, just to avoid polluting the namespace further\n", |
|
41 | "# For this example, hide these names, just to avoid polluting the namespace further\n", | |
41 | "get_ipython().user_ns_hidden['widgets'] = widgets\n", |
|
42 | "get_ipython().user_ns_hidden['widgets'] = widgets\n", | |
42 | "get_ipython().user_ns_hidden['NamespaceMagics'] = NamespaceMagics" |
|
43 | "get_ipython().user_ns_hidden['NamespaceMagics'] = NamespaceMagics" | |
43 | ], |
|
44 | ], | |
44 | "language": "python", |
|
45 | "language": "python", | |
45 | "metadata": {}, |
|
46 | "metadata": {}, | |
46 | "outputs": [], |
|
47 | "outputs": [], | |
47 | "prompt_number": 1 |
|
48 | "prompt_number": 1 | |
48 | }, |
|
49 | }, | |
49 | { |
|
50 | { | |
50 | "cell_type": "code", |
|
51 | "cell_type": "code", | |
51 | "collapsed": false, |
|
52 | "collapsed": false, | |
52 | "input": [ |
|
53 | "input": [ | |
53 | "class VariableInspectorWindow(object):\n", |
|
54 | "class VariableInspectorWindow(object):\n", | |
54 | " instance = None\n", |
|
55 | " instance = None\n", | |
55 | " \n", |
|
56 | " \n", | |
56 | " def __init__(self, ipython):\n", |
|
57 | " def __init__(self, ipython):\n", | |
57 | " \"\"\"Public constructor.\"\"\"\n", |
|
58 | " \"\"\"Public constructor.\"\"\"\n", | |
58 | " if VariableInspectorWindow.instance is not None:\n", |
|
59 | " if VariableInspectorWindow.instance is not None:\n", | |
59 | " raise Exception(\"\"\"Only one instance of the Variable Inspector can exist at a \n", |
|
60 | " raise Exception(\"\"\"Only one instance of the Variable Inspector can exist at a \n", | |
60 | " time. Call close() on the active instance before creating a new instance.\n", |
|
61 | " time. Call close() on the active instance before creating a new instance.\n", | |
61 | " If you have lost the handle to the active instance, you can re-obtain it\n", |
|
62 | " If you have lost the handle to the active instance, you can re-obtain it\n", | |
62 | " via `VariableInspectorWindow.instance`.\"\"\")\n", |
|
63 | " via `VariableInspectorWindow.instance`.\"\"\")\n", | |
63 | " \n", |
|
64 | " \n", | |
64 | " VariableInspectorWindow.instance = self\n", |
|
65 | " VariableInspectorWindow.instance = self\n", | |
65 | " self.closed = False\n", |
|
66 | " self.closed = False\n", | |
66 | " self.namespace = NamespaceMagics()\n", |
|
67 | " self.namespace = NamespaceMagics()\n", | |
67 | " self.namespace.shell = ipython.kernel.shell\n", |
|
68 | " self.namespace.shell = ipython.kernel.shell\n", | |
68 | " \n", |
|
69 | " \n", | |
69 | " self._popout = widgets.PopupWidget()\n", |
|
70 | " self._popout = widgets.PopupWidget()\n", | |
70 | " self._popout.description = \"Variable Inspector\"\n", |
|
71 | " self._popout.description = \"Variable Inspector\"\n", | |
71 | " self._popout.button_text = self._popout.description\n", |
|
72 | " self._popout.button_text = self._popout.description\n", | |
72 | "\n", |
|
73 | "\n", | |
73 | " self._modal_body = widgets.ContainerWidget()\n", |
|
74 | " self._modal_body = widgets.ContainerWidget()\n", | |
74 | " self._modal_body.set_css('overflow-y', 'scroll')\n", |
|
75 | " self._modal_body.set_css('overflow-y', 'scroll')\n", | |
75 | "\n", |
|
76 | "\n", | |
76 | " self._modal_body_label = widgets.HTMLWidget(value = 'Not hooked')\n", |
|
77 | " self._modal_body_label = widgets.HTMLWidget(value = 'Not hooked')\n", | |
77 | " self._modal_body.children = [self._modal_body_label]\n", |
|
78 | " self._modal_body.children = [self._modal_body_label]\n", | |
78 | "\n", |
|
79 | "\n", | |
79 | " self._modal_footer = widgets.ContainerWidget()\n", |
|
|||
80 | " self._var_filter = widgets.ToggleButtonsWidget(description=\"Method:\", values=['List', 'Details'], value='List')\n", |
|
|||
81 | " self._modal_footer.children = [self._var_filter]\n", |
|
|||
82 | "\n", |
|
|||
83 | " self._popout.children = [\n", |
|
80 | " self._popout.children = [\n", | |
84 | " self._modal_body, \n", |
|
81 | " self._modal_body, \n", | |
85 | " self._modal_footer,\n", |
|
|||
86 | " ]\n", |
|
82 | " ]\n", | |
87 | " \n", |
|
83 | " \n", | |
88 | " self._ipython = ipython\n", |
|
84 | " self._ipython = ipython\n", | |
89 | " self._ipython.register_post_execute(self._fill)\n", |
|
85 | " self._ipython.register_post_execute(self._fill)\n", | |
90 | " self._var_filter.on_trait_change(self._fill, 'value')\n", |
|
|||
91 | "\n", |
|
86 | "\n", | |
92 | " def close(self):\n", |
|
87 | " def close(self):\n", | |
93 | " \"\"\"Close and remove hooks.\"\"\"\n", |
|
88 | " \"\"\"Close and remove hooks.\"\"\"\n", | |
94 | " if not self.closed:\n", |
|
89 | " if not self.closed:\n", | |
95 | " del self._ipython._post_execute[self._fill]\n", |
|
90 | " del self._ipython._post_execute[self._fill]\n", | |
96 | " self._popout.close()\n", |
|
91 | " self._popout.close()\n", | |
97 | " self.closed = True\n", |
|
92 | " self.closed = True\n", | |
98 | " VariableInspectorWindow.instance = None\n", |
|
93 | " VariableInspectorWindow.instance = None\n", | |
99 | "\n", |
|
94 | "\n", | |
100 | " def _fill(self):\n", |
|
95 | " def _fill(self):\n", | |
101 | " \"\"\"Fill self with variable information.\"\"\"\n", |
|
96 | " \"\"\"Fill self with variable information.\"\"\"\n", | |
102 | " values = self.namespace.who_ls()\n", |
|
97 | " values = self.namespace.who_ls()\n", | |
103 | " mode = self._var_filter.value\n", |
|
|||
104 | " if mode == \"List\":\n", |
|
|||
105 | " self._modal_body_label.value = '<br>'.join(values)\n", |
|
|||
106 | " elif mode == \"Details\":\n", |
|
|||
107 |
" |
|
98 | " 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", | |
108 |
" |
|
99 | " '</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", | |
109 |
" |
|
100 | " '</td></tr></table>'\n", | |
110 | "\n", |
|
101 | "\n", | |
111 | " def _ipython_display_(self):\n", |
|
102 | " def _ipython_display_(self):\n", | |
112 | " \"\"\"Called when display() or pyout is used to display the Variable \n", |
|
103 | " \"\"\"Called when display() or pyout is used to display the Variable \n", | |
113 | " Inspector.\"\"\"\n", |
|
104 | " Inspector.\"\"\"\n", | |
114 | " self._popout._ipython_display_()\n", |
|
105 | " self._popout._ipython_display_()\n", | |
115 | " self._modal_footer.add_class('modal-footer')\n", |
|
|||
116 | " self._popout.add_class('vbox')\n", |
|
106 | " self._popout.add_class('vbox')\n", | |
117 | " self._modal_body.add_class('box-flex1')\n" |
|
107 | " self._modal_body.add_class('box-flex1')\n" | |
118 | ], |
|
108 | ], | |
119 | "language": "python", |
|
109 | "language": "python", | |
120 | "metadata": {}, |
|
110 | "metadata": {}, | |
121 | "outputs": [], |
|
111 | "outputs": [], | |
122 | "prompt_number": 2 |
|
112 | "prompt_number": 2 | |
123 | }, |
|
113 | }, | |
124 | { |
|
114 | { | |
125 | "cell_type": "code", |
|
115 | "cell_type": "code", | |
126 | "collapsed": false, |
|
116 | "collapsed": false, | |
127 | "input": [ |
|
117 | "input": [ | |
128 | "inspector = VariableInspectorWindow(get_ipython())\n", |
|
118 | "inspector = VariableInspectorWindow(get_ipython())\n", | |
129 | "inspector" |
|
119 | "inspector" | |
130 | ], |
|
120 | ], | |
131 | "language": "python", |
|
121 | "language": "python", | |
132 | "metadata": {}, |
|
122 | "metadata": {}, | |
133 | "outputs": [], |
|
123 | "outputs": [], | |
134 | "prompt_number": 3 |
|
124 | "prompt_number": 3 | |
135 | }, |
|
125 | }, | |
136 | { |
|
126 | { | |
137 | "cell_type": "heading", |
|
127 | "cell_type": "heading", | |
138 | "level": 1, |
|
128 | "level": 1, | |
139 | "metadata": {}, |
|
129 | "metadata": {}, | |
140 | "source": [ |
|
130 | "source": [ | |
141 | "Test" |
|
131 | "Test" | |
142 | ] |
|
132 | ] | |
143 | }, |
|
133 | }, | |
144 | { |
|
134 | { | |
145 | "cell_type": "code", |
|
135 | "cell_type": "code", | |
146 | "collapsed": false, |
|
136 | "collapsed": false, | |
147 | "input": [ |
|
137 | "input": [ | |
148 | "a = 5" |
|
138 | "a = 5" | |
149 | ], |
|
139 | ], | |
150 | "language": "python", |
|
140 | "language": "python", | |
151 | "metadata": {}, |
|
141 | "metadata": {}, | |
152 | "outputs": [], |
|
142 | "outputs": [], | |
153 | "prompt_number": 4 |
|
143 | "prompt_number": 4 | |
154 | }, |
|
144 | }, | |
155 | { |
|
145 | { | |
156 | "cell_type": "code", |
|
146 | "cell_type": "code", | |
157 | "collapsed": false, |
|
147 | "collapsed": false, | |
158 | "input": [ |
|
148 | "input": [ | |
159 | "b = 3.0" |
|
149 | "b = 3.0" | |
160 | ], |
|
150 | ], | |
161 | "language": "python", |
|
151 | "language": "python", | |
162 | "metadata": {}, |
|
152 | "metadata": {}, | |
163 | "outputs": [], |
|
153 | "outputs": [], | |
164 | "prompt_number": 5 |
|
154 | "prompt_number": 5 | |
165 | }, |
|
155 | }, | |
166 | { |
|
156 | { | |
167 | "cell_type": "code", |
|
157 | "cell_type": "code", | |
168 | "collapsed": false, |
|
158 | "collapsed": false, | |
169 | "input": [ |
|
159 | "input": [ | |
170 | "c = a * b" |
|
160 | "c = a * b" | |
171 | ], |
|
161 | ], | |
172 | "language": "python", |
|
162 | "language": "python", | |
173 | "metadata": {}, |
|
163 | "metadata": {}, | |
174 | "outputs": [], |
|
164 | "outputs": [], | |
175 | "prompt_number": 6 |
|
165 | "prompt_number": 6 | |
176 | }, |
|
166 | }, | |
177 | { |
|
167 | { | |
178 | "cell_type": "code", |
|
168 | "cell_type": "code", | |
179 | "collapsed": false, |
|
169 | "collapsed": false, | |
180 | "input": [ |
|
170 | "input": [ | |
181 | "d = \"String\"" |
|
171 | "d = \"String\"" | |
182 | ], |
|
172 | ], | |
183 | "language": "python", |
|
173 | "language": "python", | |
184 | "metadata": {}, |
|
174 | "metadata": {}, | |
185 | "outputs": [], |
|
175 | "outputs": [], | |
186 | "prompt_number": 7 |
|
176 | "prompt_number": 7 | |
187 | }, |
|
177 | }, | |
188 | { |
|
178 | { | |
189 | "cell_type": "code", |
|
179 | "cell_type": "code", | |
190 | "collapsed": false, |
|
180 | "collapsed": false, | |
191 | "input": [ |
|
181 | "input": [ | |
192 | "del b" |
|
182 | "del b" | |
193 | ], |
|
183 | ], | |
194 | "language": "python", |
|
184 | "language": "python", | |
195 | "metadata": {}, |
|
185 | "metadata": {}, | |
196 | "outputs": [], |
|
186 | "outputs": [], | |
197 | "prompt_number": 8 |
|
187 | "prompt_number": 8 | |
198 | } |
|
188 | } | |
199 | ], |
|
189 | ], | |
200 | "metadata": {} |
|
190 | "metadata": {} | |
201 | } |
|
191 | } | |
202 | ] |
|
192 | ] | |
203 | } No newline at end of file |
|
193 | } |
General Comments 0
You need to be logged in to leave comments.
Login now