##// END OF EJS Templates
ipy_leo: doc improvements
Ville M. Vainio -
Show More
@@ -203,7 +203,7 b' class LeoNode(object, UserDict.DictMixin):'
203
203
204 d = p.v.t.unknownAttributes.setdefault('ipython', {})
204 d = p.v.t.unknownAttributes.setdefault('ipython', {})
205 return d
205 return d
206 uA = property(__get_uA, doc = "Access persistent unknownAttributes of node'")
206 uA = property(__get_uA, doc = "Access persistent unknownAttributes of node")
207
207
208
208
209 class LeoWorkbook:
209 class LeoWorkbook:
@@ -2,20 +2,35 b''
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.5">
5 <globals body_outline_ratio="0.307814992026">
6 <global_window_position top="261" left="404" height="621" width="1280"/>
6 <global_window_position top="140" left="145" 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>
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>
14 <v t="vivainio.20080222193236.1" a="E"><vh>Documentation</vh>
15 <v t="vivainio.20080223133858"><vh>Introduction</vh></v>
16 <v t="vivainio.20080223133922"><vh>Installation</vh></v>
17 <v t="vivainio.20080223133947"><vh>Accessing IPython from Leo</vh></v>
18 <v t="vivainio.20080223134018" a="TV"><vh>Accessing Leo nodes from IPython</vh></v>
19 <v t="vivainio.20080223134100"><vh>Cl definitions</vh></v>
20 <v t="vivainio.20080223134118"><vh>Special node types</vh></v>
21 <v t="vivainio.20080223134433"><vh>Custom push</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>
24 </v>
25 </v>
26 </v>
12 <v t="vivainio.20080218184525"><vh>@chapters</vh></v>
27 <v t="vivainio.20080218184525"><vh>@chapters</vh></v>
13 <v t="vivainio.20080223133721" a="E"><vh>@settings</vh>
28 <v t="vivainio.20080223133721"><vh>@settings</vh>
14 <v t="vivainio.20080223133721.1"><vh>@enabled-plugins</vh></v>
29 <v t="vivainio.20080223133721.1"><vh>@enabled-plugins</vh></v>
15 </v>
30 </v>
16 <v t="vivainio.20080218184540" a="E"><vh>@ipy-startup</vh>
31 <v t="vivainio.20080218184540" a="E"><vh>@ipy-startup</vh>
17 <v t="vivainio.20080218184613.1"><vh>b</vh></v>
32 <v t="vivainio.20080218184613.1"><vh>b</vh></v>
18 <v t="vivainio.20080218200031" a="E"><vh>Some classes P</vh>
33 <v t="vivainio.20080218200031"><vh>Some classes P</vh>
19 <v t="vivainio.20080218190816"><vh>File-like access</vh></v>
34 <v t="vivainio.20080218190816"><vh>File-like access</vh></v>
20 <v t="vivainio.20080218200106"><vh>csv data</vh></v>
35 <v t="vivainio.20080218200106"><vh>csv data</vh></v>
21 <v t="vivainio.20080219225120"><vh>String list</vh></v>
36 <v t="vivainio.20080219225120"><vh>String list</vh></v>
@@ -32,22 +47,16 b''
32 <v t="vivainio.20080222201226"><vh>NewHeadline</vh></v>
47 <v t="vivainio.20080222201226"><vh>NewHeadline</vh></v>
33 <v t="vivainio.20080218201219.2"><vh>bar</vh></v>
48 <v t="vivainio.20080218201219.2"><vh>bar</vh></v>
34 </v>
49 </v>
35 <v t="vivainio.20080222193236" a="E"><vh>Docs</vh>
50 <v t="vivainio.20080222202211"><vh>test stuff</vh></v>
36 <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.20080223134136"><vh>@nosent ileointro.txt</vh>
51 <v t="vivainio.20080223142403" a="E"><vh>@ipy-results</vh>
37 <v t="vivainio.20080222193236.1" a="E"><vh>Quick intro</vh>
52 <v t="vivainio.20080223142403.1"><vh>foo</vh></v>
38 <v t="vivainio.20080223133858"><vh>Introduction</vh></v>
53 <v t="vivainio.20080223152600" a="E"><vh>baz</vh>
39 <v t="vivainio.20080223133922"><vh>Installation</vh></v>
54 <v t="vivainio.20080223174603"><vh>@k hello</vh></v>
40 <v t="vivainio.20080223133947"><vh>Accessing IPython from Leo</vh></v>
41 <v t="vivainio.20080223134018"><vh>Accessing Leo nodes from IPython</vh></v>
42 <v t="vivainio.20080223134100"><vh>Cl definitions</vh></v>
43 <v t="vivainio.20080223134118"><vh>Special node types</vh></v>
44 <v t="vivainio.20080223134433" a="TV"><vh>Custom push</vh></v>
45 <v t="vivainio.20080223134136"><vh>Acknowledgements and history</vh></v>
46 </v>
55 </v>
47 </v>
56 </v>
57 <v t="vivainio.20080222202211.1"><vh>spam</vh>
58 <v t="vivainio.20080223180139"><vh>foo bar</vh></v>
48 </v>
59 </v>
49 <v t="vivainio.20080222202211"><vh>test stuff</vh></v>
50 <v t="vivainio.20080222202211.1"><vh>spam</vh></v>
51 <v t="vivainio.20080222202211.2"><vh>NewHeadline</vh></v>
60 <v t="vivainio.20080222202211.2"><vh>NewHeadline</vh></v>
52 </vnodes>
61 </vnodes>
53 <tnodes>
62 <tnodes>
@@ -116,7 +125,7 b' lines'
116 def format_slist(obj):
125 def format_slist(obj):
117 return "@cl slist\n" + obj.n
126 return "@cl slist\n" + obj.n
118 </t>
127 </t>
119 <t tx="vivainio.20080222193236"></t>
128 <t tx="vivainio.20080222193236">?</t>
120 <t tx="vivainio.20080222193236.1">@wrap
129 <t tx="vivainio.20080222193236.1">@wrap
121 @nocolor</t>
130 @nocolor</t>
122 <t tx="vivainio.20080222201226">1+2
131 <t tx="vivainio.20080222201226">1+2
@@ -128,7 +137,7 b' def f(x):'
128
137
129 f('hello world')</t>
138 f('hello world')</t>
130 <t tx="vivainio.20080222202211"></t>
139 <t tx="vivainio.20080222202211"></t>
131 <t tx="vivainio.20080222202211.1">@cl rfile
140 <t tx="vivainio.20080222202211.1" ipython="7d71005506636f6f7264737101284b0c4bde747102732e">@cl rfile
132 hello
141 hello
133 world
142 world
134 and whatever</t>
143 and whatever</t>
@@ -178,6 +187,8 b" You need to enable the 'ipython.py' plugin in Leo:"
178
187
179 - Edit @settings--&gt;Plugins--&gt;@enabled-plugins, add/uncomment 'ipython.py'
188 - Edit @settings--&gt;Plugins--&gt;@enabled-plugins, add/uncomment 'ipython.py'
180
189
190 - Alternatively, you can add @settings--&gt;@enabled-plugins with body ipython.py to your leo document.
191
181 - Restart Leo. Be sure that you have the console window open (start leo.py from console, or double-click leo.py on windows)
192 - Restart Leo. Be sure that you have the console window open (start leo.py from console, or double-click leo.py on windows)
182
193
183 - Press alt+5 OR alt-x start-ipython to launch IPython in the console that
194 - Press alt+5 OR alt-x start-ipython to launch IPython in the console that
@@ -192,9 +203,10 b' IPython code'
192 ------------
203 ------------
193
204
194 Just enter IPython commands on a Leo node and press alt-I to execute
205 Just enter IPython commands on a Leo node and press alt-I to execute
195 push-to-ipython to execute the script in IPython. 'commands' is interpreted
206 push-to-ipython in order to execute the script in IPython. 'commands' is
196 loosely here - you can enter function and class definitions, in addition to the
207 interpreted loosely here - you can enter function and class definitions, in
197 things you would usually enter at IPython prompt - calculations, system commands etc.
208 addition to the things you would usually enter at IPython prompt - calculations,
209 system commands etc.
198
210
199 Everything that would be legal to enter on IPython prompt is legal to execute
211 Everything that would be legal to enter on IPython prompt is legal to execute
200 from ILeo.
212 from ILeo.
@@ -213,7 +225,7 b' def f(x):'
213 f('hello world')
225 f('hello world')
214 }}}
226 }}}
215
227
216 If you press alt+I on that done, you will see the following in Leo log window (IPython tab):
228 If you press alt+I on that node, you will see the following in Leo log window (IPython tab):
217
229
218 {{{
230 {{{
219 In: 1+2
231 In: 1+2
@@ -224,7 +236,8 b" In: f('hello world')"
224 &lt;6&gt; 'HELLO WORLD'
236 &lt;6&gt; 'HELLO WORLD'
225 }}}
237 }}}
226
238
227 (numbers like &lt;6&gt; mean IPython output history indices).
239 (numbers like &lt;6&gt; mean IPython output history indices; the actual object can be
240 referenced with _6 as usual in IPython).
228
241
229
242
230 Plain Python code
243 Plain Python code
@@ -257,6 +270,14 b' You can see what nodes are accessible be entering (in IPython) wb.&lt;TAB&gt;. E'
257 [C:leo/src]|12&gt; wb.
270 [C:leo/src]|12&gt; wb.
258 wb.b wb.tempfile wb.rfile wb.NewHeadline
271 wb.b wb.tempfile wb.rfile wb.NewHeadline
259 wb.bar wb.Docs wb.strlist wb.csvr
272 wb.bar wb.Docs wb.strlist wb.csvr
273 [C:leo/src]|12&gt; wb.tempfile
274 &lt;12&gt; &lt;ipy_leo.LeoNode object at 0x044B6D90&gt;
275
276 So here, we meet the 'LeoNode' class that is your key to manipulating Leo
277 content from IPython!
278
279 LeoNode
280 -------
260
281
261 Suppose that we had a node with headline 'spam' and body:
282 Suppose that we had a node with headline 'spam' and body:
262
283
@@ -281,27 +302,74 b" Which will result in the node 'spam' having the following text:"
281 'mystring'
302 'mystring'
282
303
283 What assignment to 'v' does can be configured through generic functions
304 What assignment to 'v' does can be configured through generic functions
284 (simplegeneric module, will be explained later).
305 ('simplegeneric' module, will be explained later).
306
307 Besides v, you can set the body text directly through
308
309 wb.spam.b = "some\nstring",
310
311 headline by
312
313 wb.spam.h = 'new_headline'
285
314
286 Besides v, you can set the body text directly through wb.spam.b =
315 (obviously you must access the node through wb.new_headline from that point
287 "some\nstring", headline by wb.spam.h = 'new_headline' (obviously you must
316 onwards), and access the contents as string list (IPython SList) through
288 access the node through wb.new_headline from that point onwards), and access the
317 'wb.spam.l'.
289 contents as string list (IPython SList) through 'wb.spam.l'.
290
318
291 If you do 'wb.foo.v = 12' when node named 'foo' does not exist, the node titled
319 If you do 'wb.foo.v = 12' when node named 'foo' does not exist, the node titled
292 'foo' will be automatically created and assigned body 12.
320 'foo' will be automatically created and assigned body 12.
321
322 LeoNode also supports go() that focuses the node in the Leo window, ipush that
323 simulates pressing alt+I on the node.
324
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::
329
330 [C:leo/src]|12&gt; wb.spam.uA['coords'] = (12,222)
331 [C:leo/src]|13&gt; wb.spam.uA
332 &lt;13&gt; {'coords': (12, 222)}
333
334 Accessing children with iteration and dict notation
335 ---------------------------------------------------
336
337 Sometimes, you may want to treat a node as a 'database', where the nodes
338 children represent elements in the database. You can create a new child node for
339 node 'spam', with headline 'foo bar' like this:
340
341 wb.spam['foo bar'] = "Hello"
342
343 And assign a new value for it by doing
344
345 wb.spam['foo bar'].v = "Hello again"
346
347 Note how you can't use .v when you first create the node - i.e. the node needs
348 to be initialized by simple assignment, that will be interpreted as assignment
349 to '.v'. This is a conscious design choice.
350
351 If you try to do wb.spam['bar'] = 'Hello', ILeo will assign '@k bar' as the
352 headline for the child instead, because 'bar' is a legal python name (and as
353 such would be incorporated in the workbook namespace). This is done to avoid
354 crowding the workbook namespace with extraneous items. The item will still be
355 accessible as wb.spam['bar']
356
357 LeoNodes are iterable, so to see the headlines of all the children of 'spam' do:
358
359 for n in wb.spam:
360 print n.h
293 </t>
361 </t>
294 <t tx="vivainio.20080223134100">
362 <t tx="vivainio.20080223134100">
295 @cl definitions
363 @cl definitions
296 ===============
364 ===============
297
365
298 If the first line in the body text is of the form '@cl sometext', IPython will
366 If the first line in the body text is of the form '@cl sometext', IPython will
299 will evaluate 'sometext' and call the result with the rest of the body when you
367 evaluate 'sometext' and call the result with the rest of the body when you do
300 do 'wb.foo.v'. An example is in place here. Suppose that we have defined a class
368 'wb.foo.v'. An example is in place here. Suppose that we have defined a class (I
301 (I use the term class in a non-python sense here)
369 use the term class in a non-python sense here)
302
370
303 {{{
371 {{{
304 def rfile(body,n):
372 def rfile(body,node):
305 """ @cl rfile
373 """ @cl rfile
306
374
307 produces a StringIO (file like obj) of the rest of the body """
375 produces a StringIO (file like obj) of the rest of the body """
@@ -310,7 +378,10 b' def rfile(body,n):'
310 return StringIO.StringIO(body)
378 return StringIO.StringIO(body)
311 }}}
379 }}}
312
380
313 Now, let's say you node 'spam' with text
381 (note that node is ignored here - but it could be used to access headline,
382 children etc.),
383
384 Now, let's say you have node 'spam' with text
314
385
315 {{{
386 {{{
316 @cl rfile
387 @cl rfile
@@ -319,7 +390,7 b' world'
319 and whatever
390 and whatever
320 }}}
391 }}}
321
392
322 Now, on IPython, we can do this:
393 Now, in IPython, we can do this:
323
394
324 {{{
395 {{{
325 [C:leo/src]|22&gt; f = wb.spam.v
396 [C:leo/src]|22&gt; f = wb.spam.v
@@ -335,7 +406,9 b' Now, on IPython, we can do this:'
335 &lt;27&gt; u''
406 &lt;27&gt; u''
336 }}}
407 }}}
337
408
338 You should declare new @cl types to make ILeo as convenient your problem domain as possible. For example, a "@cl etree" could return the elementtree object for xml content, or
409 You should declare new @cl types to make ILeo as convenient your problem domain
410 as possible. For example, a "@cl etree" could return the elementtree object for
411 xml content.
339 </t>
412 </t>
340 <t tx="vivainio.20080223134118">
413 <t tx="vivainio.20080223134118">
341 Special node types
414 Special node types
@@ -389,21 +462,47 b' Declaring custom push-to-ipython handlers'
389 Sometimes, you might want to configure what alt+I on a node does. You can do
462 Sometimes, you might want to configure what alt+I on a node does. You can do
390 that by creating your own push function and expose it using
463 that by creating your own push function and expose it using
391 ipy_leo.expose_ileo_push(f, priority). The function should check whether the
464 ipy_leo.expose_ileo_push(f, priority). The function should check whether the
392 node should by handled by it and raise IPython.ipapi.TryNext if it will not do
465 node should by handled by the function and raise IPython.ipapi.TryNext if it
393 the handling.
466 will not do the handling, giving the next function in the chain a chance to see
467 whether it should handle the push.
394
468
395 This would print an uppercase version of node body if the node headline ends
469 This example would print an uppercase version of node body if the node headline ends
396 with U (yes, this is completely useless!)
470 with U (yes, this is completely useless!):
397
471
472 {{{
398 def push_upcase(node):
473 def push_upcase(node):
399 if not node.h.endswith('U'):
474 if not node.h.endswith('U'):
400 raise TryNext
475 raise TryNext
401 print node.b.upper()
476 print node.b.upper()
402
477
403 expose_ileo_push(push_upcase, 12)
478 ipy_leo.expose_ileo_push(push_upcase, 12)
479 }}}
404
480
405 (the priority should be between 0-100 - typically, you don't need to care about
481 (the priority should be between 0-100 - typically, you don't need to care about
406 it and can omit the argument altogether)
482 it and can usually omit the argument altogether)
483 </t>
484 <t tx="vivainio.20080223142207">
485 Example code snippets
486 =====================
487
488 Get list of all headlines of all the nodes in leo:
489
490 [node.h for node in wb]
491
492 Create node with headline 'baz', empty body:
493 wb.baz
494
495 Create 10 child nodes for baz, where i is headline and 'Hello ' + i is body:
496
497 for i in range(10):
498 wb.baz[i] = 'Hello %d' % i
499
500
407 </t>
501 </t>
502 <t tx="vivainio.20080223142403"></t>
503 <t tx="vivainio.20080223142403.1">12</t>
504 <t tx="vivainio.20080223152600"></t>
505 <t tx="vivainio.20080223174603"></t>
506 <t tx="vivainio.20080223180139">'hello again'</t>
408 </tnodes>
507 </tnodes>
409 </leo_file>
508 </leo_file>
General Comments 0
You need to be logged in to leave comments. Login now