Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ada39dc

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 ada39dc

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed
Lines changed: 7 additions & 0 deletions
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_api>`
4+
(``.. plot::``) now supports a ``:show-source-link:`` option to show or hide
5+
the link to the source code for each plot. The default is set using the
6+
``plot_html_show_source_link`` variable in :file:`conf.py` (which
7+
defaults to True).

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 9 additions & 2 deletions
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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,50 @@ 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+
@pytest.mark.parametrize('plot_html_show_source_link', [0, 1])
126+
def test_show_source_link_true(tmpdir, plot_html_show_source_link):
127+
# Test that a source link is generated if :show-source-link: is true,
128+
# whether or not plot_html_show_source_link is true.
129+
source_dir = Path(tmpdir) / 'src'
130+
source_dir.mkdir()
131+
parent = Path(__file__).parent
132+
shutil.copyfile(parent / 'tinypages/conf.py', source_dir / 'conf.py')
133+
shutil.copytree(parent / 'tinypages/_static', source_dir / '_static')
134+
doctree_dir = source_dir / 'doctrees'
135+
(source_dir / 'index.rst').write_text("""
136+
.. plot::
137+
:show-source-link: true
138+
139+
plt.plot(range(2))
140+
""")
141+
html_dir = source_dir / '_build' / 'html'
142+
build_sphinx_html(source_dir, doctree_dir, html_dir, extra_args=[
143+
'-D', f'plot_html_show_source_link={plot_html_show_source_link}'])
144+
assert "index-1.py" in [p.name for p in html_dir.iterdir()]
145+
146+
147+
@pytest.mark.parametrize('plot_html_show_source_link', [0, 1])
148+
def test_show_source_link_false(tmpdir, plot_html_show_source_link):
149+
# Test that a source link is NOT generated if :show-source-link: is false,
150+
# whether or not plot_html_show_source_link is true.
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+
html_dir = source_dir / '_build' / 'html'
164+
build_sphinx_html(source_dir, doctree_dir, html_dir, extra_args=[
165+
'-D', f'plot_html_show_source_link={plot_html_show_source_link}'])
166+
assert "index-1.py" not in [p.name for p in html_dir.iterdir()]
167+
168+
125169
def build_sphinx_html(source_dir, doctree_dir, html_dir, extra_args=None):
126170
# Build the pages with warnings turned into errors
127171
extra_args = [] if extra_args is None else extra_args

0 commit comments

Comments
 (0)