A Sphinx extension that builds an HTML gallery of examples from any set of Python scripts.
Custom RST directives for creating mini-galleries and enhanced image displays in Sphinx documentation. These directives extend standard RST functionality with Sphinx-Gallery specific features.
Creates mini-galleries showing related examples that use specific functions or modules.
class MiniGallery(Directive):
"""
RST directive for creating mini-galleries of related examples.
Creates a gallery of examples that reference a specific function,
class, or module. Automatically finds and displays relevant examples
with thumbnails and links.
""".. minigallery:: numpy.mean
:add-heading: Examples using ``numpy.mean``
.. minigallery:: matplotlib.pyplot.plot
:add-heading:
.. minigallery:: sklearn.datasets.load_irisnumpy.mean) specifies the function/module to find examples forThe directive generates a responsive grid of example thumbnails with titles and links to the full examples. It automatically searches through your gallery examples to find those that import or use the specified function.
Enhanced image directive with responsive srcset support for high-DPI displays and better mobile experience.
class ImageSg(Directive):
"""
Enhanced image directive with responsive srcset support.
Extends the standard RST image directive to support modern
responsive image features including srcset for different
pixel densities and screen sizes.
""".. image-sg:: /auto_examples/images/plot_example_001.png
:alt: Example plot showing data visualization
:srcset: /auto_examples/images/plot_example_001.png, /auto_examples/images/plot_example_001_2x.png 2x
:class: sphx-glr-single-img
.. image-sg:: /path/to/image.png
:alt: Alternative text
:width: 400px
:height: 300pxAll standard RST image options plus:
sphx-glr-single-img)The srcset option enables responsive images:
.. image-sg:: /images/plot_basic.png
:alt: Basic plot
:srcset: /images/plot_basic.png, /images/plot_basic_2x.png 2x, /images/plot_basic_mobile.png 480wThis provides:
2x) for retina/high-DPI displays480w)Function for registering custom Sphinx nodes used by the image directive.
def imagesg_addnode(app):
"""
Registers custom Sphinx nodes for image handling.
Adds the custom image-sg node type to the Sphinx application
for proper rendering across different output formats (HTML, LaTeX, etc.).
Parameters:
- app: Sphinx application object
"""Within example Python scripts, you can reference the generated images:
"""
Example Script
==============
This example demonstrates basic plotting functionality.
.. image-sg:: /auto_examples/images/sphx_glr_plot_basic_001.png
:alt: Basic line plot
:class: sphx-glr-single-img
"""
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()Use mini-galleries to show related examples in API documentation:
numpy.mean
==========
Compute the arithmetic mean along the specified axis.
.. minigallery:: numpy.mean
:add-heading: Examples using ``numpy.mean``
This function computes the arithmetic mean of array elements...Link to specific examples using enhanced images:
Advanced Techniques
===================
For more complex visualizations, see this example:
.. image-sg:: /auto_examples/images/sphx_glr_plot_advanced_001.png
:alt: Advanced visualization example
:class: sphx-glr-single-img
:target: /auto_examples/plot_advanced.htmlThe directives automatically generate responsive HTML that adapts to different screen sizes and devices. Images scale appropriately and mini-galleries reflow based on available space.
Both directives support accessibility features:
You can customize the appearance with CSS:
.sphx-glr-single-img {
max-width: 100%;
border: 1px solid #ddd;
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.sphx-glr-thumbcontainer {
margin: 10px;
text-align: center;
}Advanced users can override the HTML templates used by the directives by providing custom templates in their Sphinx theme.
Install with Tessl CLI
npx tessl i tessl/pypi-sphinx-gallery