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

Skip to content

Add the ability to optionally include images in a gallery that were saved to disk #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions sphinx_gallery/gen_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from __future__ import division, print_function, absolute_import
from time import time
import codecs
import glob
import hashlib
import os
import re
Expand Down Expand Up @@ -229,18 +230,19 @@ def md5sum_is_current(src_file):
return False


def save_figures(image_path, fig_count, gallery_conf):
def save_figures(image_path, execution_path, fig_count, gallery_conf):
"""Save all open matplotlib figures of the example code-block

Parameters
----------
image_path : str
Path where plots are saved (format string which accepts figure number)
execution_path : str
Path where the example script was executed.
fig_count : int
Previous figure number count. Figure number add from this number
gallery_conf : dict
Contains the configuration of Sphinx-Gallery

Returns
-------
images_rst : str
Expand Down Expand Up @@ -281,6 +283,14 @@ def save_figures(image_path, fig_count, gallery_conf):
figure_list.append(current_fig)
mlab.close(all=True)

if gallery_conf.get('find_saved_figures', False):
png_paths = glob.glob(os.sep.join([execution_path, '*.png']))
cur_count = len(figure_list)
for fig_num, path in enumerate(png_paths, start=1):
current_fig = image_path.format(fig_count + fig_num + cur_count)
shutil.move(path, current_fig)
figure_list.append(current_fig)

return figure_rst(figure_list, gallery_conf['src_dir'])


Expand Down Expand Up @@ -482,8 +492,11 @@ def execute_code_block(code_block, example_globals,
if my_stdout:
stdout = CODE_OUTPUT.format(indent(my_stdout, u' ' * 4))
os.chdir(cwd)
images_rst, fig_num = save_figures(block_vars['image_path'],
block_vars['fig_count'], gallery_conf)
images_rst, fig_num = save_figures(
block_vars['image_path'],
os.path.dirname(src_file),
block_vars['fig_count'],
gallery_conf)

except Exception:
formatted_exception = traceback.format_exc()
Expand Down
4 changes: 2 additions & 2 deletions sphinx_gallery/tests/test_gen_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,14 @@ def test_save_figures():
mlab.test_plot3d()
plt.plot(1, 1)
fname_template = os.path.join(gallery_conf['gallery_dir'], 'image{0}.png')
image_rst, fig_num = sg.save_figures(fname_template, 0, gallery_conf)
image_rst, fig_num = sg.save_figures(fname_template, '', 0, gallery_conf)
assert_equal(fig_num, 2)
assert '/image1.png' in image_rst
assert '/image2.png' in image_rst

mlab.test_plot3d()
plt.plot(1, 1)
image_rst, fig_num = sg.save_figures(fname_template, 2, gallery_conf)
image_rst, fig_num = sg.save_figures(fname_template, '', 2, gallery_conf)
assert_equal(fig_num, 2)
assert '/image2.png' not in image_rst
assert '/image3.png' in image_rst
Expand Down