Skip to content

Commit a499421

Browse files
takluyverMeeseeksDev[bot]
authored andcommitted
Backport PR #10186: 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 gh-8733
1 parent 6091d8c commit a499421

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
@@ -127,6 +127,7 @@
127127

128128
# Stdlib
129129
import atexit
130+
import errno
130131
import os
131132
import re
132133
import sys
@@ -365,9 +366,9 @@ def process_image(self, decorator):
365366
source_dir = self.source_dir
366367
saveargs = decorator.split(' ')
367368
filename = saveargs[1]
368-
# insert relative path to image file in source
369-
outfile = os.path.relpath(os.path.join(savefig_dir,filename),
370-
source_dir)
369+
# insert relative path to image file in source (as absolute path for Sphinx)
370+
outfile = '/' + os.path.relpath(os.path.join(savefig_dir,filename),
371+
source_dir)
371372

372373
imagerows = ['.. image:: %s'%outfile]
373374

@@ -849,14 +850,9 @@ def get_config_options(self):
849850
config = self.state.document.settings.env.config
850851

851852
# get config variables to set figure output directory
852-
outdir = self.state.document.settings.env.app.outdir
853853
savefig_dir = config.ipython_savefig_dir
854-
source_dir = os.path.dirname(self.state.document.current_source)
855-
if savefig_dir is None:
856-
savefig_dir = config.html_static_path or '_static'
857-
if isinstance(savefig_dir, list):
858-
savefig_dir = os.path.join(*savefig_dir)
859-
savefig_dir = os.path.join(outdir, savefig_dir)
854+
source_dir = self.state.document.settings.env.srcdir
855+
savefig_dir = os.path.join(source_dir, savefig_dir)
860856

861857
# get regex and prompt stuff
862858
rgxin = config.ipython_rgxin
@@ -875,6 +871,12 @@ def setup(self):
875871
(savefig_dir, source_dir, rgxin, rgxout, promptin, promptout,
876872
mplbackend, exec_lines, hold_count) = self.get_config_options()
877873

874+
try:
875+
os.makedirs(savefig_dir)
876+
except OSError as e:
877+
if e.errno != errno.EEXIST:
878+
raise
879+
878880
if self.shell is None:
879881
# We will be here many times. However, when the
880882
# EmbeddedSphinxShell is created, its interactive shell member
@@ -983,7 +985,7 @@ def setup(app):
983985
setup.app = app
984986

985987
app.add_directive('ipython', IPythonDirective)
986-
app.add_config_value('ipython_savefig_dir', None, 'env')
988+
app.add_config_value('ipython_savefig_dir', 'savefig', 'env')
987989
app.add_config_value('ipython_rgxin',
988990
re.compile('In \[(\d+)\]:\s?(.*)\s*'), 'env')
989991
app.add_config_value('ipython_rgxout',

0 commit comments

Comments
 (0)