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