1
1
.. _ipython_directive :
2
2
3
3
=================
4
- Ipython Directive
4
+ IPython Directive
5
5
=================
6
6
7
- The ipython directive is a stateful ipython shell for embedding in
8
- sphinx documents. It knows about standard ipython prompts, and
7
+ The IPython directive is a stateful IPython shell for embedding in
8
+ Sphinx documents. It knows about standard IPython prompts, and
9
9
extracts the input and output lines. These prompts will be renumbered
10
- starting at ``1 ``. The inputs will be fed to an embedded ipython
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
@@ -29,53 +29,43 @@ will be rendered as
29
29
30
30
.. note ::
31
31
32
- This tutorial should be read side-by-side with the Sphinc source
32
+ This tutorial should be read side-by-side with the Sphinx source
33
33
for this document (see :ref: `ipython_literal `) because otherwise
34
34
you will see only the rendered output and not the code that
35
35
generated it. Excepting the example above, we will not in general
36
- be showing the liuteral rest in this document that generates the
36
+ be showing the literal reST in this document that generates the
37
37
rendered output.
38
38
39
39
40
40
The state from previous sessions is stored, and standard error is
41
- trapped. At doc build time, ipython 's output and std err will be
41
+ trapped. At doc build time, IPython 's output and std err will be
42
42
inserted, and prompts will be renumbered. So the prompt below should
43
43
be renumbered in the rendered docs, and pick up where the block above
44
44
left off.
45
45
46
46
.. ipython ::
47
47
48
- In [138 ]: z = x*3 # x is recalled from previous block
48
+ In [1 ]: z = x * 3 # x is recalled from previous block
49
49
50
- In [139]: z
51
- Out[139]: 6
50
+ In [2]: z
52
51
53
- In [140]: print z
54
- --------> print(z)
55
- 6
56
-
57
- In [141]: q = z[) # this is a syntax error -- we trap ipy exceptions
58
- ------------------------------------------------------------
59
- File "<ipython console>", line 1
60
- q = z[) # this is a syntax error -- we trap ipy exceptions
61
- ^
62
- SyntaxError: invalid syntax
52
+ In [3]: print(z)
63
53
54
+ In [4]: q = z[) # this is a syntax error -- we trap exceptions
64
55
65
56
The embedded interpreter supports some limited markup. For example,
66
- you can put comments in your ipython sessions, which are reported
57
+ you can put comments in your IPython sessions, which are reported
67
58
verbatim. There are some handy "pseudo-decorators" that let you
68
- doctest the output. The inputs are fed to an embedded ipython
69
- session and the outputs from the ipython session are inserted into
70
- your doc. If the output in your doc and in the ipython session don't
71
- match on a doctest assertion, an error will be
72
-
59
+ doctest the output. The inputs are fed to an embedded IPython
60
+ session and the outputs from the IPython session are inserted into
61
+ your doc. If the output in your doc and in the IPython session don't
62
+ match on a doctest assertion, an error will be raised
73
63
74
64
.. ipython ::
75
65
76
66
In [1]: x = 'hello world'
77
67
78
- # this will raise an error if the ipython output is different
68
+ # this will raise an error if the IPython output is different
79
69
@doctest
80
70
In [2]: x.upper()
81
71
Out[2]: 'HELLO WORLD'
@@ -87,121 +77,98 @@ match on a doctest assertion, an error will be
87
77
In [3]: x.st<TAB>
88
78
x.startswith x.strip
89
79
90
-
91
80
Multi-line input is supported.
92
81
93
82
.. ipython ::
94
83
95
- In [130]: url = 'http://ichart.finance.yahoo.com/table.csv?s=CROX\
96
- .....: &d=9&e=22&f=2009&g=d&a=1&br=8&c=2006&ignore=.csv'
97
-
98
- In [131]: print url.split('&')
99
- --------> print(url.split('&'))
100
- ['http://ichart.finance.yahoo.com/table.csv?s=CROX', 'd=9', 'e=22',
101
- 'f=2009', 'g=d', 'a=1', 'b=8', 'c=2006', 'ignore=.csv']
84
+ In [1]: url = ('https://example.com?s=CROX&d=9&e=22&f=2009&'
85
+ ...: 'g=d&a=1&br=8&c=2006&ignore=.csv')
86
+ ...:
102
87
103
- In [60 ]: import urllib
88
+ In [2 ]: print(url.split('&'))
104
89
90
+ In [3]: import urllib
105
91
106
92
You can do doctesting on multi-line output as well. Just be careful
107
- when using non-deterministic inputs like random numbers in the ipython
108
- directive, because your inputs are ruin through a live interpreter, so
93
+ when using non-deterministic inputs like random numbers in the IPython
94
+ directive, because your inputs are run through a live interpreter, so
109
95
if you are doctesting random output you will get an error. Here we
110
96
"seed" the random number generator for deterministic output, and we
111
97
suppress the seed line so it doesn't show up in the rendered output
112
98
113
99
.. ipython ::
114
100
115
- In [133 ]: import numpy.random
101
+ In [1 ]: import numpy.random
116
102
117
103
@suppress
118
- In [134 ]: numpy.random.seed(2358)
104
+ In [2 ]: numpy.random.seed(2358)
119
105
120
106
@doctest
121
- In [135 ]: numpy.random.rand(10,2)
122
- Out[135 ]:
107
+ In [3 ]: numpy.random.rand(10, 2)
108
+ Out[3 ]:
123
109
array([[ 0.64524308, 0.59943846],
124
- [ 0.47102322, 0.8715456 ],
125
- [ 0.29370834, 0.74776844],
126
- [ 0.99539577, 0.1313423 ],
127
- [ 0.16250302, 0.21103583],
128
- [ 0.81626524, 0.1312433 ],
129
- [ 0.67338089, 0.72302393],
130
- [ 0.7566368 , 0.07033696],
131
- [ 0.22591016, 0.77731835],
132
- [ 0.0072729 , 0.34273127]])
133
-
110
+ [ 0.47102322, 0.8715456 ],
111
+ [ 0.29370834, 0.74776844],
112
+ [ 0.99539577, 0.1313423 ],
113
+ [ 0.16250302, 0.21103583],
114
+ [ 0.81626524, 0.1312433 ],
115
+ [ 0.67338089, 0.72302393],
116
+ [ 0.7566368 , 0.07033696],
117
+ [ 0.22591016, 0.77731835],
118
+ [ 0.0072729 , 0.34273127]])
134
119
135
120
Another demonstration of multi-line input and output
136
121
137
122
.. ipython ::
138
123
139
- In [106]: print x
140
- --------> print(x)
141
- jdh
142
-
143
- In [109]: for i in range(10):
144
- .....: print i
145
- .....:
146
- .....:
147
- 0
148
- 1
149
- 2
150
- 3
151
- 4
152
- 5
153
- 6
154
- 7
155
- 8
156
- 9
157
-
158
-
159
- Most of the "pseudo-decorators" can be used an options to ipython
160
- mode. For example, to setup matplotlib pylab but suppress the output,
161
- you can do. When using the matplotlib ``use `` directive, it should
162
- occur before any import of pylab. This will not show up in the
163
- rendered docs, but the commands will be executed in the embedded
164
- interpreter and subsequent line numbers will be incremented to reflect
165
- the inputs::
124
+ In [1]: print(x)
125
+
126
+ In [2]: for i in range(10):
127
+ ...: print(i)
128
+ ...:
166
129
130
+ Most of the "pseudo-decorators" can be used as options to IPython
131
+ mode. For example, to setup ``matplotlib `` but suppress the output,
132
+ you can do. This will not show up in the rendered docs, but the commands
133
+ will be executed in the embedded interpreter and subsequent line numbers
134
+ will be incremented to reflect the inputs::
167
135
168
136
.. ipython::
169
137
:suppress:
170
138
171
- In [144 ]: from pylab import *
139
+ In [1 ]: import matplotlib.pyplot as plt
172
140
173
- In [145 ]: ion()
141
+ In [2 ]: plt. ion()
174
142
175
143
.. ipython ::
176
144
:suppress:
177
145
178
- In [144 ]: from pylab import *
146
+ In [1 ]: import matplotlib.pyplot as plt
179
147
180
- In [145 ]: ion()
148
+ In [2 ]: plt. ion()
181
149
182
- Likewise, you can set ``:doctest: `` or ``:verbatim: `` to apply these
183
- settings to the entire block. For example,
150
+ Likewise, you can set the ``:doctest: `` or ``:verbatim: `` Sphinx options to
151
+ apply these settings to the entire block. For example,
184
152
185
153
.. ipython ::
186
154
:verbatim:
187
155
188
- In [9 ]: cd mpl/examples/
156
+ In [1 ]: cd mpl/examples/
189
157
/home/jdhunter/mpl/examples
190
158
191
- In [10]: pwd
192
- Out[10]: '/home/jdhunter/mpl/examples'
193
-
159
+ In [2]: pwd
160
+ Out[2]: '/home/jdhunter/mpl/examples'
194
161
195
- In [14 ]: cd mpl/examples/<TAB>
162
+ In [3 ]: cd mpl/examples/<TAB>
196
163
mpl/examples/animation/ mpl/examples/misc/
197
164
mpl/examples/api/ mpl/examples/mplot3d/
198
165
mpl/examples/axes_grid/ mpl/examples/pylab_examples/
199
166
mpl/examples/event_handling/ mpl/examples/widgets
200
167
201
- In [14 ]: cd mpl/examples/widgets/
168
+ In [4 ]: cd mpl/examples/widgets/
202
169
/home/jdhunter/mpl/examples/widgets
203
170
204
- In [15 ]: !wc *
171
+ In [5 ]: !wc *
205
172
2 12 77 README.txt
206
173
40 97 884 buttons.py
207
174
26 90 712 check_buttons.py
@@ -214,32 +181,29 @@ settings to the entire block. For example,
214
181
40 124 1088 span_selector.py
215
182
450 1274 12457 total
216
183
217
-
218
-
219
- You can create one or more pyplot plots and insert them with the
184
+ You can create one or more plots and insert them with the
220
185
``@savefig `` decorator.
221
186
222
187
.. ipython ::
223
188
224
189
@savefig plot_simple.png width=4in
225
- In [151 ]: plot([1,2,3]);
190
+ In [1 ]: plt. plot([1,2,3]);
226
191
227
192
# use a semicolon to suppress the output
228
193
@savefig hist_simple.png width=4in
229
- In [151 ]: hist(np.random.randn(10000), 100);
194
+ In [2 ]: plt. hist(np.random.randn(10000), 100);
230
195
231
196
In a subsequent session, we can update the current figure with some
232
197
text, and then resave
233
198
234
199
.. ipython ::
235
200
201
+ In [1]: plt.ylabel('number')
236
202
237
- In [151 ]: ylabel('number ')
203
+ In [2 ]: plt.title('normal distribution ')
238
204
239
- In [152]: title('normal distribution')
240
-
241
- @savefig hist_with_text.png width=4in
242
- In [153]: grid(True)
205
+ @savefig hist_with_text.png width=4in align=center
206
+ In [3]: plt.grid(True)
243
207
244
208
Pseudo-Decorators
245
209
=================
@@ -249,21 +213,21 @@ take. Some of the decorators can be used as options to the entire
249
213
block (eg ``verbatim `` and ``suppress ``), and some only apply to the
250
214
line just below them (eg ``savefig ``).
251
215
252
- @suppress
216
+ `` @suppress ``
253
217
254
- execute the ipython input block, but suppress the input and output
218
+ execute the IPython input block, but suppress the input and output
255
219
block from the rendered output. Also, can be applied to the entire
256
220
``..ipython `` block as a directive option with ``:suppress: ``.
257
221
258
- @verbatim
222
+ `` @verbatim ``
259
223
260
224
insert the input and output block in verbatim, but auto-increment
261
225
the line numbers. Internally, the interpreter will be fed an empty
262
226
string, so it is a no-op that keeps line numbering consistent.
263
227
Also, can be applied to the entire ``..ipython `` block as a
264
228
directive option with ``:verbatim: ``.
265
229
266
- @savefig OUTFILE [IMAGE_OPTIONS]
230
+ `` @savefig OUTFILE [IMAGE_OPTIONS] ``
267
231
268
232
save the figure to the static directory and insert it into the
269
233
document, possibly binding it into a minipage and/or putting
@@ -273,16 +237,31 @@ line just below them (eg ``savefig``).
273
237
<http://docutils.sourceforge.net/docs/ref/rst/directives.html#image> `_
274
238
for details.
275
239
276
- @doctest
240
+ `` @doctest ``
277
241
278
- Compare the pasted in output in the ipython block with the output
242
+ Compare the pasted in output in the IPython block with the output
279
243
generated at doc build time, and raise errors if they don’t
280
244
match. Also, can be applied to the entire ``..ipython `` block as a
281
245
directive option with ``:doctest: ``.
282
246
247
+ Enabling IPython Directive
248
+ ==========================
249
+
250
+ To enable the IPython directive(s) in your Sphinx documentation,
251
+ you'll need to have ``IPython `` installed and include the following
252
+ in your Sphinx configuration (``conf.py ``):
253
+
254
+ .. code-block :: python
255
+
256
+ extensions = [
257
+ ' IPython.sphinxext.ipython_directive' ,
258
+ ' IPython.sphinxext.ipython_console_highlighting' ,
259
+ ...
260
+ ]
261
+
283
262
.. _ipython_literal :
284
263
285
264
Sphinx source for this tutorial
286
- ====================================
265
+ ===============================
287
266
288
267
.. literalinclude :: ipython_directive.rst
0 commit comments