Sphinx Prompt
Sphinx Prompt is a Sphinx directive that allows developers to add code blocks with unselectable prompts to their documentation. This ensures that readers can easily copy and paste commands without including the prompt characters. The library is actively maintained, with frequent minor releases, and is currently at version 1.10.2.
Warnings
- breaking Version 1.9.0 introduced compatibility with Sphinx 8, a major release of Sphinx. Users upgrading to `sphinx-prompt` 1.9.0 or later may encounter incompatibilities if their Sphinx project is built with an older Sphinx version (prior to Sphinx 8).
- breaking The minimum required Python version for `sphinx-prompt` was updated to Python >= 3.11 in recent releases (specifically, 1.10.x). Earlier versions of `sphinx-prompt` (e.g., 1.8.0) supported Python 3.9.
- gotcha When using the `.. prompt::` directive, if your desired prompt string contains spaces (e.g., `(venv) $`), you cannot use positional arguments to specify it due to reStructuredText parsing limitations. The prompt will be incorrectly split.
- gotcha If you are using `sphinx-prompt` alongside other Sphinx extensions that modify or interact with code blocks (e.g., `sphinx-substitution-extensions`), the order in which extensions are loaded in `conf.py` can be important. Incorrect ordering might lead to unexpected rendering or behavior.
Install
-
pip install sphinx-prompt
Imports
- Extension Setup
# in conf.py extensions = [ 'sphinx_prompt', # ... other extensions ]
Quickstart
# conf.py
extensions = [
'sphinx_prompt',
]
# my_document.rst
.. prompt:: python
>>> import sys
>>> print(sys.version)
3.11.0 (main, ...)
.. prompt:: bash $
$ ls -l
total 0
-rw-r--r-- 1 user group 0 Jan 1 10:00 myfile.txt