Structuring Python scripts for Sphinx-Gallery#
This page describes the structure and syntax that can be used in Python scripts to generate rendered HTML gallery pages.
A simple example#
Sphinx-Gallery expects each Python file to have two things:
A docstring, written in reST, that defines the header for the example. It must begin by defining a reST title. The title may contain any punctuation mark but cannot start with the same punctuation mark repeated more than 3 times. For example:
""" "This" is my example-script =========================== This example doesn't do much, it just makes a simple plot """
Python code. This can be any valid Python code that you wish. Any Matplotlib images that are generated will be saved to disk, and the reST generated will display these images with the built examples. By default only images generated by Matplotlib, or packages based on Matplotlib (e.g., Seaborn or Yellowbrick) are saved and displayed. However, you can change this to include other packages, see Image scrapers.
For a quick reference have a look at the example Introductory example - Plotting sin
Embed reST in your example Python files#
Additionally, you may embed reST syntax within your Python scripts. reST allows you to easily add formatted text, math equations and reference links, including cross referencing other examples. This will be rendered in-line with the Python code and its outputs, similar to how Jupyter Notebooks are structured (in fact, Sphinx-Gallery also creates a Jupyter Notebook for each example that is built).
You can embed reST in your Python examples by including a line of >= 20 #
symbols, #%%
, or # %%
. For consistency, it is recommended that you use
only one of the above three āblock splitterā options in your project. If using
a line of #
ās, we recommend using 79 #
ās, like this:
###############################################################################
Any commented lines (line beginning with #
followed by a space, to
be PEP8-compliant) that immediately follow a block splitter will be rendered as
reST in the built gallery examples. To switch back to writing code, either
stop starting lines with #
and a space or leave an empty line before writing
code comments. You can thus easily alternate between text and code āblocksā.
For example:
# This is commented python
myvariable = 2
print("my variable is {}".format(myvariable))
# %%
# This is a section header
# ------------------------
#
# In the built documentation, it will be rendered as reST. All reST lines
# must begin with '# ' (note the space) including underlines below section
# headers.
# These lines won't be rendered as reST because there is a gap after the last
# commented reST block. Instead, they'll resolve as regular Python comments.
# Normal Python code can follow these comments.
print('my variable plus 2 is {}'.format(myvariable + 2))
The #%%
and # %%
syntax is consistent with the ācode blockā (or
ācode cellā) separator syntax in Visual Studio Code Python extension,
Visual Studio Python Tools,
Jupytext,
Pycharm Professional,
Hydrogen plugin (for Atom)
and Spyder.
Note that although the documentation of these editors/IDEs
may only mention one of #%%
or # %%
, in practice both
work. With these editors/IDEs, #%%
or
# %%
at the start of a line signifies the start of a new code block.
Code blocks allow you to separate your code into chunks, like in Jupyter
Notebooks. All the code within a code block can be easily executed together.
This functionality can be helpful when writing a Sphinx-Gallery .py
example as
the blocks allow you to easily create pairs of subsequent Sphinx-Gallery text
and code blocks.
Here are the contents of an example Python file using the ācode blockā functionality:
"""
This is my example script
=========================
This example doesn't do much, it just makes a simple plot
"""
# %%
# This is a section header
# ------------------------
# This is the first section!
# The `#%%` signifies to Sphinx-Gallery that this text should be rendered as
# reST and if using one of the above IDE/plugin's, also signifies the start of a
# 'code block'.
# This line won't be rendered as reST because there's a space after the last block.
myvariable = 2
print("my variable is {}".format(myvariable))
# This is the end of the 'code block' (if using an above IDE). All code within
# this block can be easily executed all at once.
# %%
# This is another section header
# ------------------------------
#
# In the built documentation, it will be rendered as reST after the code above!
# This is also another code block.
print('my variable plus 2 is {}'.format(myvariable + 2))
For a clear example refer to the rendered example
Alternating text and code and compare it to the generated
original python script
Plain reST examples#
Sphinx-Gallery generates examples from Python scripts, so examples written in plain reST files are not supported. If youāre looking to generate hyperlinks for functions (linking to their corresponding online documentation) in code blocks of ordinary reST documentation, you might find sphinx-codeautolink helpful.