Skip to content

Commit c9e8860

Browse files
committed
Put savefig images back in source directory
Simplifying the logic a bit. I think there was an issue with the way we were finding the source directory: when it included a docstring from code, the source directory was that of the code. This always uses the main source directory of the docs. Closes ipythongh-8733
1 parent 9008fe7 commit c9e8860

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

‎IPython/sphinxext/ipython_directive.py‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126

127127
# Stdlib
128128
import atexit
129+
import errno
129130
import os
130131
import re
131132
import sys
@@ -358,9 +359,9 @@ def process_image(self, decorator):
358359
source_dir = self.source_dir
359360
saveargs = decorator.split(' ')
360361
filename = saveargs[1]
361-
# insert relative path to image file in source
362-
outfile = os.path.relpath(os.path.join(savefig_dir,filename),
363-
source_dir)
362+
# insert relative path to image file in source (as absolute path for Sphinx)
363+
outfile = '/' + os.path.relpath(os.path.join(savefig_dir,filename),
364+
source_dir)
364365

365366
imagerows = ['.. image:: %s'%outfile]
366367

@@ -842,14 +843,9 @@ def get_config_options(self):
842843
config = self.state.document.settings.env.config
843844

844845
# get config variables to set figure output directory
845-
outdir = self.state.document.settings.env.app.outdir
846846
savefig_dir = config.ipython_savefig_dir
847-
source_dir = os.path.dirname(self.state.document.current_source)
848-
if savefig_dir is None:
849-
savefig_dir = config.html_static_path or '_static'
850-
if isinstance(savefig_dir, list):
851-
savefig_dir = os.path.join(*savefig_dir)
852-
savefig_dir = os.path.join(outdir, savefig_dir)
847+
source_dir = self.state.document.settings.env.srcdir
848+
savefig_dir = os.path.join(source_dir, savefig_dir)
853849

854850
# get regex and prompt stuff
855851
rgxin = config.ipython_rgxin
@@ -868,6 +864,12 @@ def setup(self):
868864
(savefig_dir, source_dir, rgxin, rgxout, promptin, promptout,
869865
mplbackend, exec_lines, hold_count) = self.get_config_options()
870866

867+
try:
868+
os.makedirs(savefig_dir)
869+
except OSError as e:
870+
if e.errno != errno.EEXIST:
871+
raise
872+
871873
if self.shell is None:
872874
# We will be here many times. However, when the
873875
# EmbeddedSphinxShell is created, its interactive shell member
@@ -976,7 +978,7 @@ def setup(app):
976978
setup.app = app
977979

978980
app.add_directive('ipython', IPythonDirective)
979-
app.add_config_value('ipython_savefig_dir', None, 'env')
981+
app.add_config_value('ipython_savefig_dir', 'savefig', 'env')
980982
app.add_config_value('ipython_rgxin',
981983
re.compile('In \[(\d+)\]:\s?(.*)\s*'), 'env')
982984
app.add_config_value('ipython_rgxout',

0 commit comments

Comments
 (0)