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

Skip to content

Commit 7413bf0

Browse files
authored
Merge pull request #24205 from MikhailRyazanov/download
Plot directive: delegate file handling to Sphinx
2 parents de58d98 + 3e304bb commit 7413bf0

File tree

2 files changed

+27
-45
lines changed

2 files changed

+27
-45
lines changed

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -369,16 +369,16 @@ def _split_code_at_show(text):
369369
370370
.. only:: html
371371
372-
{% if source_link or (html_show_formats and not multi_image) %}
372+
{% if src_name or (html_show_formats and not multi_image) %}
373373
(
374-
{%- if source_link -%}
375-
`Source code <{{ source_link }}>`__
374+
{%- if src_name -%}
375+
:download:`Source code <{{ build_dir }}/{{ src_name }}>`
376376
{%- endif -%}
377377
{%- if html_show_formats and not multi_image -%}
378378
{%- for img in images -%}
379379
{%- for fmt in img.formats -%}
380-
{%- if source_link or not loop.first -%}, {% endif -%}
381-
`{{ fmt }} <{{ dest_dir }}/{{ img.basename }}.{{ fmt }}>`__
380+
{%- if src_name or not loop.first -%}, {% endif -%}
381+
:download:`{{ fmt }} <{{ build_dir }}/{{ img.basename }}.{{ fmt }}>`
382382
{%- endfor -%}
383383
{%- endfor -%}
384384
{%- endif -%}
@@ -395,7 +395,7 @@ def _split_code_at_show(text):
395395
(
396396
{%- for fmt in img.formats -%}
397397
{%- if not loop.first -%}, {% endif -%}
398-
`{{ fmt }} <{{ dest_dir }}/{{ img.basename }}.{{ fmt }}>`__
398+
:download:`{{ fmt }} <{{ build_dir }}/{{ img.basename }}.{{ fmt }}>`
399399
{%- endfor -%}
400400
)
401401
{%- endif -%}
@@ -756,21 +756,13 @@ def run(arguments, content, options, state_machine, state, lineno):
756756
build_dir = os.path.normpath(build_dir)
757757
os.makedirs(build_dir, exist_ok=True)
758758

759-
# output_dir: final location in the builder's directory
760-
dest_dir = os.path.abspath(os.path.join(setup.app.builder.outdir,
761-
source_rel_dir))
762-
os.makedirs(dest_dir, exist_ok=True)
763-
764759
# how to link to files from the RST file
765-
dest_dir_link = os.path.join(relpath(setup.confdir, rst_dir),
766-
source_rel_dir).replace(os.path.sep, '/')
767760
try:
768761
build_dir_link = relpath(build_dir, rst_dir).replace(os.path.sep, '/')
769762
except ValueError:
770763
# on Windows, relpath raises ValueError when path and start are on
771764
# different mounts/drives
772765
build_dir_link = build_dir
773-
source_link = dest_dir_link + '/' + output_base + source_ext
774766

775767
# get list of included rst files so that the output is updated when any
776768
# plots in the included files change. These attributes are modified by the
@@ -791,6 +783,14 @@ def run(arguments, content, options, state_machine, state, lineno):
791783
except ValueError:
792784
pass
793785

786+
# save script (if necessary)
787+
if options['show-source-link']:
788+
Path(build_dir, output_base + source_ext).write_text(
789+
doctest.script_from_examples(code)
790+
if source_file_name == rst_file and is_doctest
791+
else code,
792+
encoding='utf-8')
793+
794794
# make figures
795795
try:
796796
results = render_figures(code,
@@ -837,18 +837,17 @@ def run(arguments, content, options, state_machine, state, lineno):
837837
':%s: %s' % (key, val) for key, val in options.items()
838838
if key in ('alt', 'height', 'width', 'scale', 'align', 'class')]
839839

840-
# Not-None src_link signals the need for a source link in the generated
841-
# html
840+
# Not-None src_name signals the need for a source download in the
841+
# generated html
842842
if j == 0 and options['show-source-link']:
843-
src_link = source_link
843+
src_name = output_base + source_ext
844844
else:
845-
src_link = None
845+
src_name = None
846846

847847
result = jinja2.Template(config.plot_template or TEMPLATE).render(
848848
default_fmt=default_fmt,
849-
dest_dir=dest_dir_link,
850849
build_dir=build_dir_link,
851-
source_link=src_link,
850+
src_name=src_name,
852851
multi_image=len(images) > 1,
853852
options=opts,
854853
images=images,
@@ -862,22 +861,4 @@ def run(arguments, content, options, state_machine, state, lineno):
862861
if total_lines:
863862
state_machine.insert_input(total_lines, source=source_file_name)
864863

865-
# copy image files to builder's output directory, if necessary
866-
Path(dest_dir).mkdir(parents=True, exist_ok=True)
867-
868-
for code_piece, images in results:
869-
for img in images:
870-
for fn in img.filenames():
871-
destimg = os.path.join(dest_dir, os.path.basename(fn))
872-
if fn != destimg:
873-
shutil.copyfile(fn, destimg)
874-
875-
# copy script (if necessary)
876-
if options['show-source-link']:
877-
Path(dest_dir, output_base + source_ext).write_text(
878-
doctest.script_from_examples(code)
879-
if source_file_name == rst_file and is_doctest
880-
else code,
881-
encoding='utf-8')
882-
883864
return errors

lib/matplotlib/tests/test_sphinxext.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def test_tinypages(tmp_path):
1818
shutil.copytree(Path(__file__).parent / 'tinypages', tmp_path,
1919
dirs_exist_ok=True)
2020
html_dir = tmp_path / '_build' / 'html'
21+
img_dir = html_dir / '_images'
2122
doctree_dir = tmp_path / 'doctrees'
2223
# Build the pages with warnings turned into errors
2324
cmd = [sys.executable, '-msphinx', '-W', '-b', 'html',
@@ -35,7 +36,7 @@ def test_tinypages(tmp_path):
3536
build_sphinx_html(tmp_path, doctree_dir, html_dir)
3637

3738
def plot_file(num):
38-
return html_dir / f'some_plots-{num}.png'
39+
return img_dir / f'some_plots-{num}.png'
3940

4041
def plot_directive_file(num):
4142
# This is always next to the doctree dir.
@@ -58,8 +59,8 @@ def plot_directive_file(num):
5859

5960
assert b'# Only a comment' in html_contents
6061
# check plot defined in external file.
61-
assert filecmp.cmp(range_4, html_dir / 'range4.png')
62-
assert filecmp.cmp(range_6, html_dir / 'range6.png')
62+
assert filecmp.cmp(range_4, img_dir / 'range4.png')
63+
assert filecmp.cmp(range_6, img_dir / 'range6.png')
6364
# check if figure caption made it into html file
6465
assert b'This is the caption for plot 15.' in html_contents
6566
# check if figure caption using :caption: made it into html file
@@ -111,13 +112,13 @@ def test_plot_html_show_source_link(tmp_path):
111112
# Make sure source scripts are created by default
112113
html_dir1 = tmp_path / '_build' / 'html1'
113114
build_sphinx_html(tmp_path, doctree_dir, html_dir1)
114-
assert "index-1.py" in [p.name for p in html_dir1.iterdir()]
115+
assert len(list(html_dir1.glob("**/index-1.py"))) == 1
115116
# Make sure source scripts are NOT created when
116117
# plot_html_show_source_link` is False
117118
html_dir2 = tmp_path / '_build' / 'html2'
118119
build_sphinx_html(tmp_path, doctree_dir, html_dir2,
119120
extra_args=['-D', 'plot_html_show_source_link=0'])
120-
assert "index-1.py" not in [p.name for p in html_dir2.iterdir()]
121+
assert len(list(html_dir2.glob("**/index-1.py"))) == 0
121122

122123

123124
@pytest.mark.parametrize('plot_html_show_source_link', [0, 1])
@@ -137,7 +138,7 @@ def test_show_source_link_true(tmp_path, plot_html_show_source_link):
137138
html_dir = tmp_path / '_build' / 'html'
138139
build_sphinx_html(tmp_path, doctree_dir, html_dir, extra_args=[
139140
'-D', f'plot_html_show_source_link={plot_html_show_source_link}'])
140-
assert "index-1.py" in [p.name for p in html_dir.iterdir()]
141+
assert len(list(html_dir.glob("**/index-1.py"))) == 1
141142

142143

143144
@pytest.mark.parametrize('plot_html_show_source_link', [0, 1])
@@ -157,7 +158,7 @@ def test_show_source_link_false(tmp_path, plot_html_show_source_link):
157158
html_dir = tmp_path / '_build' / 'html'
158159
build_sphinx_html(tmp_path, doctree_dir, html_dir, extra_args=[
159160
'-D', f'plot_html_show_source_link={plot_html_show_source_link}'])
160-
assert "index-1.py" not in [p.name for p in html_dir.iterdir()]
161+
assert len(list(html_dir.glob("**/index-1.py"))) == 0
161162

162163

163164
def build_sphinx_html(tmp_path, doctree_dir, html_dir, extra_args=None):

0 commit comments

Comments
 (0)