Examples with executable code#
Introduction#
This Sphinx extension provides an interface to examples with executable code.
This extension includes code adapted from sphinx-exercise.
Installation#
To install the Sphinx-Code-Examples, follow these steps:
Step 1: Install the Package
Install the Sphinx-Code-Examples
package using pip
:
pip install sphinx-code-examples
Step 2: Add to requirements.txt
Make sure that the package is included in your project’s requirements.txt
to track the dependency:
sphinx-code-examples
Step 3: Enable in _config.yml
In your _config.yml
file, add the extension to the list of Sphinx extra extensions:
sphinx:
extra_extensions:
- sphinx_code_examples
Documentation#
Further documentation for this extension is available in the TeachBooks manual.
Configuration#
The extension provides several configuration values, which can be added to _config.yml
:
sphinx:
config:
sphinx_codex_name: "" # default value
sphinx_codex_style_from_proof: true # default value
sphinx_codex_icon_from_proof: false # default value
sphinx_codex_merge_with_proof: false # default value
sphinx_codex_name
:""
(default) orstring
:if
""
allcodex
directives will have the title “Code example” and will be referred to as “Code example” in the text.if a
string
is provided, allcodex
directives will have the title set to that string and will be referred to as that string in the text.
sphinx_codex_style_from_proof
:true
(default) orfalse
:if
true
, the CSS color style of thecodex
directive will be taken from theprf:example
directive from the sphinx-proof extension.if
false
, the CSS color style of thecodex
directive will be taken from the generaladmonition
directive.
sphinx_codex_icon_from_proof
:true
(default) orfalse
:if
true
, the icon of thecodex
directive will be taken from theprf:example
directive from the sphinx-proof extension.if
false
, the icon of thecodex
directive will be set to .
sphinx_codex_merge_with_proof
:false
(default) ortrue
:if
true
, thecodex
directive will be merged with theprf:example
directive from the sphinx-proof extension. This means that:All
codex
directives andprf:example
directives will be named “Example” (unless set otherwise) and styled (color and icon) as defined by theprf:example
directive from the sphinx-proof extension.The numbering of the
codex
directives will be merged with the numbering of theprf:example
directives from the sphinx-proof extension.
if
false
, thecodex
directive will not be merged with theprf:example
directive from the sphinx-proof extension. This means that:All
codex
directives will be named “Code example” (unless set otherwise) and styled (color and icon) as defined by thesphinx_codex_style_from_proof
andsphinx_codex_icon_from_proof
configuration values.The numbering of the
codex
directives will be independent of the numbering of theprf:example
directives from the sphinx-proof extension.
Usage#
This extension provides three directives:
codex
codex-start
codex-end
codex
directive#
Warning
Although this directive is called codex
, it does not imply that the code is executable. It is simply a directive to create the possibility to use one extension for both executable and non-executable (code) examples.
The codex
directive is used to create an example. It can be used as with the following minimal syntax:
:::{codex}
:label: example-codex
This is an example from sphinx-codex. It is a non-code example.
:::
This will be rendered as
Code example 1
This is an example from sphinx-codex. It is a non-code example.
codex-start
and codex-end
directives#
Warning
This extension does not provide a way to execute code. The codex-start
and codex-end
directives are used to create a code example that may contain executable code, but the code itself is not automatically executable.
The codex-start
and codex-end
directives are used to create a code example that may contain executable code. It can be used as with the following minimal syntax:
:::{codex-start}
:label: example-code
Executing the next piece of code
:::
print("Hello from a code cell in Sphinx Book!")
should be a piece of cake.
:::{codex-end}
:::
This will be rendered as
Code example 2
Executing the next piece of code
print("Hello from a code cell in Sphinx Book!")
should be a piece of cake.
References#
References to codex
examples can be made using the ref
role. The label of the example is used as the reference target. Examples can be referenced as
{ref}`example-code` shows a code example from sphinx-codex.
{ref}`example-codex` shows a non-code example from sphinx-codex.
which will be rendered as
Code example 2 shows a code example from sphinx-codex.
Code example 1 shows a non-code example from sphinx-codex.