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