How can I add the output to interactive Python console blocks in Sphinx docs?
For example, so I can do something like:
First set the variable::
>>> x = 1
Then print the variable::
>>> print x
And have Sphinx automatically insert the output of print x into the documentation?
I've tried:
sphinxcontrib-autorun https://pypi.python.org/pypi/sphinxcontrib-autorun/0.1-20140415 — but it runs each block in a separate interpriterIPython.sphinxext.ipython_directive — but it forces IPython syntax instead of the "traditional" Python console.Is there anything else that can do this?
Okay, I've gone ahead and built one of those here: https://bitbucket.org/wolever/sphinx-contrib/src/tip/autorun2/?at=default
The docs for writing an extension for Sphinx is at http://sphinx-doc.org/extdev/index.html#dev-extensions
The only hint I would give is that using self.state.nested_parse(..) will save you a lot of headaches. Here is the simplest extension I have:
class SvnRevisionDirective(Directive):
"""Directive to display subversion revision of the path.
"""
has_content = True
required_arguments = 1
optional_arguments = 1
final_argument_whitespace = False
option_spec = {}
def run(self):
path = self.arguments[0]
rev = svntools.Revision(path) # uses subprocess etc.
paragraph = nodes.paragraph()
self.state.nested_parse(
StringList([
'**Revision:** r%d' % rev # you can use regular rst syntax here(!)
]), 0, paragraph)
return [paragraph]
...
def setup(app):
...
app.add_directive('svnrevision', SvnRevisionDirective)
it's used like
.. snvrevision: 'my/file.py'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With