##// END OF EJS Templates
ileo: pylab demo stuff
Ville M. Vainio -
Show More
@@ -1,522 +1,528 b''
1 <?xml version="1.0" encoding="utf-8"?>
1 <?xml version="1.0" encoding="utf-8"?>
2 <?xml-stylesheet ekr_test?>
2 <?xml-stylesheet ekr_test?>
3 <leo_file>
3 <leo_file>
4 <leo_header file_format="2" tnodes="0" max_tnode_index="0" clone_windows="0"/>
4 <leo_header file_format="2" tnodes="0" max_tnode_index="0" clone_windows="0"/>
5 <globals body_outline_ratio="0.307814992026">
5 <globals body_outline_ratio="0.307814992026">
6 <global_window_position top="256" left="325" height="627" width="1280"/>
6 <global_window_position top="180" left="223" height="627" width="1280"/>
7 <global_log_window_position top="0" left="0" height="0" width="0"/>
7 <global_log_window_position top="0" left="0" height="0" width="0"/>
8 </globals>
8 </globals>
9 <preferences/>
9 <preferences/>
10 <find_panel_settings/>
10 <find_panel_settings/>
11 <vnodes>
11 <vnodes>
12 <v t="vivainio.20080222193236" a="E"><vh>Documentation</vh>
12 <v t="vivainio.20080222193236" a="E"><vh>Documentation</vh>
13 <v t="vivainio.20080223121915" tnodeList="vivainio.20080223121915,vivainio.20080222193236.1,vivainio.20080223133858,vivainio.20080223133922,vivainio.20080223133947,vivainio.20080223134018,vivainio.20080223134100,vivainio.20080223134118,vivainio.20080223134433,vivainio.20080223142207,vivainio.20080223134136"><vh>@nosent ILeo_doc.txt</vh>
13 <v t="vivainio.20080223121915" tnodeList="vivainio.20080223121915,vivainio.20080222193236.1,vivainio.20080223133858,vivainio.20080223133922,vivainio.20080223133947,vivainio.20080223134018,vivainio.20080223134100,vivainio.20080223134118,vivainio.20080223134433,vivainio.20080223142207,vivainio.20080223134136"><vh>@nosent ILeo_doc.txt</vh>
14 <v t="vivainio.20080222193236.1"><vh>Documentation</vh>
14 <v t="vivainio.20080222193236.1"><vh>Documentation</vh>
15 <v t="vivainio.20080223133858"><vh>Introduction</vh></v>
15 <v t="vivainio.20080223133858"><vh>Introduction</vh></v>
16 <v t="vivainio.20080223133922"><vh>Installation</vh></v>
16 <v t="vivainio.20080223133922"><vh>Installation</vh></v>
17 <v t="vivainio.20080223133947"><vh>Accessing IPython from Leo</vh></v>
17 <v t="vivainio.20080223133947"><vh>Accessing IPython from Leo</vh></v>
18 <v t="vivainio.20080223134018"><vh>Accessing Leo nodes from IPython</vh></v>
18 <v t="vivainio.20080223134018"><vh>Accessing Leo nodes from IPython</vh></v>
19 <v t="vivainio.20080223134100"><vh>Cl definitions</vh></v>
19 <v t="vivainio.20080223134100"><vh>Cl definitions</vh></v>
20 <v t="vivainio.20080223134118"><vh>Special node types</vh></v>
20 <v t="vivainio.20080223134118"><vh>Special node types</vh></v>
21 <v t="vivainio.20080223134433"><vh>Custom push</vh></v>
21 <v t="vivainio.20080223134433"><vh>Custom push</vh></v>
22 <v t="vivainio.20080223142207" a="E"><vh>Code snippets</vh></v>
22 <v t="vivainio.20080223142207" a="E"><vh>Code snippets</vh></v>
23 <v t="vivainio.20080223134136"><vh>Acknowledgements and history</vh></v>
23 <v t="vivainio.20080223134136"><vh>Acknowledgements and history</vh></v>
24 </v>
24 </v>
25 </v>
25 </v>
26 </v>
26 </v>
27 <v t="vivainio.20080218184525"><vh>@chapters</vh></v>
27 <v t="vivainio.20080218184525"><vh>@chapters</vh></v>
28 <v t="vivainio.20080223133721" a="E"><vh>@settings</vh>
28 <v t="vivainio.20080223133721" a="E"><vh>@settings</vh>
29 <v t="vivainio.20080316092617"><vh>@@string ipython_argv = ipython -pylab</vh></v>
29 <v t="vivainio.20080316092617"><vh>@@string ipython_argv = ipython -pylab</vh></v>
30 <v t="vivainio.20080223133721.1"><vh>@enabled-plugins</vh></v>
30 <v t="vivainio.20080223133721.1"><vh>@enabled-plugins</vh></v>
31 </v>
31 </v>
32 <v t="vivainio.20080218184540"><vh>@ipy-startup</vh>
32 <v t="vivainio.20080218184540"><vh>@ipy-startup</vh>
33 <v t="vivainio.20080218184613.1"><vh>b</vh></v>
33 <v t="vivainio.20080218184613.1"><vh>b</vh></v>
34 <v t="vivainio.20080218200031"><vh>Some classes P</vh>
34 <v t="vivainio.20080218200031"><vh>Some classes P</vh>
35 <v t="vivainio.20080218190816"><vh>File-like access</vh></v>
35 <v t="vivainio.20080218190816"><vh>File-like access</vh></v>
36 <v t="vivainio.20080218200106"><vh>csv data</vh></v>
36 <v t="vivainio.20080218200106"><vh>csv data</vh></v>
37 <v t="vivainio.20080219225120"><vh>String list</vh></v>
37 <v t="vivainio.20080219225120"><vh>String list</vh></v>
38 <v t="vivainio.20080219230342"><vh>slist to leo</vh></v>
38 <v t="vivainio.20080219230342"><vh>slist to leo</vh></v>
39 </v>
39 </v>
40 </v>
40 </v>
41 <v t="vivainio.20080218195413"><vh>Class tests</vh>
41 <v t="vivainio.20080218195413"><vh>Class tests</vh>
42 <v t="vivainio.20080218200509"><vh>csvr</vh></v>
42 <v t="vivainio.20080218200509"><vh>csvr</vh></v>
43 <v t="vivainio.20080218191007"><vh>tempfile</vh></v>
43 <v t="vivainio.20080218191007"><vh>tempfile</vh></v>
44 <v t="vivainio.20080218195413.1"><vh>rfile</vh></v>
44 <v t="vivainio.20080218195413.1"><vh>rfile</vh></v>
45 <v t="vivainio.20080219225804"><vh>strlist</vh></v>
45 <v t="vivainio.20080219225804"><vh>strlist</vh></v>
46 </v>
46 </v>
47 <v t="vivainio.20080218201219" a="E"><vh>Direct variables</vh>
47 <v t="vivainio.20080218201219" a="E"><vh>Direct variables</vh>
48 <v t="vivainio.20080222201226"><vh>NewHeadline</vh></v>
48 <v t="vivainio.20080222201226"><vh>NewHeadline</vh></v>
49 <v t="vivainio.20080218201219.2"><vh>bar</vh></v>
49 <v t="vivainio.20080218201219.2"><vh>bar</vh></v>
50 </v>
50 </v>
51 <v t="vivainio.20080316144536" a="ETV"><vh>pylab tests</vh>
51 <v t="vivainio.20080316144536" a="E"><vh>pylab tests</vh>
52 <v t="vivainio.20080316145539.2" a="TV"><vh>Generate testarr</vh></v>
52 <v t="vivainio.20080316085925"><vh>testarr</vh></v>
53 <v t="vivainio.20080316085925"><vh>testarr</vh></v>
53 <v t="vivainio.20080316085950"><vh>Call plotter on testarr</vh></v>
54 <v t="vivainio.20080316085950"><vh>Call plotter on testarr</vh></v>
54 </v>
55 </v>
55 <v t="vivainio.20080222202211"><vh>test stuff</vh></v>
56 <v t="vivainio.20080222202211"><vh>test stuff</vh></v>
56 <v t="vivainio.20080223142403"><vh>@ipy-results</vh>
57 <v t="vivainio.20080223142403"><vh>@ipy-results</vh>
57 <v t="vivainio.20080223142403.1"><vh>foo</vh></v>
58 <v t="vivainio.20080223142403.1"><vh>foo</vh></v>
58 </v>
59 </v>
59 <v t="vivainio.20080222202211.1" a="E"><vh>spam</vh></v>
60 <v t="vivainio.20080222202211.1" a="E"><vh>spam</vh></v>
60 </vnodes>
61 </vnodes>
61 <tnodes>
62 <tnodes>
62 <t tx="vivainio.20080218184525">?</t>
63 <t tx="vivainio.20080218184525">?</t>
63 <t tx="vivainio.20080218184540">?Direct children of this node will be pushed at ipython bridge startup
64 <t tx="vivainio.20080218184540">?Direct children of this node will be pushed at ipython bridge startup
64
65
65 This node itself will *not* be pushed</t>
66 This node itself will *not* be pushed</t>
66 <t tx="vivainio.20080218184613.1">print "world"</t>
67 <t tx="vivainio.20080218184613.1">print "world"</t>
67 <t tx="vivainio.20080218190816">def rfile(body,n):
68 <t tx="vivainio.20080218190816">def rfile(body,n):
68 """ @cl rfile
69 """ @cl rfile
69
70
70 produces a StringIO (file like obj of the rest of the body) """
71 produces a StringIO (file like obj of the rest of the body) """
71
72
72 import StringIO
73 import StringIO
73 return StringIO.StringIO(body)
74 return StringIO.StringIO(body)
74
75
75 def tmpfile(body,n):
76 def tmpfile(body,n):
76 """ @cl tmpfile
77 """ @cl tmpfile
77
78
78 Produces a temporary file, with node body as contents
79 Produces a temporary file, with node body as contents
79
80
80 """
81 """
81 import tempfile
82 import tempfile
82 h, fname = tempfile.mkstemp()
83 h, fname = tempfile.mkstemp()
83 f = open(fname,'w')
84 f = open(fname,'w')
84 f.write(body)
85 f.write(body)
85 f.close()
86 f.close()
86 return fname
87 return fname
87 </t>
88 </t>
88 <t tx="vivainio.20080218191007">@cl tmpfile
89 <t tx="vivainio.20080218191007">@cl tmpfile
89
90
90 Hello</t>
91 Hello</t>
91 <t tx="vivainio.20080218195413">?</t>
92 <t tx="vivainio.20080218195413">?</t>
92 <t tx="vivainio.20080218195413.1">@cl rfile
93 <t tx="vivainio.20080218195413.1">@cl rfile
93 These
94 These
94 lines
95 lines
95 should
96 should
96 be
97 be
97 readable </t>
98 readable </t>
98 <t tx="vivainio.20080218200031">@others</t>
99 <t tx="vivainio.20080218200031">@others</t>
99 <t tx="vivainio.20080218200106">def csvdata(body,n):
100 <t tx="vivainio.20080218200106">def csvdata(body,n):
100 import csv
101 import csv
101 d = csv.Sniffer().sniff(body)
102 d = csv.Sniffer().sniff(body)
102 reader = csv.reader(body.splitlines(), dialect = d)
103 reader = csv.reader(body.splitlines(), dialect = d)
103 return reader</t>
104 return reader</t>
104 <t tx="vivainio.20080218200509">@cl csvdata
105 <t tx="vivainio.20080218200509">@cl csvdata
105
106
106 a,b,b
107 a,b,b
107 1,2,2</t>
108 1,2,2</t>
108 <t tx="vivainio.20080218201219"></t>
109 <t tx="vivainio.20080218201219"></t>
109 <t tx="vivainio.20080218201219.2">@cl
110 <t tx="vivainio.20080218201219.2">@cl
110 "hello world"</t>
111 "hello world"</t>
111 <t tx="vivainio.20080219225120">import IPython.genutils
112 <t tx="vivainio.20080219225120">import IPython.genutils
112 def slist(body,n):
113 def slist(body,n):
113 return IPython.genutils.SList(body.splitlines())
114 return IPython.genutils.SList(body.splitlines())
114 </t>
115 </t>
115 <t tx="vivainio.20080219225804">@cl slist
116 <t tx="vivainio.20080219225804">@cl slist
116 hello
117 hello
117 world
118 world
118 on
119 on
119 many
120 many
120 lines
121 lines
121 </t>
122 </t>
122 <t tx="vivainio.20080219230342">import ipy_leo
123 <t tx="vivainio.20080219230342">import ipy_leo
123 @ipy_leo.format_for_leo.when_type(IPython.genutils.SList)
124 @ipy_leo.format_for_leo.when_type(IPython.genutils.SList)
124 def format_slist(obj):
125 def format_slist(obj):
125 return "@cl slist\n" + obj.n
126 return "@cl slist\n" + obj.n
126 </t>
127 </t>
127 <t tx="vivainio.20080222193236">?</t>
128 <t tx="vivainio.20080222193236">?</t>
128 <t tx="vivainio.20080222193236.1">@wrap
129 <t tx="vivainio.20080222193236.1">@wrap
129 @nocolor</t>
130 @nocolor</t>
130 <t tx="vivainio.20080222201226">1+2
131 <t tx="vivainio.20080222201226">1+2
131 print "hello"
132 print "hello"
132 3+4
133 3+4
133
134
134 def f(x):
135 def f(x):
135 return x.upper()
136 return x.upper()
136
137
137 f('hello world')
138 f('hello world')
138
139
139 if 0:
140 if 0:
140 print "foo"
141 print "foo"
141 else:
142 else:
142 print "bar"
143 print "bar"
143
144
144 </t>
145 </t>
145 <t tx="vivainio.20080222202211"></t>
146 <t tx="vivainio.20080222202211"></t>
146 <t tx="vivainio.20080222202211.1" ipython="7d71005506636f6f7264737101284b0c4bde747102732e">@cl rfile
147 <t tx="vivainio.20080222202211.1" ipython="7d71005506636f6f7264737101284b0c4bde747102732e">@cl rfile
147 hello
148 hello
148 world
149 world
149 and whatever</t>
150 and whatever</t>
150 <t tx="vivainio.20080223121915">@others
151 <t tx="vivainio.20080223121915">@others
151 </t>
152 </t>
152 <t tx="vivainio.20080223133721"></t>
153 <t tx="vivainio.20080223133721"></t>
153 <t tx="vivainio.20080223133721.1">ipython.py</t>
154 <t tx="vivainio.20080223133721.1">ipython.py</t>
154 <t tx="vivainio.20080223133858">
155 <t tx="vivainio.20080223133858">
155 Introduction
156 Introduction
156 ============
157 ============
157
158
158 The purpose of ILeo, or leo-ipython bridge, is being a two-way communication
159 The purpose of ILeo, or leo-ipython bridge, is being a two-way communication
159 channel between Leo and IPython. The level of integration is much deeper than
160 channel between Leo and IPython. The level of integration is much deeper than
160 conventional integration in IDEs; most notably, you are able to store *data* in
161 conventional integration in IDEs; most notably, you are able to store *data* in
161 Leo nodes, in addition to mere program code. The possibilities of this are
162 Leo nodes, in addition to mere program code. The possibilities of this are
162 endless, and this degree of integration has not been seen previously in the python
163 endless, and this degree of integration has not been seen previously in the python
163 world.
164 world.
164
165
165 IPython users are accustomed to using things like %edit to produce non-trivial
166 IPython users are accustomed to using things like %edit to produce non-trivial
166 functions/classes (i.e. something that they don't want to enter directly on the
167 functions/classes (i.e. something that they don't want to enter directly on the
167 interactive prompt, but creating a proper script/module involves too much
168 interactive prompt, but creating a proper script/module involves too much
168 overhead). In ILeo, this task consists just going to the Leo window, creating a node
169 overhead). In ILeo, this task consists just going to the Leo window, creating a node
169 and writing the code there, and pressing alt+I (push-to-ipython).
170 and writing the code there, and pressing alt+I (push-to-ipython).
170
171
171 Obviously, you can save the Leo document as usual - this is a great advantage
172 Obviously, you can save the Leo document as usual - this is a great advantage
172 of ILeo over using %edit, you can save your experimental scripts all at one
173 of ILeo over using %edit, you can save your experimental scripts all at one
173 time, without having to organize them into script/module files (before you
174 time, without having to organize them into script/module files (before you
174 really want to, of course!)
175 really want to, of course!)
175 </t>
176 </t>
176 <t tx="vivainio.20080223133922">
177 <t tx="vivainio.20080223133922">
177 Installation
178 Installation
178 ============
179 ============
179
180
180 You need at least Leo 4.4.7, and the development version of IPython (ILeo
181 You need at least Leo 4.4.7, and the development version of IPython (ILeo
181 will be incorporated to IPython 0.8.3).
182 will be incorporated to IPython 0.8.3).
182
183
183 You can get IPython from Launchpad by installing bzr and doing
184 You can get IPython from Launchpad by installing bzr and doing
184
185
185 bzr branch lp:ipython
186 bzr branch lp:ipython
186
187
187 and running "setup.py install".
188 and running "setup.py install".
188
189
189 You need to enable the 'ipython.py' plugin in Leo:
190 You need to enable the 'ipython.py' plugin in Leo:
190
191
191 - Help -&gt; Open LeoSettings.leo
192 - Help -&gt; Open LeoSettings.leo
192
193
193 - Edit @settings--&gt;Plugins--&gt;@enabled-plugins, add/uncomment 'ipython.py'
194 - Edit @settings--&gt;Plugins--&gt;@enabled-plugins, add/uncomment 'ipython.py'
194
195
195 - Alternatively, you can add @settings--&gt;@enabled-plugins with body ipython.py to your leo document.
196 - Alternatively, you can add @settings--&gt;@enabled-plugins with body ipython.py to your leo document.
196
197
197 - Restart Leo. Be sure that you have the console window open (start leo.py from console, or double-click leo.py on windows)
198 - Restart Leo. Be sure that you have the console window open (start leo.py from console, or double-click leo.py on windows)
198
199
199 - Press alt+5 OR alt-x start-ipython to launch IPython in the console that
200 - Press alt+5 OR alt-x start-ipython to launch IPython in the console that
200 started leo. You can start entering IPython commands normally, and Leo will keep
201 started leo. You can start entering IPython commands normally, and Leo will keep
201 running at the same time.
202 running at the same time.
202 </t>
203 </t>
203 <t tx="vivainio.20080223133947">
204 <t tx="vivainio.20080223133947">
204 Accessing IPython from Leo
205 Accessing IPython from Leo
205 ==========================
206 ==========================
206
207
207 IPython code
208 IPython code
208 ------------
209 ------------
209
210
210 Just enter IPython commands on a Leo node and press alt-I to execute
211 Just enter IPython commands on a Leo node and press alt-I to execute
211 push-to-ipython in order to execute the script in IPython. 'commands' is
212 push-to-ipython in order to execute the script in IPython. 'commands' is
212 interpreted loosely here - you can enter function and class definitions, in
213 interpreted loosely here - you can enter function and class definitions, in
213 addition to the things you would usually enter at IPython prompt - calculations,
214 addition to the things you would usually enter at IPython prompt - calculations,
214 system commands etc.
215 system commands etc.
215
216
216 Everything that would be legal to enter on IPython prompt is legal to execute
217 Everything that would be legal to enter on IPython prompt is legal to execute
217 from ILeo.
218 from ILeo.
218
219
219 Results will be shows in Leo log window for convenience, in addition to the console.
220 Results will be shows in Leo log window for convenience, in addition to the console.
220
221
221 Suppose that a node had the following contents:
222 Suppose that a node had the following contents:
222 {{{
223 {{{
223 1+2
224 1+2
224 print "hello"
225 print "hello"
225 3+4
226 3+4
226
227
227 def f(x):
228 def f(x):
228 return x.upper()
229 return x.upper()
229
230
230 f('hello world')
231 f('hello world')
231 }}}
232 }}}
232
233
233 If you press alt+I on that node, you will see the following in Leo log window (IPython tab):
234 If you press alt+I on that node, you will see the following in Leo log window (IPython tab):
234
235
235 {{{
236 {{{
236 In: 1+2
237 In: 1+2
237 &lt;2&gt; 3
238 &lt;2&gt; 3
238 In: 3+4
239 In: 3+4
239 &lt;4&gt; 7
240 &lt;4&gt; 7
240 In: f('hello world')
241 In: f('hello world')
241 &lt;6&gt; 'HELLO WORLD'
242 &lt;6&gt; 'HELLO WORLD'
242 }}}
243 }}}
243
244
244 (numbers like &lt;6&gt; mean IPython output history indices; the actual object can be
245 (numbers like &lt;6&gt; mean IPython output history indices; the actual object can be
245 referenced with _6 as usual in IPython).
246 referenced with _6 as usual in IPython).
246
247
247
248
248 Plain Python code
249 Plain Python code
249 -----------------
250 -----------------
250
251
251 If the headline of the node ends with capital P, alt-I will not run the code
252 If the headline of the node ends with capital P, alt-I will not run the code
252 through IPython translation mechanism but use the direct python 'exec' statement
253 through IPython translation mechanism but use the direct python 'exec' statement
253 (in IPython user namespace) to execute the code. It wont be shown in IPython
254 (in IPython user namespace) to execute the code. It wont be shown in IPython
254 history, and sometimes it is safer (and more efficient) to execute things as
255 history, and sometimes it is safer (and more efficient) to execute things as
255 plain Python statements. Large class definitions are good candidates for P
256 plain Python statements. Large class definitions are good candidates for P
256 nodes.
257 nodes.
257 </t>
258 </t>
258 <t tx="vivainio.20080223134018">
259 <t tx="vivainio.20080223134018">
259 Accessing Leo nodes from IPython
260 Accessing Leo nodes from IPython
260 ================================
261 ================================
261
262
262 The real fun starts when you start entering text to leo nodes, and are using
263 The real fun starts when you start entering text to leo nodes, and are using
263 that as data (input/output) for your IPython work.
264 that as data (input/output) for your IPython work.
264
265
265 Accessing Leo nodes happens through the variable 'wb' (short for "WorkBook")
266 Accessing Leo nodes happens through the variable 'wb' (short for "WorkBook")
266 that exist in the IPython user namespace. Nodes that are directly accessible are
267 that exist in the IPython user namespace. Nodes that are directly accessible are
267 the ones that have simple names which could also be Python variable names;
268 the ones that have simple names which could also be Python variable names;
268 'foo_1' will be accessible directly from IPython, whereas 'my scripts' will not.
269 'foo_1' will be accessible directly from IPython, whereas 'my scripts' will not.
269 If you want to access a node with arbitrary headline, add a child node '@a foo'
270 If you want to access a node with arbitrary headline, add a child node '@a foo'
270 (@a stands for 'anchor'). Then, the parent of '@a foo' is accessible through
271 (@a stands for 'anchor'). Then, the parent of '@a foo' is accessible through
271 'wb.foo'.
272 'wb.foo'.
272
273
273 You can see what nodes are accessible be entering (in IPython) wb.&lt;TAB&gt;. Example:
274 You can see what nodes are accessible be entering (in IPython) wb.&lt;TAB&gt;. Example:
274
275
275 [C:leo/src]|12&gt; wb.
276 [C:leo/src]|12&gt; wb.
276 wb.b wb.tempfile wb.rfile wb.NewHeadline
277 wb.b wb.tempfile wb.rfile wb.NewHeadline
277 wb.bar wb.Docs wb.strlist wb.csvr
278 wb.bar wb.Docs wb.strlist wb.csvr
278 [C:leo/src]|12&gt; wb.tempfile
279 [C:leo/src]|12&gt; wb.tempfile
279 &lt;12&gt; &lt;ipy_leo.LeoNode object at 0x044B6D90&gt;
280 &lt;12&gt; &lt;ipy_leo.LeoNode object at 0x044B6D90&gt;
280
281
281 So here, we meet the 'LeoNode' class that is your key to manipulating Leo
282 So here, we meet the 'LeoNode' class that is your key to manipulating Leo
282 content from IPython!
283 content from IPython!
283
284
284 LeoNode
285 LeoNode
285 -------
286 -------
286
287
287 Suppose that we had a node with headline 'spam' and body:
288 Suppose that we had a node with headline 'spam' and body:
288
289
289 ['12',2222+32]
290 ['12',2222+32]
290
291
291 we can access it from IPython (or from scripts entered into other Leo nodes!) by doing:
292 we can access it from IPython (or from scripts entered into other Leo nodes!) by doing:
292
293
293 C:leo/src]|19&gt; wb.spam.v
294 C:leo/src]|19&gt; wb.spam.v
294 &lt;19&gt; ['12', 2254]
295 &lt;19&gt; ['12', 2254]
295
296
296 'v' attribute stands for 'value', which means the node contents will be run
297 'v' attribute stands for 'value', which means the node contents will be run
297 through 'eval' and everything you would be able to enter into IPython prompt
298 through 'eval' and everything you would be able to enter into IPython prompt
298 will be converted to objects. This mechanism can be extended far beyond direct
299 will be converted to objects. This mechanism can be extended far beyond direct
299 evaluation (see '@cl definitions').
300 evaluation (see '@cl definitions').
300
301
301 'v' attribute also has a setter, i.e. you can do:
302 'v' attribute also has a setter, i.e. you can do:
302
303
303 wb.spam.v = "mystring"
304 wb.spam.v = "mystring"
304
305
305 Which will result in the node 'spam' having the following text:
306 Which will result in the node 'spam' having the following text:
306
307
307 'mystring'
308 'mystring'
308
309
309 What assignment to 'v' does can be configured through generic functions
310 What assignment to 'v' does can be configured through generic functions
310 ('simplegeneric' module, will be explained later).
311 ('simplegeneric' module, will be explained later).
311
312
312 Besides v, you can set the body text directly through
313 Besides v, you can set the body text directly through
313
314
314 wb.spam.b = "some\nstring",
315 wb.spam.b = "some\nstring",
315
316
316 headline by
317 headline by
317
318
318 wb.spam.h = 'new_headline'
319 wb.spam.h = 'new_headline'
319
320
320 (obviously you must access the node through wb.new_headline from that point
321 (obviously you must access the node through wb.new_headline from that point
321 onwards), and access the contents as string list (IPython SList) through
322 onwards), and access the contents as string list (IPython SList) through
322 'wb.spam.l'.
323 'wb.spam.l'.
323
324
324 If you do 'wb.foo.v = 12' when node named 'foo' does not exist, the node titled
325 If you do 'wb.foo.v = 12' when node named 'foo' does not exist, the node titled
325 'foo' will be automatically created and assigned body 12.
326 'foo' will be automatically created and assigned body 12.
326
327
327 LeoNode also supports go() that focuses the node in the Leo window, and ipush()
328 LeoNode also supports go() that focuses the node in the Leo window, and ipush()
328 that simulates pressing alt+I on the node.
329 that simulates pressing alt+I on the node.
329
330
330 You can access unknownAttributes by .uA property dictionary. Unknown attributes
331 You can access unknownAttributes by .uA property dictionary. Unknown attributes
331 allow you to store arbitrary (pickleable) python objects in the Leo nodes; the
332 allow you to store arbitrary (pickleable) python objects in the Leo nodes; the
332 attributes are stored when you save the .leo document, and recreated when you
333 attributes are stored when you save the .leo document, and recreated when you
333 open the document again. The attributes are not visible anywhere, but can be
334 open the document again. The attributes are not visible anywhere, but can be
334 used for domain-specific metatada. Example:
335 used for domain-specific metatada. Example:
335
336
336 [C:leo/src]|12&gt; wb.spam.uA['coords'] = (12,222)
337 [C:leo/src]|12&gt; wb.spam.uA['coords'] = (12,222)
337 [C:leo/src]|13&gt; wb.spam.uA
338 [C:leo/src]|13&gt; wb.spam.uA
338 &lt;13&gt; {'coords': (12, 222)}
339 &lt;13&gt; {'coords': (12, 222)}
339
340
340 Accessing children with iteration and dict notation
341 Accessing children with iteration and dict notation
341 ---------------------------------------------------
342 ---------------------------------------------------
342
343
343 Sometimes, you may want to treat a node as a 'database', where the nodes
344 Sometimes, you may want to treat a node as a 'database', where the nodes
344 children represent elements in the database. You can create a new child node for
345 children represent elements in the database. You can create a new child node for
345 node 'spam', with headline 'foo bar' like this:
346 node 'spam', with headline 'foo bar' like this:
346
347
347 wb.spam['foo bar'] = "Hello"
348 wb.spam['foo bar'] = "Hello"
348
349
349 And assign a new value for it by doing
350 And assign a new value for it by doing
350
351
351 wb.spam['foo bar'].v = "Hello again"
352 wb.spam['foo bar'].v = "Hello again"
352
353
353 Note how you can't use .v when you first create the node - i.e. the node needs
354 Note how you can't use .v when you first create the node - i.e. the node needs
354 to be initialized by simple assignment, that will be interpreted as assignment
355 to be initialized by simple assignment, that will be interpreted as assignment
355 to '.v'. This is a conscious design choice.
356 to '.v'. This is a conscious design choice.
356
357
357 If you try to do wb.spam['bar'] = 'Hello', ILeo will assign '@k bar' as the
358 If you try to do wb.spam['bar'] = 'Hello', ILeo will assign '@k bar' as the
358 headline for the child instead, because 'bar' is a legal python name (and as
359 headline for the child instead, because 'bar' is a legal python name (and as
359 such would be incorporated in the workbook namespace). This is done to avoid
360 such would be incorporated in the workbook namespace). This is done to avoid
360 crowding the workbook namespace with extraneous items. The item will still be
361 crowding the workbook namespace with extraneous items. The item will still be
361 accessible as wb.spam['bar']
362 accessible as wb.spam['bar']
362
363
363 LeoNodes are iterable, so to see the headlines of all the children of 'spam' do:
364 LeoNodes are iterable, so to see the headlines of all the children of 'spam' do:
364
365
365 for n in wb.spam:
366 for n in wb.spam:
366 print n.h
367 print n.h
367 </t>
368 </t>
368 <t tx="vivainio.20080223134100">
369 <t tx="vivainio.20080223134100">
369 @cl definitions
370 @cl definitions
370 ===============
371 ===============
371
372
372 If the first line in the body text is of the form '@cl sometext', IPython will
373 If the first line in the body text is of the form '@cl sometext', IPython will
373 evaluate 'sometext' and call the result with the rest of the body when you do
374 evaluate 'sometext' and call the result with the rest of the body when you do
374 'wb.foo.v'. An example is in place here. Suppose that we have defined a class (I
375 'wb.foo.v'. An example is in place here. Suppose that we have defined a class (I
375 use the term class in a non-python sense here)
376 use the term class in a non-python sense here)
376
377
377 {{{
378 {{{
378 def rfile(body,node):
379 def rfile(body,node):
379 """ @cl rfile
380 """ @cl rfile
380
381
381 produces a StringIO (file like obj) of the rest of the body """
382 produces a StringIO (file like obj) of the rest of the body """
382
383
383 import StringIO
384 import StringIO
384 return StringIO.StringIO(body)
385 return StringIO.StringIO(body)
385 }}}
386 }}}
386
387
387 (note that node is ignored here - but it could be used to access headline,
388 (note that node is ignored here - but it could be used to access headline,
388 children etc.),
389 children etc.),
389
390
390 Now, let's say you have node 'spam' with text
391 Now, let's say you have node 'spam' with text
391
392
392 {{{
393 {{{
393 @cl rfile
394 @cl rfile
394 hello
395 hello
395 world
396 world
396 and whatever
397 and whatever
397 }}}
398 }}}
398
399
399 Now, in IPython, we can do this:
400 Now, in IPython, we can do this:
400
401
401 {{{
402 {{{
402 [C:leo/src]|22&gt; f = wb.spam.v
403 [C:leo/src]|22&gt; f = wb.spam.v
403 [C:leo/src]|23&gt; f
404 [C:leo/src]|23&gt; f
404 &lt;23&gt; &lt;StringIO.StringIO instance at 0x04E7E490&gt;
405 &lt;23&gt; &lt;StringIO.StringIO instance at 0x04E7E490&gt;
405 [C:leo/src]|24&gt; f.readline()
406 [C:leo/src]|24&gt; f.readline()
406 &lt;24&gt; u'hello\n'
407 &lt;24&gt; u'hello\n'
407 [C:leo/src]|25&gt; f.readline()
408 [C:leo/src]|25&gt; f.readline()
408 &lt;25&gt; u'world\n'
409 &lt;25&gt; u'world\n'
409 [C:leo/src]|26&gt; f.readline()
410 [C:leo/src]|26&gt; f.readline()
410 &lt;26&gt; u'and whatever'
411 &lt;26&gt; u'and whatever'
411 [C:leo/src]|27&gt; f.readline()
412 [C:leo/src]|27&gt; f.readline()
412 &lt;27&gt; u''
413 &lt;27&gt; u''
413 }}}
414 }}}
414
415
415 You should declare new @cl types to make ILeo as convenient your problem domain
416 You should declare new @cl types to make ILeo as convenient your problem domain
416 as possible. For example, a "@cl etree" could return the elementtree object for
417 as possible. For example, a "@cl etree" could return the elementtree object for
417 xml content.
418 xml content.
418 </t>
419 </t>
419 <t tx="vivainio.20080223134118">
420 <t tx="vivainio.20080223134118">
420 Special node types
421 Special node types
421 ==================
422 ==================
422
423
423 @ipy-startup
424 @ipy-startup
424 ------------
425 ------------
425
426
426 If this node exist, the *direct children* of this will be pushed to IPython when
427 If this node exist, the *direct children* of this will be pushed to IPython when
427 ILeo is started (you press alt+5). Use it to push your own @cl definitions etc.
428 ILeo is started (you press alt+5). Use it to push your own @cl definitions etc.
428 The contents of of the node itself will be ignored.
429 The contents of of the node itself will be ignored.
429
430
430 @ipy-results
431 @ipy-results
431 ------------
432 ------------
432
433
433 When you create a new node (wb.foo.v = 'stuff'), the node foo will be created as
434 When you create a new node (wb.foo.v = 'stuff'), the node foo will be created as
434 a child of this node. If @ipy-results does not exist, the new node will be created after the currently selected node.
435 a child of this node. If @ipy-results does not exist, the new node will be created after the currently selected node.
435
436
436 @a nodes
437 @a nodes
437 --------
438 --------
438
439
439 You can attach these as children of existing nodes to provide a way to access
440 You can attach these as children of existing nodes to provide a way to access
440 nodes with arbitrary headlines, or to provide aliases to other nodes. If
441 nodes with arbitrary headlines, or to provide aliases to other nodes. If
441 multiple @a nodes are attached as children of a node, all the names can be used
442 multiple @a nodes are attached as children of a node, all the names can be used
442 to access the same object.
443 to access the same object.
443 </t>
444 </t>
444 <t tx="vivainio.20080223134136">
445 <t tx="vivainio.20080223134136">
445 Acknowledgements &amp; History
446 Acknowledgements &amp; History
446 ==========================
447 ==========================
447
448
448 This idea got started when I (Ville) saw this post by Edward Ream (the author of
449 This idea got started when I (Ville) saw this post by Edward Ream (the author of
449 Leo) on IPython developer mailing list:
450 Leo) on IPython developer mailing list:
450
451
451 http://lists.ipython.scipy.org/pipermail/ipython-dev/2008-January/003551.html
452 http://lists.ipython.scipy.org/pipermail/ipython-dev/2008-January/003551.html
452
453
453 I was using FreeMind as mind mapping software, and so I had an immediate use
454 I was using FreeMind as mind mapping software, and so I had an immediate use
454 case for Leo (which, incidentally, is superior to FreeMind as mind mapper). The
455 case for Leo (which, incidentally, is superior to FreeMind as mind mapper). The
455 wheels started rolling, I got obsessed with the power of this concept
456 wheels started rolling, I got obsessed with the power of this concept
456 (everything clicked together), and Edwards excitement paralleled mine.
457 (everything clicked together), and Edwards excitement paralleled mine.
457 Everything was mind-bogglingly easy/trivial, something that is typical of all
458 Everything was mind-bogglingly easy/trivial, something that is typical of all
458 revolutionary technologies (think Python here).
459 revolutionary technologies (think Python here).
459
460
460 The discussion that "built" ILeo is here:
461 The discussion that "built" ILeo is here:
461 http://sourceforge.net/forum/forum.php?thread_id=1911662&amp;forum_id=10226
462 http://sourceforge.net/forum/forum.php?thread_id=1911662&amp;forum_id=10226
462
463
463 ?</t>
464 ?</t>
464 <t tx="vivainio.20080223134433">
465 <t tx="vivainio.20080223134433">
465 Declaring custom push-to-ipython handlers
466 Declaring custom push-to-ipython handlers
466 =========================================
467 =========================================
467
468
468 Sometimes, you might want to configure what alt+I on a node does. You can do
469 Sometimes, you might want to configure what alt+I on a node does. You can do
469 that by creating your own push function and expose it using
470 that by creating your own push function and expose it using
470 ipy_leo.expose_ileo_push(f, priority). The function should check whether the
471 ipy_leo.expose_ileo_push(f, priority). The function should check whether the
471 node should by handled by the function and raise IPython.ipapi.TryNext if it
472 node should by handled by the function and raise IPython.ipapi.TryNext if it
472 will not do the handling, giving the next function in the chain a chance to see
473 will not do the handling, giving the next function in the chain a chance to see
473 whether it should handle the push.
474 whether it should handle the push.
474
475
475 This example would print an uppercase version of node body if the node headline ends
476 This example would print an uppercase version of node body if the node headline ends
476 with U (yes, this is completely useless!):
477 with U (yes, this is completely useless!):
477
478
478 {{{
479 {{{
479 def push_upcase(node):
480 def push_upcase(node):
480 if not node.h.endswith('U'):
481 if not node.h.endswith('U'):
481 raise TryNext
482 raise TryNext
482 print node.b.upper()
483 print node.b.upper()
483
484
484 ipy_leo.expose_ileo_push(push_upcase, 12)
485 ipy_leo.expose_ileo_push(push_upcase, 12)
485 }}}
486 }}}
486
487
487 (the priority should be between 0-100 - typically, you don't need to care about
488 (the priority should be between 0-100 - typically, you don't need to care about
488 it and can usually omit the argument altogether)
489 it and can usually omit the argument altogether)
489 </t>
490 </t>
490 <t tx="vivainio.20080223142207">
491 <t tx="vivainio.20080223142207">
491 Example code snippets
492 Example code snippets
492 =====================
493 =====================
493
494
494 Get list of all headlines of all the nodes in leo:
495 Get list of all headlines of all the nodes in leo:
495
496
496 [node.h for node in wb]
497 [node.h for node in wb]
497
498
498 Create node with headline 'baz', empty body:
499 Create node with headline 'baz', empty body:
499 wb.baz
500 wb.baz
500
501
501 Create 10 child nodes for baz, where i is headline and 'Hello ' + i is body:
502 Create 10 child nodes for baz, where i is headline and 'Hello ' + i is body:
502
503
503 for i in range(10):
504 for i in range(10):
504 wb.baz[i] = 'Hello %d' % i
505 wb.baz[i] = 'Hello %d' % i
505
506
506
507
507 </t>
508 </t>
508 <t tx="vivainio.20080223142403"></t>
509 <t tx="vivainio.20080223142403"></t>
509 <t tx="vivainio.20080223142403.1">12</t>
510 <t tx="vivainio.20080223142403.1">12</t>
510 <t tx="vivainio.20080316085925">array([[1, 2, 3],
511 <t tx="vivainio.20080316085925">array([[ 0, 1, 2],
511 [2, 4, 6]])</t>
512 [ 3, 4, 5],
513 [ 6, 7, 8],
514 [ 9, 10, 11]])</t>
512 <t tx="vivainio.20080316085950"># press alt+i here to plot testarr
515 <t tx="vivainio.20080316085950"># press alt+i here to plot testarr
513
516
514 plot(wb.testarr.v)</t>
517 plot(wb.testarr.v)</t>
515 <t tx="vivainio.20080316092617"></t>
518 <t tx="vivainio.20080316092617"></t>
516 <t tx="vivainio.20080316144536">Quickstart:
519 <t tx="vivainio.20080316144536">Quickstart:
517 easy_install numpy
520 easy_install numpy
518 easy_install matplotlib
521 easy_install matplotlib
519
522
520 Make sure you have '@string ipython-argv = ipython -pylab' in @settings. We currently recommend using TkAgg as the backend (it's also the default)</t>
523 Make sure you have '@string ipython-argv = ipython -pylab' in @settings. We currently recommend using TkAgg as the backend (it's also the default)</t>
524 <t tx="vivainio.20080316145539.2">#press alt+i here to generate an array for plotter
525
526 wb.testarr.v = arange(12).reshape(4,3)</t>
521 </tnodes>
527 </tnodes>
522 </leo_file>
528 </leo_file>
General Comments 0
You need to be logged in to leave comments. Login now