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