Permalink
Browse files

Added more configuration options.

* plot_apply_rcparams
Allows appyling rcParams when context is used.
* plot_working_directory
Allows running plot directive codes in any directory.
* plot_template
Allows customization of template used for generating restructured text.
  • Loading branch information...
1 parent 967c15a commit d30d57c8443d81566b1c29b78eb579932c173593 @abakan abakan committed Jul 27, 2012
Showing with 24 additions and 4 deletions.
  1. +24 −4 lib/matplotlib/sphinxext/plot_directive.py
@@ -105,6 +105,21 @@
A dictionary containing any non-standard rcParams that should
be applied before each plot.
+ plot_apply_rcparams
+ Apply rcParams before each plot. When context is used, rcParams are
+ not applied, and this configuration option overrides this behavior.
+
+ plot_working_directory
+ By default, the working directory will be changed to the directory of
+ the example, so the code can get at its data files, if any. Also its
+ path to sys.path so it can import any helper modules sitting beside it.
+ This configuration option can be used to specify a central directory
+ where data files for all code are located.
+
+ plot_template
+ Provide a customized template for preparing resturctured text.
+
+
"""
from __future__ import print_function
@@ -284,6 +299,9 @@ def setup(app):
app.add_config_value('plot_basedir', None, True)
app.add_config_value('plot_html_show_formats', True, True)
app.add_config_value('plot_rcparams', {}, True)
+ app.add_config_value('plot_apply_rcparams', False, True)
+ app.add_config_value('plot_working_directory', None, True)
+ app.add_config_value('plot_template', None, True)
app.connect('doctree-read', mark_plot_labels)
@@ -445,7 +463,9 @@ def run_code(code, code_path, ns=None, function_name=None):
pwd = os.getcwd()
old_sys_path = list(sys.path)
- if code_path is not None:
+ if setup.config.plot_working_directory:
+ os.chdir(setup.config.plot_working_directory)
+ elif code_path is not None:
dirname = os.path.abspath(os.path.dirname(code_path))
os.chdir(dirname)
sys.path.insert(0, dirname)
@@ -564,7 +584,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
ns = {}
for i, code_piece in enumerate(code_pieces):
- if not context:
+ if not context or config.plot_apply_rcparams:
clear_state(config.plot_rcparams)
run_code(code_piece, code_path, ns, function_name)
@@ -588,7 +608,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
results.append((code_piece, images))
- if not context:
+ if not context or config.plot_apply_rcparams:
clear_state(config.plot_rcparams)
return results
@@ -733,7 +753,7 @@ def run(arguments, content, options, state_machine, state, lineno):
src_link = None
result = format_template(
- TEMPLATE,
+ config.plot_template or TEMPLATE,
dest_dir=dest_dir_link,
build_dir=build_dir_link,
source_link=src_link,

0 comments on commit d30d57c

Please sign in to comment.