42
42
non-ASCII encoding, the encoding must be specified using the
43
43
`:encoding:` option.
44
44
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
+
45
54
The set of file formats to generate can be specified with the
46
55
`plot_formats` configuration variable.
47
56
@@ -176,6 +185,10 @@ def relpath(target, base=os.curdir):
176
185
177
186
template_content_indent = ' '
178
187
188
+ # the context of the plot for all directives specified with the
189
+ # :context: option
190
+ plot_context = dict ()
191
+
179
192
def out_of_date (original , derived ):
180
193
"""
181
194
Returns True if derivative is out-of-date wrt original,
@@ -185,17 +198,23 @@ def out_of_date(original, derived):
185
198
(os .path .exists (original ) and
186
199
os .stat (derived ).st_mtime < os .stat (original ).st_mtime ))
187
200
188
- def run_code (plot_path , function_name , plot_code ):
201
+ def run_code (plot_path , function_name , plot_code , context = False ):
189
202
"""
190
203
Import a Python module from a path, and run the function given by
191
204
name, if function_name is not None.
192
205
"""
206
+
193
207
# Change the working directory to the directory of the example, so
194
208
# it can get at its data files, if any. Add its path to sys.path
195
209
# so it can import any helper modules sitting beside it.
210
+
196
211
if plot_code is not None :
197
212
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
+
199
218
else :
200
219
pwd = os .getcwd ()
201
220
path , fname = os .path .split (plot_path )
@@ -251,7 +270,7 @@ def clear_state():
251
270
matplotlib .rcParams ['figure.figsize' ] = (5.5 , 4.5 )
252
271
253
272
def render_figures (plot_path , function_name , plot_code , tmpdir , destdir ,
254
- formats ):
273
+ formats , context = False ):
255
274
"""
256
275
Run a pyplot script and save the low and high res PNGs and a PDF
257
276
in outdir.
@@ -293,9 +312,10 @@ def render_figures(plot_path, function_name, plot_code, tmpdir, destdir,
293
312
294
313
# We didn't find the files, so build them
295
314
296
- clear_state ()
315
+ if not context :
316
+ clear_state ()
297
317
try :
298
- run_code (plot_path , function_name , plot_code )
318
+ run_code (plot_path , function_name , plot_code , context = context )
299
319
except :
300
320
s = cbook .exception_to_str ("Exception running plot %s" % plot_path )
301
321
warnings .warn (s , PlotWarning )
@@ -310,6 +330,16 @@ def render_figures(plot_path, function_name, plot_code, tmpdir, destdir,
310
330
311
331
def _plot_directive (plot_path , basedir , function_name , plot_code , caption ,
312
332
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
+
313
343
formats = setup .config .plot_formats
314
344
if type (formats ) == str :
315
345
formats = eval (formats )
@@ -356,7 +386,7 @@ def _plot_directive(plot_path, basedir, function_name, plot_code, caption,
356
386
357
387
# Generate the figures, and return the number of them
358
388
num_figs = render_figures (plot_path , function_name , plot_code , tmpdir ,
359
- destdir , formats )
389
+ destdir , formats , context = context )
360
390
361
391
# Now start generating the lines of output
362
392
lines = []
@@ -386,32 +416,33 @@ def _plot_directive(plot_path, basedir, function_name, plot_code, caption,
386
416
else :
387
417
lines = []
388
418
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 ' ))
415
446
416
447
if len (lines ):
417
448
state_machine .insert_input (
@@ -496,6 +527,8 @@ def setup(app):
496
527
'align' : align ,
497
528
'class' : directives .class_option ,
498
529
'include-source' : directives .flag ,
530
+ 'context' : directives .flag ,
531
+ 'nofigs' : directives .flag ,
499
532
'encoding' : directives .encoding }
500
533
501
534
app .add_directive ('plot' , plot_directive , True , (0 , 2 , 0 ), ** options )
0 commit comments