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