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

Skip to content

Commit 84e0589

Browse files
committed
added context and nofigs options to support context between plot directive code blocks
svn path=/branches/v1_0_maint/; revision=8780
1 parent 30ac5e5 commit 84e0589

1 file changed

Lines changed: 65 additions & 32 deletions

File tree

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
non-ASCII encoding, the encoding must be specified using the
4343
`:encoding:` option.
4444
45+
If the `:context:` option is plotted, the code will be run in the
46+
context of all previous plot directives for which the context option
47+
was specified. This only applies to inline code plot directives, not
48+
those run from files.
49+
50+
If the ``:nofigs:`` option is specified, the code block will be run,
51+
but no figures will be inserted. This is usually useful with the
52+
``:context:`` option.
53+
4554
The set of file formats to generate can be specified with the
4655
`plot_formats` configuration variable.
4756
@@ -176,6 +185,10 @@ def relpath(target, base=os.curdir):
176185

177186
template_content_indent = ' '
178187

188+
# the context of the plot for all directives specified with the
189+
# :context: option
190+
plot_context = dict()
191+
179192
def out_of_date(original, derived):
180193
"""
181194
Returns True if derivative is out-of-date wrt original,
@@ -185,17 +198,23 @@ def out_of_date(original, derived):
185198
(os.path.exists(original) and
186199
os.stat(derived).st_mtime < os.stat(original).st_mtime))
187200

188-
def run_code(plot_path, function_name, plot_code):
201+
def run_code(plot_path, function_name, plot_code, context=False):
189202
"""
190203
Import a Python module from a path, and run the function given by
191204
name, if function_name is not None.
192205
"""
206+
193207
# Change the working directory to the directory of the example, so
194208
# it can get at its data files, if any. Add its path to sys.path
195209
# so it can import any helper modules sitting beside it.
210+
196211
if plot_code is not None:
197212
exec_code = 'import numpy as np; import matplotlib.pyplot as plt\n%s'%plot_code
198-
exec(exec_code)
213+
if context:
214+
exec(exec_code, None, plot_context)
215+
else:
216+
exec(exec_code)
217+
199218
else:
200219
pwd = os.getcwd()
201220
path, fname = os.path.split(plot_path)
@@ -251,7 +270,7 @@ def clear_state():
251270
matplotlib.rcParams['figure.figsize'] = (5.5, 4.5)
252271

253272
def render_figures(plot_path, function_name, plot_code, tmpdir, destdir,
254-
formats):
273+
formats, context=False):
255274
"""
256275
Run a pyplot script and save the low and high res PNGs and a PDF
257276
in outdir.
@@ -293,9 +312,10 @@ def render_figures(plot_path, function_name, plot_code, tmpdir, destdir,
293312

294313
# We didn't find the files, so build them
295314

296-
clear_state()
315+
if not context:
316+
clear_state()
297317
try:
298-
run_code(plot_path, function_name, plot_code)
318+
run_code(plot_path, function_name, plot_code, context=context)
299319
except:
300320
s = cbook.exception_to_str("Exception running plot %s" % plot_path)
301321
warnings.warn(s, PlotWarning)
@@ -310,6 +330,16 @@ def render_figures(plot_path, function_name, plot_code, tmpdir, destdir,
310330

311331
def _plot_directive(plot_path, basedir, function_name, plot_code, caption,
312332
options, state_machine):
333+
context = options.has_key('context')
334+
if context:
335+
# remove for figure directive
336+
del options['context']
337+
338+
nofigs = options.has_key('nofigs')
339+
if nofigs:
340+
# remove for figure directive
341+
del options['nofigs']
342+
313343
formats = setup.config.plot_formats
314344
if type(formats) == str:
315345
formats = eval(formats)
@@ -356,7 +386,7 @@ def _plot_directive(plot_path, basedir, function_name, plot_code, caption,
356386

357387
# Generate the figures, and return the number of them
358388
num_figs = render_figures(plot_path, function_name, plot_code, tmpdir,
359-
destdir, formats)
389+
destdir, formats, context=context)
360390

361391
# Now start generating the lines of output
362392
lines = []
@@ -386,32 +416,33 @@ def _plot_directive(plot_path, basedir, function_name, plot_code, caption,
386416
else:
387417
lines = []
388418

389-
if num_figs > 0:
390-
options = ['%s:%s: %s' % (template_content_indent, key, val)
391-
for key, val in options.items()]
392-
options = "\n".join(options)
393-
394-
for i in range(num_figs):
395-
if num_figs == 1:
396-
outname = basename
397-
else:
398-
outname = "%s_%02d" % (basename, i)
399-
400-
# Copy the linked-to files to the destination within the build tree,
401-
# and add a link for them
402-
links = []
403-
if plot_code is None:
404-
links.append('`source code <%(linkdir)s/%(basename)s.py>`__')
405-
for format, dpi in formats[1:]:
406-
links.append('`%s <%s/%s.%s>`__' % (format, linkdir, outname, format))
407-
if len(links):
408-
links = '[%s]' % (', '.join(links) % locals())
409-
else:
410-
links = ''
411-
412-
lines.extend((template % locals()).split('\n'))
413-
else:
414-
lines.extend((exception_template % locals()).split('\n'))
419+
if not nofigs:
420+
if num_figs > 0:
421+
options = ['%s:%s: %s' % (template_content_indent, key, val)
422+
for key, val in options.items()]
423+
options = "\n".join(options)
424+
425+
for i in range(num_figs):
426+
if num_figs == 1:
427+
outname = basename
428+
else:
429+
outname = "%s_%02d" % (basename, i)
430+
431+
# Copy the linked-to files to the destination within the build tree,
432+
# and add a link for them
433+
links = []
434+
if plot_code is None:
435+
links.append('`source code <%(linkdir)s/%(basename)s.py>`__')
436+
for format, dpi in formats[1:]:
437+
links.append('`%s <%s/%s.%s>`__' % (format, linkdir, outname, format))
438+
if len(links):
439+
links = '[%s]' % (', '.join(links) % locals())
440+
else:
441+
links = ''
442+
443+
lines.extend((template % locals()).split('\n'))
444+
else:
445+
lines.extend((exception_template % locals()).split('\n'))
415446

416447
if len(lines):
417448
state_machine.insert_input(
@@ -496,6 +527,8 @@ def setup(app):
496527
'align': align,
497528
'class': directives.class_option,
498529
'include-source': directives.flag,
530+
'context': directives.flag,
531+
'nofigs': directives.flag,
499532
'encoding': directives.encoding }
500533

501534
app.add_directive('plot', plot_directive, True, (0, 2, 0), **options)

0 commit comments

Comments
 (0)