Skip to content

Commit 9c5a5dc

Browse files
committed
Add :shows-source-link: option to Sphinx plot directive
The Sphinx plot directive (`.. plot::`) now supports a `:show-source-link` option to show or hide the link to the source for each plot. The default is set using the `plot_html_show_source_link` variable in `conf.py` (which itself defaults to True).
1 parent 007c4c1 commit 9c5a5dc

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Source links can be shown or hidden for each Sphinx plot directive
2+
------------------------------------------------------------------
3+
The :doc:`Sphinx plot directive </api/sphinxext_plot_directive>`
4+
(``.. plot::``) now supports a ``:show-source-link:`` option to show or hide
5+
the link to the source for each plot. The default is set using the
6+
``plot_html_show_source_link`` variable in :file:`conf.py` (which itself
7+
defaults to True).

lib/matplotlib/sphinxext/plot_directive.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
the ``plot_include_source`` variable in :file:`conf.py` (which itself
5656
defaults to False).
5757
58+
``:show-source-link:`` : bool
59+
Whether to show a link to the source in HTML. The default can be
60+
changed using the ``plot_html_show_source_link`` variable in
61+
:file:`conf.py` (which itself defaults to True).
62+
5863
``:encoding:`` : str
5964
If this source file is in a non-UTF8 or non-ASCII encoding, the
6065
encoding must be specified using the ``:encoding:`` option. The
@@ -245,6 +250,7 @@ class PlotDirective(Directive):
245250
'align': Image.align,
246251
'class': directives.class_option,
247252
'include-source': _option_boolean,
253+
'show-source-link': _option_boolean,
248254
'format': _option_format,
249255
'context': _option_context,
250256
'nofigs': directives.flag,
@@ -666,6 +672,7 @@ def run(arguments, content, options, state_machine, state, lineno):
666672
default_fmt = formats[0][0]
667673

668674
options.setdefault('include-source', config.plot_include_source)
675+
options.setdefault('show-source-link', config.plot_html_show_source_link)
669676
if 'class' in options:
670677
# classes are parsed into a list of string, and output by simply
671678
# printing the list, abusing the fact that RST guarantees to strip
@@ -832,7 +839,7 @@ def run(arguments, content, options, state_machine, state, lineno):
832839

833840
# Not-None src_link signals the need for a source link in the generated
834841
# html
835-
if j == 0 and config.plot_html_show_source_link:
842+
if j == 0 and options['show-source-link']:
836843
src_link = source_link
837844
else:
838845
src_link = None
@@ -866,7 +873,7 @@ def run(arguments, content, options, state_machine, state, lineno):
866873
shutil.copyfile(fn, destimg)
867874

868875
# copy script (if necessary)
869-
if config.plot_html_show_source_link:
876+
if options['show-source-link']:
870877
Path(dest_dir, output_base + source_ext).write_text(
871878
doctest.script_from_examples(code)
872879
if source_file_name == rst_file and is_doctest

lib/matplotlib/tests/test_sphinxext.py

+50
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,56 @@ def test_plot_html_show_source_link(tmpdir):
122122
assert "index-1.py" not in [p.name for p in html_dir2.iterdir()]
123123

124124

125+
def test_show_source_link_true(tmpdir):
126+
source_dir = Path(tmpdir) / 'src'
127+
source_dir.mkdir()
128+
parent = Path(__file__).parent
129+
shutil.copyfile(parent / 'tinypages/conf.py', source_dir / 'conf.py')
130+
shutil.copytree(parent / 'tinypages/_static', source_dir / '_static')
131+
doctree_dir = source_dir / 'doctrees'
132+
(source_dir / 'index.rst').write_text("""
133+
.. plot::
134+
:show-source-link: true
135+
136+
plt.plot(range(2))
137+
""")
138+
# Make sure source scripts are created by default
139+
html_dir1 = source_dir / '_build' / 'html1'
140+
build_sphinx_html(source_dir, doctree_dir, html_dir1)
141+
assert "index-1.py" in [p.name for p in html_dir1.iterdir()]
142+
# Make sure source scripts are still created when
143+
# plot_html_show_source_link` is False
144+
html_dir2 = source_dir / '_build' / 'html2'
145+
build_sphinx_html(source_dir, doctree_dir, html_dir2,
146+
extra_args=['-D', 'plot_html_show_source_link=0'])
147+
assert "index-1.py" in [p.name for p in html_dir1.iterdir()]
148+
149+
150+
def test_show_source_link_false(tmpdir):
151+
source_dir = Path(tmpdir) / 'src'
152+
source_dir.mkdir()
153+
parent = Path(__file__).parent
154+
shutil.copyfile(parent / 'tinypages/conf.py', source_dir / 'conf.py')
155+
shutil.copytree(parent / 'tinypages/_static', source_dir / '_static')
156+
doctree_dir = source_dir / 'doctrees'
157+
(source_dir / 'index.rst').write_text("""
158+
.. plot::
159+
:show-source-link: false
160+
161+
plt.plot(range(2))
162+
""")
163+
# Make sure source scripts are NOT created
164+
html_dir1 = source_dir / '_build' / 'html1'
165+
build_sphinx_html(source_dir, doctree_dir, html_dir1)
166+
assert "index-1.py" not in [p.name for p in html_dir1.iterdir()]
167+
# Make sure source scripts are still NOT created when
168+
# plot_html_show_source_link` is False
169+
html_dir2 = source_dir / '_build' / 'html2'
170+
build_sphinx_html(source_dir, doctree_dir, html_dir2,
171+
extra_args=['-D', 'plot_html_show_source_link=0'])
172+
assert "index-1.py" not in [p.name for p in html_dir2.iterdir()]
173+
174+
125175
def build_sphinx_html(source_dir, doctree_dir, html_dir, extra_args=None):
126176
# Build the pages with warnings turned into errors
127177
extra_args = [] if extra_args is None else extra_args

0 commit comments

Comments
 (0)