-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Add a filename-prefix option to the Sphinx plot directive #28187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
615017a
b432962
426abc7
8485bfd
f94a932
19daf49
8f05ba6
1fa88dd
a22fcc3
86fb167
e0be21e
f322125
fc33c38
7d416cf
ce23c88
f654a74
13e5291
20bed26
7f56c94
550e382
d4f2440
bab3aaf
c8eba90
f4f1fbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,15 @@ | |
|
|
||
| The ``.. plot::`` directive supports the following options: | ||
|
|
||
| ``:output-base-name:`` : str | ||
| The base name (without the extension) of the outputted image files. The | ||
| default is to use the same name as the input script, or the name of | ||
| the RST document if no script is provided. The string can include the | ||
| format ``{counter}`` to use an incremented counter. For example, | ||
| ``'plot-{counter}'`` will create files like ``plot-1.png``, ``plot-2.png``, | ||
| and so on. If the ``{counter}`` is not provided, two plots with the same | ||
| output-base-name may overwrite each other. | ||
|
QuLogic marked this conversation as resolved.
Outdated
|
||
|
|
||
| ``:format:`` : {'python', 'doctest'} | ||
| The format of the input. If unset, the format is auto-detected. | ||
|
|
||
|
|
@@ -88,6 +97,10 @@ | |
|
|
||
| The plot directive has the following configuration options: | ||
|
|
||
| plot_output_base_name | ||
| Default value for the output-base-name option (default is to use the name | ||
| of the input script, or the name of the RST file if no script is provided) | ||
|
|
||
| plot_include_source | ||
| Default value for the include-source option (default: False). | ||
|
|
||
|
|
@@ -265,6 +278,7 @@ class PlotDirective(Directive): | |
| 'scale': directives.nonnegative_int, | ||
| 'align': Image.align, | ||
| 'class': directives.class_option, | ||
| 'output-base-name': directives.unchanged, | ||
| 'include-source': _option_boolean, | ||
| 'show-source-link': _option_boolean, | ||
| 'format': _option_format, | ||
|
|
@@ -299,6 +313,7 @@ def setup(app): | |
| app.add_config_value('plot_pre_code', None, True) | ||
| app.add_config_value('plot_include_source', False, True) | ||
| app.add_config_value('plot_html_show_source_link', True, True) | ||
| app.add_config_value('plot_output_base_name', None, True) | ||
| app.add_config_value('plot_formats', ['png', 'hires.png', 'pdf'], True) | ||
| app.add_config_value('plot_basedir', None, True) | ||
| app.add_config_value('plot_html_show_formats', True, True) | ||
|
|
@@ -734,6 +749,7 @@ def run(arguments, content, options, state_machine, state, lineno): | |
|
|
||
| options.setdefault('include-source', config.plot_include_source) | ||
| options.setdefault('show-source-link', config.plot_html_show_source_link) | ||
| options.setdefault('output-base-name', config.plot_output_base_name) | ||
|
|
||
| if 'class' in options: | ||
| # 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): | |
| function_name = None | ||
|
|
||
| code = Path(source_file_name).read_text(encoding='utf-8') | ||
| output_base = os.path.basename(source_file_name) | ||
| if options['output-base-name']: | ||
| output_base = options['output-base-name'] | ||
| else: | ||
| output_base = os.path.basename(source_file_name) | ||
| else: | ||
| source_file_name = rst_file | ||
| code = textwrap.dedent("\n".join(map(str, content))) | ||
| counter = document.attributes.get('_plot_counter', 0) + 1 | ||
| document.attributes['_plot_counter'] = counter | ||
| base, ext = os.path.splitext(os.path.basename(source_file_name)) | ||
|
asmeurer marked this conversation as resolved.
Outdated
|
||
| output_base = '%s-%d.py' % (base, counter) | ||
| if options['output-base-name']: | ||
| output_base = options['output-base-name'].format(counter=counter) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we prefix in the script/rst file name here as well? If you have duplicate output names in the same rst file that is a quick search to find and fix, but if you have the collision across multiple rst files it could be much more annoying to find where they are colliding.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well my thinking here is also that users should have full control over the filename that is produced. If we always inject something into the name, that won't be the case. For instance, if you right-click and save a plot, this will be the filename used for the image. Really, we need to figure out how to error if the same name is used twice. My naive idea of checking if the file already exists doesn't work because it could just exist from a previous build. It's probably possible to keep a global list of already used filenames, but I'll have to figure out how to make that work with partial Sphinx rebuilds. |
||
| else: | ||
| output_base = '%s-%d.py' % (base, counter) | ||
| function_name = None | ||
| caption = options.get('caption', '') | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really only the filename base or can it be a relative or absolute path without extension? Either case should be documented. Also, since you start giving users control over the created files, you probably should mention where they go.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just the file name. The files go wherever Sphinx puts them (it looks like it's in the
_imagesdirectory, but I don't know if that's configurable).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification. In that case
*-base-namemakes sense.Two further thoughts:
image-base-namebe better (because more concrete) thanoutput-base-name?:output-base-name: ../escaped_from_image_dir/myimageornested_dir/myimage?