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

Skip to content

Commit a429c41

Browse files
committed
Merge pull request #2913 from astrofrog/reset-context-plot-directive
Allow :context: directive to take 'reset' option. Fixes #2892.
2 parents 19069e5 + 0679520 commit a429c41

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

doc/users/whats_new.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,14 @@ url as a link in output SVGs. This allows one to make clickable text in
241241
saved figures using the url kwarg of the :class:`~matplotlib.text.Text`
242242
class.
243243

244+
Sphinx extensions
245+
-----------------
246+
247+
The ``:context:`` directive in the `~matplotlib.sphinxext.plot_directive`
248+
Sphinx extension can now accept an optional ``reset`` setting, which will
249+
cause the context to be reset. This allows more than one distinct context to
250+
be present in documentation. To enable this option, use ``:context: reset``
251+
instead of ``:context:`` any time you want to reset the context.
244252

245253
.. _whats-new-1-3:
246254

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@
5858
The encoding will not be inferred using the ``-*- coding -*-``
5959
metacomment.
6060
61-
context : bool
61+
context : bool or str
6262
If provided, the code will be run in the context of all
6363
previous plot directives for which the `:context:` option was
6464
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.
6667
6768
nofigs : bool
6869
If specified, the code block will be run, but no figures will
@@ -249,9 +250,19 @@ def _option_boolean(arg):
249250
else:
250251
raise ValueError('"%s" unknown boolean' % arg)
251252

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+
252262
def _option_format(arg):
253263
return directives.choice(arg, ('python', 'doctest'))
254264

265+
255266
def _option_align(arg):
256267
return directives.choice(arg, ("top", "middle", "bottom", "left", "center",
257268
"right"))
@@ -299,7 +310,7 @@ def setup(app):
299310
'class': directives.class_option,
300311
'include-source': _option_boolean,
301312
'format': _option_format,
302-
'context': directives.flag,
313+
'context': _option_context,
303314
'nofigs': directives.flag,
304315
'encoding': directives.encoding
305316
}
@@ -561,7 +572,7 @@ def clear_state(plot_rcparams, close=True):
561572
matplotlib.rcParams.update(plot_rcparams)
562573

563574
def render_figures(code, code_path, output_dir, output_base, context,
564-
function_name, config):
575+
function_name, config, context_reset=False):
565576
"""
566577
Run a pyplot script and save the low and high res PNGs and a PDF
567578
in outdir.
@@ -636,6 +647,8 @@ def render_figures(code, code_path, output_dir, output_base, context,
636647
else:
637648
ns = {}
638649

650+
if context_reset:
651+
clear_state(config.plot_rcparams)
639652

640653
for i, code_piece in enumerate(code_pieces):
641654

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

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

684698
rst_file = document.attributes['source']
685699
rst_dir = os.path.dirname(rst_file)
@@ -764,7 +778,8 @@ def run(arguments, content, options, state_machine, state, lineno):
764778
# make figures
765779
try:
766780
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)
768783
errors = []
769784
except PlotError as err:
770785
reporter = state.memo.reporter

0 commit comments

Comments
 (0)