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