|  | 
| 58 | 58 |         The encoding will not be inferred using the ``-*- coding -*-`` | 
| 59 | 59 |         metacomment. | 
| 60 | 60 | 
 | 
| 61 |  | -    context : bool | 
|  | 61 | +    context : bool or str | 
| 62 | 62 |         If provided, the code will be run in the context of all | 
| 63 | 63 |         previous plot directives for which the `:context:` option was | 
| 64 | 64 |         specified.  This only applies to inline code plot directives, | 
| 65 |  | -        not those run from files. | 
|  | 65 | +        not those run from files. If the ``:context: reset`` is specified, | 
|  | 66 | +        the context is reset for this and future plots. | 
| 66 | 67 | 
 | 
| 67 | 68 |     nofigs : bool | 
| 68 | 69 |         If specified, the code block will be run, but no figures will | 
| @@ -249,9 +250,19 @@ def _option_boolean(arg): | 
| 249 | 250 |     else: | 
| 250 | 251 |         raise ValueError('"%s" unknown boolean' % arg) | 
| 251 | 252 | 
 | 
|  | 253 | + | 
|  | 254 | +def _option_context(arg): | 
|  | 255 | +    if arg in [None, 'reset']: | 
|  | 256 | +        return arg | 
|  | 257 | +    else: | 
|  | 258 | +        raise ValueError("argument should be None or 'reset'") | 
|  | 259 | +    return directives.choice(arg, ('None', 'reset')) | 
|  | 260 | + | 
|  | 261 | + | 
| 252 | 262 | def _option_format(arg): | 
| 253 | 263 |     return directives.choice(arg, ('python', 'doctest')) | 
| 254 | 264 | 
 | 
|  | 265 | + | 
| 255 | 266 | def _option_align(arg): | 
| 256 | 267 |     return directives.choice(arg, ("top", "middle", "bottom", "left", "center", | 
| 257 | 268 |                                    "right")) | 
| @@ -299,7 +310,7 @@ def setup(app): | 
| 299 | 310 |                'class': directives.class_option, | 
| 300 | 311 |                'include-source': _option_boolean, | 
| 301 | 312 |                'format': _option_format, | 
| 302 |  | -               'context': directives.flag, | 
|  | 313 | +               'context': _option_context, | 
| 303 | 314 |                'nofigs': directives.flag, | 
| 304 | 315 |                'encoding': directives.encoding | 
| 305 | 316 |                } | 
| @@ -561,7 +572,7 @@ def clear_state(plot_rcparams, close=True): | 
| 561 | 572 |     matplotlib.rcParams.update(plot_rcparams) | 
| 562 | 573 | 
 | 
| 563 | 574 | def render_figures(code, code_path, output_dir, output_base, context, | 
| 564 |  | -                   function_name, config): | 
|  | 575 | +                   function_name, config, context_reset=False): | 
| 565 | 576 |     """ | 
| 566 | 577 |     Run a pyplot script and save the low and high res PNGs and a PDF | 
| 567 | 578 |     in outdir. | 
| @@ -636,6 +647,8 @@ def render_figures(code, code_path, output_dir, output_base, context, | 
| 636 | 647 |     else: | 
| 637 | 648 |         ns = {} | 
| 638 | 649 | 
 | 
|  | 650 | +    if context_reset: | 
|  | 651 | +        clear_state(config.plot_rcparams) | 
| 639 | 652 | 
 | 
| 640 | 653 |     for i, code_piece in enumerate(code_pieces): | 
| 641 | 654 | 
 | 
| @@ -680,6 +693,7 @@ def run(arguments, content, options, state_machine, state, lineno): | 
| 680 | 693 | 
 | 
| 681 | 694 |     options.setdefault('include-source', config.plot_include_source) | 
| 682 | 695 |     context = 'context' in options | 
|  | 696 | +    context_reset = True if (context and options['context'] == 'reset') else False | 
| 683 | 697 | 
 | 
| 684 | 698 |     rst_file = document.attributes['source'] | 
| 685 | 699 |     rst_dir = os.path.dirname(rst_file) | 
| @@ -764,7 +778,8 @@ def run(arguments, content, options, state_machine, state, lineno): | 
| 764 | 778 |     # make figures | 
| 765 | 779 |     try: | 
| 766 | 780 |         results = render_figures(code, source_file_name, build_dir, output_base, | 
| 767 |  | -                                 context, function_name, config) | 
|  | 781 | +                                 context, function_name, config, | 
|  | 782 | +                                 context_reset=context_reset) | 
| 768 | 783 |         errors = [] | 
| 769 | 784 |     except PlotError as err: | 
| 770 | 785 |         reporter = state.memo.reporter | 
|  | 
0 commit comments