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