Show More
@@ -1,99 +1,363 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.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/> | |
10 | <find_panel_settings/> |
|
10 | <find_panel_settings/> | |
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> | |
19 | <v t="vivainio.20080219230342"><vh>slist to leo</vh></v> |
|
19 | <v t="vivainio.20080219230342"><vh>slist to leo</vh></v> | |
20 | </v> |
|
20 | </v> | |
21 | </v> |
|
21 | </v> | |
22 | <v t="vivainio.20080218195413"><vh>Class tests</vh> |
|
22 | <v t="vivainio.20080218195413"><vh>Class tests</vh> | |
23 | <v t="vivainio.20080218200509"><vh>csvr</vh></v> |
|
23 | <v t="vivainio.20080218200509"><vh>csvr</vh></v> | |
24 | <v t="vivainio.20080218191007"><vh>tempfile</vh></v> |
|
24 | <v t="vivainio.20080218191007"><vh>tempfile</vh></v> | |
25 | <v t="vivainio.20080218195413.1"><vh>rfile</vh></v> |
|
25 | <v t="vivainio.20080218195413.1"><vh>rfile</vh></v> | |
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> | |
34 | <t tx="vivainio.20080218184540">?Direct children of this node will be pushed at ipython bridge startup |
|
41 | <t tx="vivainio.20080218184540">?Direct children of this node will be pushed at ipython bridge startup | |
35 |
|
42 | |||
36 | This node itself will *not* be pushed</t> |
|
43 | This node itself will *not* be pushed</t> | |
37 | <t tx="vivainio.20080218184613.1">print "world"</t> |
|
44 | <t tx="vivainio.20080218184613.1">print "world"</t> | |
38 | <t tx="vivainio.20080218190816">def rfile(body,n): |
|
45 | <t tx="vivainio.20080218190816">def rfile(body,n): | |
39 | """ @cl rfile |
|
46 | """ @cl rfile | |
40 |
|
47 | |||
41 | produces a StringIO (file like obj of the rest of the body) """ |
|
48 | produces a StringIO (file like obj of the rest of the body) """ | |
42 |
|
49 | |||
43 | import StringIO |
|
50 | import StringIO | |
44 | return StringIO.StringIO(body) |
|
51 | return StringIO.StringIO(body) | |
45 |
|
52 | |||
46 | def tmpfile(body,n): |
|
53 | def tmpfile(body,n): | |
47 | """ @cl tmpfile |
|
54 | """ @cl tmpfile | |
48 |
|
55 | |||
49 | Produces a temporary file, with node body as contents |
|
56 | Produces a temporary file, with node body as contents | |
50 |
|
57 | |||
51 | """ |
|
58 | """ | |
52 | import tempfile |
|
59 | import tempfile | |
53 | h, fname = tempfile.mkstemp() |
|
60 | h, fname = tempfile.mkstemp() | |
54 | f = open(fname,'w') |
|
61 | f = open(fname,'w') | |
55 | f.write(body) |
|
62 | f.write(body) | |
56 | f.close() |
|
63 | f.close() | |
57 | return fname |
|
64 | return fname | |
58 | </t> |
|
65 | </t> | |
59 | <t tx="vivainio.20080218191007">@cl tmpfile |
|
66 | <t tx="vivainio.20080218191007">@cl tmpfile | |
60 |
|
67 | |||
61 | Hello</t> |
|
68 | Hello</t> | |
62 | <t tx="vivainio.20080218195413">?</t> |
|
69 | <t tx="vivainio.20080218195413">?</t> | |
63 | <t tx="vivainio.20080218195413.1">@cl rfile |
|
70 | <t tx="vivainio.20080218195413.1">@cl rfile | |
64 | These |
|
71 | These | |
65 | lines |
|
72 | lines | |
66 | should |
|
73 | should | |
67 | be |
|
74 | be | |
68 | readable </t> |
|
75 | readable </t> | |
69 | <t tx="vivainio.20080218200031">@others</t> |
|
76 | <t tx="vivainio.20080218200031">@others</t> | |
70 | <t tx="vivainio.20080218200106">def csvdata(body,n): |
|
77 | <t tx="vivainio.20080218200106">def csvdata(body,n): | |
71 | import csv |
|
78 | import csv | |
72 | d = csv.Sniffer().sniff(body) |
|
79 | d = csv.Sniffer().sniff(body) | |
73 | reader = csv.reader(body.splitlines(), dialect = d) |
|
80 | reader = csv.reader(body.splitlines(), dialect = d) | |
74 | return reader</t> |
|
81 | return reader</t> | |
75 | <t tx="vivainio.20080218200509">@cl csvdata |
|
82 | <t tx="vivainio.20080218200509">@cl csvdata | |
76 |
|
83 | |||
77 | a,b,b |
|
84 | a,b,b | |
78 | 1,2,2</t> |
|
85 | 1,2,2</t> | |
79 | <t tx="vivainio.20080218201219"></t> |
|
86 | <t tx="vivainio.20080218201219"></t> | |
80 | <t tx="vivainio.20080218201219.2">@cl |
|
87 | <t tx="vivainio.20080218201219.2">@cl | |
81 | "hello world"</t> |
|
88 | "hello world"</t> | |
82 | <t tx="vivainio.20080219225120">import IPython.genutils |
|
89 | <t tx="vivainio.20080219225120">import IPython.genutils | |
83 | def slist(body,n): |
|
90 | def slist(body,n): | |
84 | return IPython.genutils.SList(body.splitlines()) |
|
91 | return IPython.genutils.SList(body.splitlines()) | |
85 | </t> |
|
92 | </t> | |
86 | <t tx="vivainio.20080219225804">@cl slist |
|
93 | <t tx="vivainio.20080219225804">@cl slist | |
87 | hello |
|
94 | hello | |
88 | world |
|
95 | world | |
89 | on |
|
96 | on | |
90 | many |
|
97 | many | |
91 | lines |
|
98 | lines | |
92 | </t> |
|
99 | </t> | |
93 | <t tx="vivainio.20080219230342">import ipy_leo |
|
100 | <t tx="vivainio.20080219230342">import ipy_leo | |
94 | @ipy_leo.format_for_leo.when_type(IPython.genutils.SList) |
|
101 | @ipy_leo.format_for_leo.when_type(IPython.genutils.SList) | |
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