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