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