Show More
@@ -3,7 +3,7 b'' | |||||
3 | <leo_file> |
|
3 | <leo_file> | |
4 | <leo_header file_format="2" tnodes="0" max_tnode_index="0" clone_windows="0"/> |
|
4 | <leo_header file_format="2" tnodes="0" max_tnode_index="0" clone_windows="0"/> | |
5 | <globals body_outline_ratio="0.5"> |
|
5 | <globals body_outline_ratio="0.5"> | |
6 |
<global_window_position top="1 |
|
6 | <global_window_position top="165" left="26" height="621" 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/> | |
@@ -11,8 +11,8 b'' | |||||
11 | <vnodes> |
|
11 | <vnodes> | |
12 | <v t="vivainio.20080218184525"><vh>@chapters</vh></v> |
|
12 | <v t="vivainio.20080218184525"><vh>@chapters</vh></v> | |
13 | <v t="vivainio.20080218184540" a="E"><vh>@ipy-startup</vh> |
|
13 | <v t="vivainio.20080218184540" a="E"><vh>@ipy-startup</vh> | |
14 |
<v t="vivainio.20080218184613.1" |
|
14 | <v t="vivainio.20080218184613.1"><vh>b</vh></v> | |
15 |
<v t="vivainio.20080218200031" |
|
15 | <v t="vivainio.20080218200031"><vh>Some classes P</vh> | |
16 | <v t="vivainio.20080218190816"><vh>File-like access</vh></v> |
|
16 | <v t="vivainio.20080218190816"><vh>File-like access</vh></v> | |
17 | <v t="vivainio.20080218200106"><vh>csv data</vh></v> |
|
17 | <v t="vivainio.20080218200106"><vh>csv data</vh></v> | |
18 | <v t="vivainio.20080219225120"><vh>String list</vh></v> |
|
18 | <v t="vivainio.20080219225120"><vh>String list</vh></v> | |
@@ -26,8 +26,15 b'' | |||||
26 | <v t="vivainio.20080219225804"><vh>strlist</vh></v> |
|
26 | <v t="vivainio.20080219225804"><vh>strlist</vh></v> | |
27 | </v> |
|
27 | </v> | |
28 | <v t="vivainio.20080218201219"><vh>Direct variables</vh> |
|
28 | <v t="vivainio.20080218201219"><vh>Direct variables</vh> | |
29 |
<v t="vivainio.200802 |
|
29 | <v t="vivainio.20080222201226"><vh>NewHeadline</vh></v> | |
|
30 | <v t="vivainio.20080218201219.2"><vh>bar</vh></v> | |||
30 | </v> |
|
31 | </v> | |
|
32 | <v t="vivainio.20080222193236" a="E"><vh>Docs</vh> | |||
|
33 | <v t="vivainio.20080222193236.1" a="TV"><vh>Quick intro</vh></v> | |||
|
34 | </v> | |||
|
35 | <v t="vivainio.20080222202211"><vh>test stuff</vh></v> | |||
|
36 | <v t="vivainio.20080222202211.1"><vh>spam</vh></v> | |||
|
37 | <v t="vivainio.20080222202211.2"><vh>NewHeadline</vh></v> | |||
31 | </vnodes> |
|
38 | </vnodes> | |
32 | <tnodes> |
|
39 | <tnodes> | |
33 | <t tx="vivainio.20080218184525">?</t> |
|
40 | <t tx="vivainio.20080218184525">?</t> | |
@@ -95,5 +102,262 b' lines' | |||||
95 | def format_slist(obj): |
|
102 | def format_slist(obj): | |
96 | return "@cl slist\n" + obj.n |
|
103 | return "@cl slist\n" + obj.n | |
97 | </t> |
|
104 | </t> | |
|
105 | <t tx="vivainio.20080222193236"></t> | |||
|
106 | <t tx="vivainio.20080222193236.1">@wrap | |||
|
107 | @nocolor | |||
|
108 | ||||
|
109 | Introduction | |||
|
110 | ============ | |||
|
111 | ||||
|
112 | The purpose of ILeo, or leo-ipython bridge, is being a two-way communication | |||
|
113 | channel between Leo and IPython. The level of integration is much deeper than | |||
|
114 | conventional integration in IDEs; most notably, you are able to store *data* in | |||
|
115 | Leo nodes, in addition to mere program code. The possibilities of this are | |||
|
116 | endless, and this degree of integration has not been seen previously in the python | |||
|
117 | world. | |||
|
118 | ||||
|
119 | IPython users are accustomed to using things like %edit to produce non-trivial | |||
|
120 | functions/classes (i.e. something that they don't want to enter directly on the | |||
|
121 | interactive prompt, but creating a proper script/module involves too much | |||
|
122 | overhead). In ILeo, this task consists just going to the Leo window, creating a node | |||
|
123 | and writing the code there, and pressing alt+I (push-to-ipython). | |||
|
124 | ||||
|
125 | Obviously, you can save the Leo document as usual - this is a great advantage | |||
|
126 | of ILeo over using %edit, you can save your experimental scripts all at one | |||
|
127 | time, without having to organize them into script/module files (before you | |||
|
128 | really want to, of course!) | |||
|
129 | ||||
|
130 | ||||
|
131 | Installation | |||
|
132 | ============ | |||
|
133 | ||||
|
134 | You need the latest version of Leo, and the development version of IPython (ILeo | |||
|
135 | will be incorporated to IPython 0.8.3. | |||
|
136 | ||||
|
137 | You can get IPython from Launchpad by installing bzr and doing | |||
|
138 | ||||
|
139 | bzr branch lp:ipython | |||
|
140 | ||||
|
141 | and running "setup.py install". | |||
|
142 | ||||
|
143 | You need to enable the 'ipython.py' plugin in Leo: | |||
|
144 | ||||
|
145 | - Help -> Open LeoSettings.leo | |||
|
146 | ||||
|
147 | - Edit @settings-->Plugins-->@enabled-plugins, add/uncomment 'ipython.py' | |||
|
148 | ||||
|
149 | - Restart Leo. Be sure that you have the console window open (start leo.py from console, or double-click leo.py on windows) | |||
|
150 | ||||
|
151 | - Press alt+5 OR alt-x start-ipython to launch IPython in the console that | |||
|
152 | started leo. You can start entering IPython commands normally, and Leo will keep | |||
|
153 | running in the same time. | |||
|
154 | ||||
|
155 | Accessing IPython from Leo | |||
|
156 | ========================== | |||
|
157 | ||||
|
158 | IPython code | |||
|
159 | ------------ | |||
|
160 | ||||
|
161 | Just enter IPython commands on a Leo node and press alt-I to execute | |||
|
162 | push-to-ipython to execute the script in IPython. 'commands' is interpreted | |||
|
163 | loosely here - you can enter function and class definitions, in addition to the | |||
|
164 | things you would usually enter at IPython prompt - calculations, system commands etc. | |||
|
165 | ||||
|
166 | Everything that would be legal to enter on IPython prompt is legal to execute | |||
|
167 | from ILeo. | |||
|
168 | ||||
|
169 | Results will be shows in Leo log window for convenience, in addition to the console. | |||
|
170 | ||||
|
171 | Suppose that a node had the following contents: | |||
|
172 | {{{ | |||
|
173 | 1+2 | |||
|
174 | print "hello" | |||
|
175 | 3+4 | |||
|
176 | ||||
|
177 | def f(x): | |||
|
178 | return x.upper() | |||
|
179 | ||||
|
180 | f('hello world') | |||
|
181 | }}} | |||
|
182 | ||||
|
183 | If you press alt+I on that done, you will see the following in Leo log window (IPython tab): | |||
|
184 | ||||
|
185 | {{{ | |||
|
186 | In: 1+2 | |||
|
187 | <2> 3 | |||
|
188 | In: 3+4 | |||
|
189 | <4> 7 | |||
|
190 | In: f('hello world') | |||
|
191 | <6> 'HELLO WORLD' | |||
|
192 | }}} | |||
|
193 | ||||
|
194 | (numbers like <6> mean IPython output history indices). | |||
|
195 | ||||
|
196 | ||||
|
197 | Plain Python code | |||
|
198 | ----------------- | |||
|
199 | ||||
|
200 | If the headline of the node ends with capital P, alt-I will not run the code | |||
|
201 | through IPython translation mechanism but use the direct python 'exec' statement | |||
|
202 | (in IPython user namespace) to execute the code. It wont be shown in IPython | |||
|
203 | history, and sometimes it is safer (and more efficient) to execute things as | |||
|
204 | plain Python statements. Large class definitions are good candidates for P | |||
|
205 | nodes. | |||
|
206 | ||||
|
207 | Accessing Leo nodes from IPython | |||
|
208 | ================================ | |||
|
209 | ||||
|
210 | The real fun starts when you start entering text to leo nodes, and are using | |||
|
211 | that as data (input/output) for your IPython work. | |||
|
212 | ||||
|
213 | Accessing Leo nodes happens through the variable 'wb' (short for "WorkBook") | |||
|
214 | that exist in the IPython user namespace. Nodes that are directly accessible are | |||
|
215 | the ones that have simple names which could also be Python variable names; | |||
|
216 | 'foo_1' will be accessible directly from IPython, whereas 'my scripts' will not. | |||
|
217 | If you want to access a node with arbitrary headline, add a child node '@a foo' | |||
|
218 | (@a stands for 'anchor'). Then, the parent of '@a foo' is accessible through | |||
|
219 | 'wb.foo'. | |||
|
220 | ||||
|
221 | You can see what nodes are accessible be entering (in IPython) wb.<TAB>. Example: | |||
|
222 | ||||
|
223 | [C:leo/src]|12> wb. | |||
|
224 | wb.b wb.tempfile wb.rfile wb.NewHeadline | |||
|
225 | wb.bar wb.Docs wb.strlist wb.csvr | |||
|
226 | ||||
|
227 | Suppose that we had a node with headline 'spam' and body: | |||
|
228 | ||||
|
229 | ['12',2222+32] | |||
|
230 | ||||
|
231 | we can access it from IPython (or from scripts entered into other Leo nodes!) by doing: | |||
|
232 | ||||
|
233 | C:leo/src]|19> wb.spam.v | |||
|
234 | <19> ['12', 2254] | |||
|
235 | ||||
|
236 | 'v' attribute stands for 'value', which means the node contents will be run | |||
|
237 | through 'eval' and everything you would be able to enter into IPython prompt | |||
|
238 | will be converted to objects. This mechanism can be extended far beyond direct | |||
|
239 | evaluation (see '@cl definitions'). | |||
|
240 | ||||
|
241 | 'v' attribute also has a setter, i.e. you can do: | |||
|
242 | ||||
|
243 | wb.spam.v = "mystring" | |||
|
244 | ||||
|
245 | Which will result in the node 'spam' having the following text: | |||
|
246 | ||||
|
247 | 'mystring' | |||
|
248 | ||||
|
249 | What assignment to 'v' does can be configured through generic functions | |||
|
250 | (simplegeneric module, will be explained later). | |||
|
251 | ||||
|
252 | Besides v, you can set the body text directly through wb.spam.b = | |||
|
253 | "some\nstring", headline by wb.spam.h = 'new_headline' (obviously you must | |||
|
254 | access the node through wb.new_headline from that point onwards), and access the | |||
|
255 | contents as string list (IPython SList) through 'wb.spam.l'. | |||
|
256 | ||||
|
257 | If you do 'wb.foo.v = 12' when node named 'foo' does not exist, the node titled | |||
|
258 | 'foo' will be automatically created and assigned body 12. | |||
|
259 | ||||
|
260 | @cl definitions | |||
|
261 | =============== | |||
|
262 | ||||
|
263 | If the first line in the body text is of the form '@cl sometext', IPython will | |||
|
264 | will evaluate 'sometext' and call the result with the rest of the body when you | |||
|
265 | do 'wb.foo.v'. An example is in place here. Suppose that we have defined a class | |||
|
266 | (I use the term class in a non-python sense here) | |||
|
267 | ||||
|
268 | {{{ | |||
|
269 | def rfile(body,n): | |||
|
270 | """ @cl rfile | |||
|
271 | ||||
|
272 | produces a StringIO (file like obj) of the rest of the body """ | |||
|
273 | ||||
|
274 | import StringIO | |||
|
275 | return StringIO.StringIO(body) | |||
|
276 | }}} | |||
|
277 | ||||
|
278 | Now, let's say you node 'spam' with text | |||
|
279 | ||||
|
280 | {{{ | |||
|
281 | @cl rfile | |||
|
282 | hello | |||
|
283 | world | |||
|
284 | and whatever | |||
|
285 | }}} | |||
|
286 | ||||
|
287 | Now, on IPython, we can do this: | |||
|
288 | ||||
|
289 | {{{ | |||
|
290 | [C:leo/src]|22> f = wb.spam.v | |||
|
291 | [C:leo/src]|23> f | |||
|
292 | <23> <StringIO.StringIO instance at 0x04E7E490> | |||
|
293 | [C:leo/src]|24> f.readline() | |||
|
294 | <24> u'hello\n' | |||
|
295 | [C:leo/src]|25> f.readline() | |||
|
296 | <25> u'world\n' | |||
|
297 | [C:leo/src]|26> f.readline() | |||
|
298 | <26> u'and whatever' | |||
|
299 | [C:leo/src]|27> f.readline() | |||
|
300 | <27> u'' | |||
|
301 | }}} | |||
|
302 | ||||
|
303 | You should declare new @cl types to make ILeo as convenient your problem domain as possible. For example, a "@cl etree" could return the elementtree object for xml content, or | |||
|
304 | ||||
|
305 | Special node types | |||
|
306 | ================== | |||
|
307 | ||||
|
308 | @ipy-startup | |||
|
309 | ------------ | |||
|
310 | ||||
|
311 | If this node exist, the *direct children* of this will be pushed to IPython when | |||
|
312 | ILeo is started (you press alt+5). Use it to push your own @cl definitions etc. | |||
|
313 | The contents of of the node itself will be ignored. | |||
|
314 | ||||
|
315 | @ipy-results | |||
|
316 | ------------ | |||
|
317 | ||||
|
318 | When you create a new node (wb.foo.v = 'stuff'), the node foo will be created as | |||
|
319 | a child of this node. If @ipy-results does not exist, the new node will be created after the currently selected node. | |||
|
320 | ||||
|
321 | @a nodes | |||
|
322 | -------- | |||
|
323 | ||||
|
324 | You can attach these as children of existing nodes to provide a way to access | |||
|
325 | nodes with arbitrary headlines, or to provide aliases to other nodes. If | |||
|
326 | multiple @a nodes are attached as children of a node, all the names can be used | |||
|
327 | to access the same object. | |||
|
328 | ||||
|
329 | Acknowledgements & History | |||
|
330 | ========================== | |||
|
331 | ||||
|
332 | This idea got started when I (Ville) saw this post by Edward Ream (the author of | |||
|
333 | Leo) on IPython developer mailing list: | |||
|
334 | ||||
|
335 | http://lists.ipython.scipy.org/pipermail/ipython-dev/2008-January/003551.html | |||
|
336 | ||||
|
337 | I was using FreeMind as mind mapping software, and so I had an immediate use | |||
|
338 | case for Leo (which, incidentally, is superior to FreeMind as mind mapper). The | |||
|
339 | wheels started rolling, I got obsessed with the power of this concept | |||
|
340 | (everything clicked together), and Edwards excitement paralleled mine. | |||
|
341 | Everything was mind-bogglingly easy/trivial, something that is typical of all | |||
|
342 | revolutionary technologies (think Python here). | |||
|
343 | ||||
|
344 | The discussion that "built" ILeo is here: | |||
|
345 | http://sourceforge.net/forum/forum.php?thread_id=1911662&forum_id=10226 | |||
|
346 | ||||
|
347 | </t> | |||
|
348 | <t tx="vivainio.20080222201226">1+2 | |||
|
349 | print "hello" | |||
|
350 | 3+4 | |||
|
351 | ||||
|
352 | def f(x): | |||
|
353 | return x.upper() | |||
|
354 | ||||
|
355 | f('hello world')</t> | |||
|
356 | <t tx="vivainio.20080222202211"></t> | |||
|
357 | <t tx="vivainio.20080222202211.1">@cl rfile | |||
|
358 | hello | |||
|
359 | world | |||
|
360 | and whatever</t> | |||
|
361 | <t tx="vivainio.20080222202211.2"></t> | |||
98 | </tnodes> |
|
362 | </tnodes> | |
99 | </leo_file> |
|
363 | </leo_file> |
General Comments 0
You need to be logged in to leave comments.
Login now