Ipython Directive¶
The ipython directive is a stateful ipython shell for embedding in
sphinx documents. It knows about standard ipython prompts, and
extracts the input and output lines. These prompts will be renumbered
starting at 1
. The inputs will be fed to an embedded ipython
interpreter and the outputs from that interpreter will be inserted as
well. For example, code blocks like the following:
.. ipython::
In [136]: x = 2
In [137]: x**3
Out[137]: 8
will be rendered as
In [1]: x = 2
In [2]: x**3
Out[2]: 8
Note
This tutorial should be read side-by-side with the Sphinc source for this document (see Sphinx source for this tutorial) because otherwise you will see only the rendered output and not the code that generated it. Excepting the example above, we will not in general be showing the liuteral rest in this document that generates the rendered output.
The state from previous sessions is stored, and standard error is trapped. At doc build time, ipython’s output and std err will be inserted, and prompts will be renumbered. So the prompt below should be renumbered in the rendered docs, and pick up where the block above left off.
In [3]: z = x*3 # x is recalled from previous block
In [4]: z
Out[4]: 6
In [5]: print z