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