Show More
@@ -1,427 +1,419 b'' | |||
|
1 | 1 | .. _ipython_directive: |
|
2 | 2 | |
|
3 | 3 | ======================== |
|
4 | 4 | Ipython Sphinx Directive |
|
5 | 5 | ======================== |
|
6 | 6 | |
|
7 | 7 | The ipython directive is a stateful ipython shell for embedding in |
|
8 | 8 | sphinx documents. It knows about standard ipython prompts, and |
|
9 | 9 | extracts the input and output lines. These prompts will be renumbered |
|
10 | 10 | starting at ``1``. The inputs will be fed to an embedded ipython |
|
11 | 11 | interpreter and the outputs from that interpreter will be inserted as |
|
12 | 12 | well. For example, code blocks like the following:: |
|
13 | 13 | |
|
14 | 14 | .. ipython:: |
|
15 | 15 | |
|
16 | 16 | In [136]: x = 2 |
|
17 | 17 | |
|
18 | 18 | In [137]: x**3 |
|
19 | 19 | Out[137]: 8 |
|
20 | 20 | |
|
21 | 21 | will be rendered as |
|
22 | 22 | |
|
23 | 23 | .. ipython:: |
|
24 | 24 | |
|
25 | 25 | In [136]: x = 2 |
|
26 | 26 | |
|
27 | 27 | In [137]: x**3 |
|
28 | 28 | Out[137]: 8 |
|
29 | 29 | |
|
30 | 30 | .. note:: |
|
31 | 31 | |
|
32 | 32 | This tutorial should be read side-by-side with the Sphinx source |
|
33 | 33 | for this document because otherwise you will see only the rendered |
|
34 | 34 | output and not the code that generated it. Excepting the example |
|
35 | 35 | above, we will not in general be showing the literal ReST in this |
|
36 | 36 | document that generates the rendered output. |
|
37 | 37 | |
|
38 | 38 | |
|
39 | 39 | The state from previous sessions is stored, and standard error is |
|
40 | 40 | trapped. At doc build time, ipython's output and std err will be |
|
41 | 41 | inserted, and prompts will be renumbered. So the prompt below should |
|
42 | 42 | be renumbered in the rendered docs, and pick up where the block above |
|
43 | 43 | left off. |
|
44 | 44 | |
|
45 | 45 | .. ipython:: |
|
46 | 46 | |
|
47 | 47 | In [138]: z = x*3 # x is recalled from previous block |
|
48 | 48 | |
|
49 | 49 | In [139]: z |
|
50 | 50 | Out[139]: 6 |
|
51 | 51 | |
|
52 | 52 | In [140]: print z |
|
53 | 53 | --------> print(z) |
|
54 | 54 | 6 |
|
55 | 55 | |
|
56 | 56 | In [141]: q = z[) # this is a syntax error -- we trap ipy exceptions |
|
57 | 57 | ------------------------------------------------------------ |
|
58 | 58 | File "<ipython console>", line 1 |
|
59 | 59 | q = z[) # this is a syntax error -- we trap ipy exceptions |
|
60 | 60 | ^ |
|
61 | 61 | SyntaxError: invalid syntax |
|
62 | 62 | |
|
63 | 63 | |
|
64 | 64 | The embedded interpreter supports some limited markup. For example, |
|
65 | 65 | you can put comments in your ipython sessions, which are reported |
|
66 | 66 | verbatim. There are some handy "pseudo-decorators" that let you |
|
67 | 67 | doctest the output. The inputs are fed to an embedded ipython |
|
68 | 68 | session and the outputs from the ipython session are inserted into |
|
69 | 69 | your doc. If the output in your doc and in the ipython session don't |
|
70 | 70 | match on a doctest assertion, an error will be |
|
71 | 71 | |
|
72 | 72 | |
|
73 | 73 | .. ipython:: |
|
74 | 74 | |
|
75 | 75 | In [1]: x = 'hello world' |
|
76 | 76 | |
|
77 | 77 | # this will raise an error if the ipython output is different |
|
78 | 78 | @doctest |
|
79 | 79 | In [2]: x.upper() |
|
80 | 80 | Out[2]: 'HELLO WORLD' |
|
81 | 81 | |
|
82 | 82 | # some readline features cannot be supported, so we allow |
|
83 | 83 | # "verbatim" blocks, which are dumped in verbatim except prompts |
|
84 | 84 | # are continuously numbered |
|
85 | 85 | @verbatim |
|
86 | 86 | In [3]: x.st<TAB> |
|
87 | 87 | x.startswith x.strip |
|
88 | 88 | |
|
89 | 89 | |
|
90 | 90 | Multi-line input is supported. |
|
91 | 91 | |
|
92 | 92 | .. ipython:: |
|
93 | 93 | |
|
94 | 94 | In [130]: url = 'http://ichart.finance.yahoo.com/table.csv?s=CROX\ |
|
95 | 95 | .....: &d=9&e=22&f=2009&g=d&a=1&br=8&c=2006&ignore=.csv' |
|
96 | 96 | |
|
97 | 97 | In [131]: print url.split('&') |
|
98 | 98 | --------> print(url.split('&')) |
|
99 | 99 | ['http://ichart.finance.yahoo.com/table.csv?s=CROX', 'd=9', 'e=22', |
|
100 | 100 | |
|
101 | 101 | You can do doctesting on multi-line output as well. Just be careful |
|
102 | 102 | when using non-deterministic inputs like random numbers in the ipython |
|
103 | 103 | directive, because your inputs are ruin through a live interpreter, so |
|
104 | 104 | if you are doctesting random output you will get an error. Here we |
|
105 | 105 | "seed" the random number generator for deterministic output, and we |
|
106 | 106 | suppress the seed line so it doesn't show up in the rendered output |
|
107 | 107 | |
|
108 | 108 | .. ipython:: |
|
109 | 109 | |
|
110 | 110 | In [133]: import numpy.random |
|
111 | 111 | |
|
112 | 112 | @suppress |
|
113 | 113 | In [134]: numpy.random.seed(2358) |
|
114 | 114 | |
|
115 | 115 | @doctest |
|
116 | 116 | In [135]: numpy.random.rand(10,2) |
|
117 | 117 | Out[135]: |
|
118 | 118 | array([[ 0.64524308, 0.59943846], |
|
119 | 119 | [ 0.47102322, 0.8715456 ], |
|
120 | 120 | [ 0.29370834, 0.74776844], |
|
121 | 121 | [ 0.99539577, 0.1313423 ], |
|
122 | 122 | [ 0.16250302, 0.21103583], |
|
123 | 123 | [ 0.81626524, 0.1312433 ], |
|
124 | 124 | [ 0.67338089, 0.72302393], |
|
125 | 125 | [ 0.7566368 , 0.07033696], |
|
126 | 126 | [ 0.22591016, 0.77731835], |
|
127 | 127 | [ 0.0072729 , 0.34273127]]) |
|
128 | 128 | |
|
129 | 129 | |
|
130 | 130 | Another demonstration of multi-line input and output |
|
131 | 131 | |
|
132 | 132 | .. ipython:: |
|
133 | 133 | |
|
134 | 134 | In [106]: print x |
|
135 | 135 | --------> print(x) |
|
136 | 136 | jdh |
|
137 | 137 | |
|
138 | 138 | In [109]: for i in range(10): |
|
139 | 139 | .....: print i |
|
140 | 140 | .....: |
|
141 | 141 | .....: |
|
142 | 142 | 0 |
|
143 | 143 | 1 |
|
144 | 144 | 2 |
|
145 | 145 | 3 |
|
146 | 146 | 4 |
|
147 | 147 | 5 |
|
148 | 148 | 6 |
|
149 | 149 | 7 |
|
150 | 150 | 8 |
|
151 | 151 | 9 |
|
152 | 152 | |
|
153 | 153 | |
|
154 | 154 | Most of the "pseudo-decorators" can be used an options to ipython |
|
155 | 155 | mode. For example, to setup matplotlib pylab but suppress the output, |
|
156 | 156 | you can do. When using the matplotlib ``use`` directive, it should |
|
157 | 157 | occur before any import of pylab. This will not show up in the |
|
158 | 158 | rendered docs, but the commands will be executed in the embedded |
|
159 | 159 | interpreter and subsequent line numbers will be incremented to reflect |
|
160 | 160 | the inputs:: |
|
161 | 161 | |
|
162 | 162 | |
|
163 | 163 | .. ipython:: |
|
164 | 164 | :suppress: |
|
165 | 165 | |
|
166 | 166 | In [144]: from pylab import * |
|
167 | 167 | |
|
168 | 168 | In [145]: ion() |
|
169 | 169 | |
|
170 | 170 | .. ipython:: |
|
171 | 171 | :suppress: |
|
172 | 172 | |
|
173 | 173 | In [144]: from pylab import * |
|
174 | 174 | |
|
175 | 175 | In [145]: ion() |
|
176 | 176 | |
|
177 | 177 | Likewise, you can set ``:doctest:`` or ``:verbatim:`` to apply these |
|
178 | 178 | settings to the entire block. For example, |
|
179 | 179 | |
|
180 | 180 | .. ipython:: |
|
181 | 181 | :verbatim: |
|
182 | 182 | |
|
183 | 183 | In [9]: cd mpl/examples/ |
|
184 | 184 | /home/jdhunter/mpl/examples |
|
185 | 185 | |
|
186 | 186 | In [10]: pwd |
|
187 | 187 | Out[10]: '/home/jdhunter/mpl/examples' |
|
188 | 188 | |
|
189 | 189 | |
|
190 | 190 | In [14]: cd mpl/examples/<TAB> |
|
191 | 191 | mpl/examples/animation/ mpl/examples/misc/ |
|
192 | 192 | mpl/examples/api/ mpl/examples/mplot3d/ |
|
193 | 193 | mpl/examples/axes_grid/ mpl/examples/pylab_examples/ |
|
194 | 194 | mpl/examples/event_handling/ mpl/examples/widgets |
|
195 | 195 | |
|
196 | 196 | In [14]: cd mpl/examples/widgets/ |
|
197 | 197 | /home/msierig/mpl/examples/widgets |
|
198 | 198 | |
|
199 | 199 | In [15]: !wc * |
|
200 | 200 | 2 12 77 README.txt |
|
201 | 201 | 40 97 884 buttons.py |
|
202 | 202 | 26 90 712 check_buttons.py |
|
203 | 203 | 19 52 416 cursor.py |
|
204 | 204 | 180 404 4882 menu.py |
|
205 | 205 | 16 45 337 multicursor.py |
|
206 | 206 | 36 106 916 radio_buttons.py |
|
207 | 207 | 48 226 2082 rectangle_selector.py |
|
208 | 208 | 43 118 1063 slider_demo.py |
|
209 | 209 | 40 124 1088 span_selector.py |
|
210 | 210 | 450 1274 12457 total |
|
211 | 211 | |
|
212 | 212 | You can create one or more pyplot plots and insert them with the |
|
213 | 213 | ``@savefig`` decorator. |
|
214 | 214 | |
|
215 | 215 | .. ipython:: |
|
216 | 216 | |
|
217 | 217 | @savefig plot_simple.png width=4in |
|
218 | 218 | In [151]: plot([1,2,3]); |
|
219 | 219 | |
|
220 | 220 | # use a semicolon to suppress the output |
|
221 | 221 | @savefig hist_simple.png width=4in |
|
222 | 222 | In [151]: hist(np.random.randn(10000), 100); |
|
223 | 223 | |
|
224 | 224 | In a subsequent session, we can update the current figure with some |
|
225 | 225 | text, and then resave |
|
226 | 226 | |
|
227 | 227 | .. ipython:: |
|
228 | 228 | |
|
229 | 229 | |
|
230 | 230 | In [151]: ylabel('number') |
|
231 | 231 | |
|
232 | 232 | In [152]: title('normal distribution') |
|
233 | 233 | |
|
234 | 234 | @savefig hist_with_text.png width=4in |
|
235 | 235 | In [153]: grid(True) |
|
236 | 236 | |
|
237 | 237 | You can also have function definitions included in the source. |
|
238 | 238 | |
|
239 | 239 | .. ipython:: |
|
240 | 240 | |
|
241 | 241 | In [3]: def square(x): |
|
242 | 242 | ...: """ |
|
243 | 243 | ...: An overcomplicated square function as an example. |
|
244 | 244 | ...: """ |
|
245 | 245 | ...: if x < 0: |
|
246 | 246 | ...: x = abs(x) |
|
247 | 247 | ...: y = x * x |
|
248 | 248 | ...: return y |
|
249 | 249 | ...: |
|
250 | 250 | |
|
251 | 251 | Then call it from a subsequent section. |
|
252 | 252 | |
|
253 | 253 | .. ipython:: |
|
254 | 254 | |
|
255 | 255 | In [4]: square(3) |
|
256 | 256 | Out [4]: 9 |
|
257 | 257 | |
|
258 | 258 | In [5]: square(-2) |
|
259 | 259 | Out [5]: 4 |
|
260 | 260 | |
|
261 | 261 | |
|
262 | 262 | Writing Pure Python Code |
|
263 | 263 | ------------------------ |
|
264 | 264 | |
|
265 | 265 | Pure python code is supported by the optional argument `python`. In this pure |
|
266 | 266 | python syntax you do not include the output from the python interpreter. The |
|
267 | 267 | following markup:: |
|
268 | 268 | |
|
269 | 269 | .. ipython:: python |
|
270 | 270 | |
|
271 | 271 | foo = 'bar' |
|
272 | 272 | print foo |
|
273 | 273 | foo = 2 |
|
274 | 274 | foo**2 |
|
275 | 275 | |
|
276 | 276 | Renders as |
|
277 | 277 | |
|
278 | 278 | .. ipython:: python |
|
279 | 279 | |
|
280 | 280 | foo = 'bar' |
|
281 | 281 | print foo |
|
282 | 282 | foo = 2 |
|
283 | 283 | foo**2 |
|
284 | 284 | |
|
285 | 285 | We can even plot from python, using the savefig decorator, as well as, suppress |
|
286 | 286 | output with a semicolon |
|
287 | 287 | |
|
288 | 288 | .. ipython:: python |
|
289 | 289 | |
|
290 | 290 | @savefig plot_simple_python.png width=4in |
|
291 | 291 | plot([1,2,3]); |
|
292 | 292 | |
|
293 | 293 | Similarly, std err is inserted |
|
294 | 294 | |
|
295 | 295 | .. ipython:: python |
|
296 | 296 | |
|
297 | 297 | foo = 'bar' |
|
298 | 298 | foo[) |
|
299 | 299 | |
|
300 | 300 | Comments are handled and state is preserved |
|
301 | 301 | |
|
302 | 302 | .. ipython:: python |
|
303 | 303 | |
|
304 | 304 | # comments are handled |
|
305 | 305 | print foo |
|
306 | 306 | |
|
307 | 307 | If you don't see the next code block then the options work. |
|
308 | 308 | |
|
309 | 309 | .. ipython:: python |
|
310 | 310 | :suppress: |
|
311 | 311 | |
|
312 | 312 | ioff() |
|
313 | 313 | ion() |
|
314 | 314 | |
|
315 | 315 | Multi-line input is handled. |
|
316 | 316 | |
|
317 | 317 | .. ipython:: python |
|
318 | 318 | |
|
319 | 319 | line = 'Multi\ |
|
320 | 320 | line &\ |
|
321 | 321 | support &\ |
|
322 | 322 | works' |
|
323 | 323 | print line.split('&') |
|
324 | 324 | |
|
325 | 325 | Functions definitions are correctly parsed |
|
326 | 326 | |
|
327 | 327 | .. ipython:: python |
|
328 | 328 | |
|
329 | 329 | def square(x): |
|
330 | 330 | """ |
|
331 | 331 | An overcomplicated square function as an example. |
|
332 | 332 | """ |
|
333 | 333 | if x < 0: |
|
334 | 334 | x = abs(x) |
|
335 | 335 | y = x * x |
|
336 | 336 | return y |
|
337 | 337 | |
|
338 | 338 | And persist across sessions |
|
339 | 339 | |
|
340 | 340 | .. ipython:: python |
|
341 | 341 | |
|
342 | 342 | print square(3) |
|
343 | 343 | print square(-2) |
|
344 | 344 | |
|
345 | 345 | Pretty much anything you can do with the ipython code, you can do with |
|
346 | 346 | with a simple python script. Obviously, though it doesn't make sense |
|
347 | 347 | to use the doctest option. |
|
348 | 348 | |
|
349 | 349 | Pseudo-Decorators |
|
350 | 350 | ================= |
|
351 | 351 | |
|
352 | 352 | Here are the supported decorators, and any optional arguments they |
|
353 | 353 | take. Some of the decorators can be used as options to the entire |
|
354 | 354 | block (eg ``verbatim`` and ``suppress``), and some only apply to the |
|
355 | 355 | line just below them (eg ``savefig``). |
|
356 | 356 | |
|
357 | 357 | @suppress |
|
358 | 358 | |
|
359 | 359 | execute the ipython input block, but suppress the input and output |
|
360 | 360 | block from the rendered output. Also, can be applied to the entire |
|
361 | 361 | ``..ipython`` block as a directive option with ``:suppress:``. |
|
362 | 362 | |
|
363 | 363 | @verbatim |
|
364 | 364 | |
|
365 | 365 | insert the input and output block in verbatim, but auto-increment |
|
366 | 366 | the line numbers. Internally, the interpreter will be fed an empty |
|
367 | 367 | string, so it is a no-op that keeps line numbering consistent. |
|
368 | 368 | Also, can be applied to the entire ``..ipython`` block as a |
|
369 | 369 | directive option with ``:verbatim:``. |
|
370 | 370 | |
|
371 | 371 | @savefig OUTFILE [IMAGE_OPTIONS] |
|
372 | 372 | |
|
373 | 373 | save the figure to the static directory and insert it into the |
|
374 | 374 | document, possibly binding it into a minipage and/or putting |
|
375 | 375 | code/figure label/references to associate the code and the |
|
376 | 376 | figure. Takes args to pass to the image directive (*scale*, |
|
377 | 377 | *width*, etc can be kwargs); see `image options |
|
378 | 378 | <http://docutils.sourceforge.net/docs/ref/rst/directives.html#image>`_ |
|
379 | 379 | for details. |
|
380 | 380 | |
|
381 | 381 | @doctest |
|
382 | 382 | |
|
383 | 383 | Compare the pasted in output in the ipython block with the output |
|
384 | 384 | generated at doc build time, and raise errors if they don’t |
|
385 | 385 | match. Also, can be applied to the entire ``..ipython`` block as a |
|
386 | 386 | directive option with ``:doctest:``. |
|
387 | 387 | |
|
388 | 388 | Configuration Options |
|
389 | 389 | ===================== |
|
390 | 390 | |
|
391 | 391 | ipython_savefig_dir |
|
392 | 392 | |
|
393 | 393 | The directory in which to save the figures. This is relative to the |
|
394 | 394 | Sphinx source directory. The default is `html_static_path`. |
|
395 | 395 | |
|
396 | 396 | ipython_rgxin |
|
397 | 397 | |
|
398 | 398 | The compiled regular expression to denote the start of IPython input |
|
399 | 399 | lines. The default is re.compile('In \[(\d+)\]:\s?(.*)\s*'). You |
|
400 | 400 | shouldn't need to change this. |
|
401 | 401 | |
|
402 | 402 | ipython_rgxout |
|
403 | 403 | |
|
404 | 404 | The compiled regular expression to denote the start of IPython output |
|
405 | 405 | lines. The default is re.compile('Out\[(\d+)\]:\s?(.*)\s*'). You |
|
406 | 406 | shouldn't need to change this. |
|
407 | 407 | |
|
408 | 408 | |
|
409 | 409 | ipython_promptin |
|
410 | 410 | |
|
411 | 411 | The string to represent the IPython input prompt in the generated ReST. |
|
412 | 412 | The default is 'In [%d]:'. This expects that the line numbers are used |
|
413 | 413 | in the prompt. |
|
414 | 414 | |
|
415 | 415 | ipython_promptout |
|
416 | 416 | |
|
417 | 417 | The string to represent the IPython prompt in the generated ReST. The |
|
418 | 418 | default is 'Out [%d]:'. This expects that the line numbers are used |
|
419 | 419 | in the prompt. |
|
420 | ||
|
421 | .. _ipython_literal: | |
|
422 | ||
|
423 | Sphinx source for this tutorial | |
|
424 | =============================== | |
|
425 | ||
|
426 | .. literalinclude:: ipython_directive.rst | |
|
427 |
General Comments 0
You need to be logged in to leave comments.
Login now