|
47 | 47 |
|
48 | 48 | The ``.. plot::`` directive supports the following options:
|
49 | 49 |
|
| 50 | +``:output-base-name:`` : str |
| 51 | + The base name (without the extension) of the outputted image files. The |
| 52 | + default is to use the same name as the input script, or the name of |
| 53 | + the RST document if no script is provided. The string can include the |
| 54 | + format ``{counter}`` to use an incremented counter. For example, |
| 55 | + ``'plot-{counter}'`` will create files like ``plot-1.png``, ``plot-2.png``, |
| 56 | + and so on. If the ``{counter}`` is not provided, two plots with the same |
| 57 | + output-base-name may overwrite each other. |
| 58 | +
|
50 | 59 | ``:format:`` : {'python', 'doctest'}
|
51 | 60 | The format of the input. If unset, the format is auto-detected.
|
52 | 61 |
|
|
88 | 97 |
|
89 | 98 | The plot directive has the following configuration options:
|
90 | 99 |
|
| 100 | +plot_output_base_name |
| 101 | + Default value for the output-base-name option (default is to use the name |
| 102 | + of the input script, or the name of the RST file if no script is provided) |
| 103 | +
|
91 | 104 | plot_include_source
|
92 | 105 | Default value for the include-source option (default: False).
|
93 | 106 |
|
@@ -265,6 +278,7 @@ class PlotDirective(Directive):
|
265 | 278 | 'scale': directives.nonnegative_int,
|
266 | 279 | 'align': Image.align,
|
267 | 280 | 'class': directives.class_option,
|
| 281 | + 'output-base-name': directives.unchanged, |
268 | 282 | 'include-source': _option_boolean,
|
269 | 283 | 'show-source-link': _option_boolean,
|
270 | 284 | 'format': _option_format,
|
@@ -299,6 +313,7 @@ def setup(app):
|
299 | 313 | app.add_config_value('plot_pre_code', None, True)
|
300 | 314 | app.add_config_value('plot_include_source', False, True)
|
301 | 315 | app.add_config_value('plot_html_show_source_link', True, True)
|
| 316 | + app.add_config_value('plot_output_base_name', None, True) |
302 | 317 | app.add_config_value('plot_formats', ['png', 'hires.png', 'pdf'], True)
|
303 | 318 | app.add_config_value('plot_basedir', None, True)
|
304 | 319 | app.add_config_value('plot_html_show_formats', True, True)
|
@@ -734,6 +749,7 @@ def run(arguments, content, options, state_machine, state, lineno):
|
734 | 749 |
|
735 | 750 | options.setdefault('include-source', config.plot_include_source)
|
736 | 751 | options.setdefault('show-source-link', config.plot_html_show_source_link)
|
| 752 | + options.setdefault('output-base-name', config.plot_output_base_name) |
737 | 753 |
|
738 | 754 | if 'class' in options:
|
739 | 755 | # classes are parsed into a list of string, and output by simply
|
@@ -775,14 +791,20 @@ def run(arguments, content, options, state_machine, state, lineno):
|
775 | 791 | function_name = None
|
776 | 792 |
|
777 | 793 | code = Path(source_file_name).read_text(encoding='utf-8')
|
778 |
| - output_base = os.path.basename(source_file_name) |
| 794 | + if options['output-base-name']: |
| 795 | + output_base = options['output-base-name'] |
| 796 | + else: |
| 797 | + output_base = os.path.basename(source_file_name) |
779 | 798 | else:
|
780 | 799 | source_file_name = rst_file
|
781 | 800 | code = textwrap.dedent("\n".join(map(str, content)))
|
782 | 801 | counter = document.attributes.get('_plot_counter', 0) + 1
|
783 | 802 | document.attributes['_plot_counter'] = counter
|
784 | 803 | base, ext = os.path.splitext(os.path.basename(source_file_name))
|
785 |
| - output_base = '%s-%d.py' % (base, counter) |
| 804 | + if options['output-base-name']: |
| 805 | + output_base = options['output-base-name'].format(counter=counter) |
| 806 | + else: |
| 807 | + output_base = '%s-%d.py' % (base, counter) |
786 | 808 | function_name = None
|
787 | 809 | caption = options.get('caption', '')
|
788 | 810 |
|
|
0 commit comments