|
| 1 | +Caching sphinx directive figure outputs |
| 2 | +--------------------------------------- |
| 3 | + |
| 4 | +The new ``:outname:`` property for the Sphinx plot directive can |
| 5 | +be used to cache generated images. It is used like: |
| 6 | + |
| 7 | +.. code-block:: rst |
| 8 | +
|
| 9 | + .. plot:: |
| 10 | + :outname: stinkbug_plot |
| 11 | +
|
| 12 | + import matplotlib.pyplot as plt |
| 13 | + import matplotlib.image as mpimg |
| 14 | + import numpy as np |
| 15 | + img = mpimg.imread('_static/stinkbug.png') |
| 16 | + imgplot = plt.imshow(img) |
| 17 | +
|
| 18 | +Without ``:outname:``, the figure generated above would normally be called, |
| 19 | +e.g. :file:`docfile3-4-01.png` or something equally mysterious. With |
| 20 | +``:outname:`` the figure generated will instead be named |
| 21 | +:file:`stinkbug_plot-01.png` or even :file:`stinkbug_plot.png`. This makes it |
| 22 | +easy to understand which output image is which and, more importantly, uniquely |
| 23 | +keys output images to the code snippets that generated them. |
| 24 | + |
| 25 | +Configuring the cache directory |
| 26 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 27 | +The directory that images are cached to can be configured using the |
| 28 | +``plot_cache_dir`` configuration value in the Sphinx configuration file. |
| 29 | + |
| 30 | +If an image is already in ``plot_cache_dir`` when documentation is being |
| 31 | +generated, this image is copied to the build directory thereby pre-empting |
| 32 | +generation and reducing computation time in low-resource environments. |
0 commit comments