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