Skip to content

Commit

Permalink
Allow :context: directive to take 'reset' option. Fixes matplotlib#2892.
Browse files Browse the repository at this point in the history
  • Loading branch information
astrofrog committed Apr 17, 2014
1 parent 19069e5 commit 14a47c4
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/matplotlib/sphinxext/plot_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@
The encoding will not be inferred using the ``-*- coding -*-``
metacomment.
context : bool
context : bool or str
If provided, the code will be run in the context of all
previous plot directives for which the `:context:` option was
specified. This only applies to inline code plot directives,
not those run from files.
not those run from files. If the ``:context: reset`` is specified,
the context is reset for this and future plots.
nofigs : bool
If specified, the code block will be run, but no figures will
Expand Down Expand Up @@ -249,9 +250,19 @@ def _option_boolean(arg):
else:
raise ValueError('"%s" unknown boolean' % arg)


def _option_context(arg):
if arg in [None, 'reset']:
return arg
else:
raise ValueError("argument should be None or 'reset'")
return directives.choice(arg, ('None', 'reset'))


def _option_format(arg):
return directives.choice(arg, ('python', 'doctest'))


def _option_align(arg):
return directives.choice(arg, ("top", "middle", "bottom", "left", "center",
"right"))
Expand Down Expand Up @@ -299,7 +310,7 @@ def setup(app):
'class': directives.class_option,
'include-source': _option_boolean,
'format': _option_format,
'context': directives.flag,
'context': _option_context,
'nofigs': directives.flag,
'encoding': directives.encoding
}
Expand Down Expand Up @@ -561,7 +572,7 @@ def clear_state(plot_rcparams, close=True):
matplotlib.rcParams.update(plot_rcparams)

def render_figures(code, code_path, output_dir, output_base, context,
function_name, config):
function_name, config, context_reset=False):
"""
Run a pyplot script and save the low and high res PNGs and a PDF
in outdir.
Expand Down Expand Up @@ -636,6 +647,8 @@ def render_figures(code, code_path, output_dir, output_base, context,
else:
ns = {}

if context_reset:
clear_state(config.plot_rcparams)

for i, code_piece in enumerate(code_pieces):

Expand Down Expand Up @@ -680,6 +693,7 @@ def run(arguments, content, options, state_machine, state, lineno):

options.setdefault('include-source', config.plot_include_source)
context = 'context' in options
context_reset = True if (context and options['context'] == 'reset') else False

rst_file = document.attributes['source']
rst_dir = os.path.dirname(rst_file)
Expand Down Expand Up @@ -764,7 +778,8 @@ def run(arguments, content, options, state_machine, state, lineno):
# make figures
try:
results = render_figures(code, source_file_name, build_dir, output_base,
context, function_name, config)
context, function_name, config,
context_reset=context_reset)
errors = []
except PlotError as err:
reporter = state.memo.reporter
Expand Down

0 comments on commit 14a47c4

Please sign in to comment.