Skip to content

[ENH] Add option to render multiple images from same cell as single-img#1384

Merged
larsoner merged 14 commits intosphinx-gallery:masterfrom
tsbinns:allow_multi_single_img
Oct 8, 2024
Merged

[ENH] Add option to render multiple images from same cell as single-img#1384
larsoner merged 14 commits intosphinx-gallery:masterfrom
tsbinns:allow_multi_single_img

Conversation

@tsbinns
Copy link
Copy Markdown
Contributor

@tsbinns tsbinns commented Oct 2, 2024

Discussed in #1371.

3 ideas were proposed:

  1. Global no-width-limit: change the CSS max-width for multi-img
  2. A single example no-width-limit: change set the # sphinx_gallery_multi_image = single somewhere in the file
  3. A single block with an example no-width-limit: set the # sphinx_gallery_multi_image_block = single within that block

This PR so far addresses point 2 (adding a config setting for the whole example). Will have a go at point 3 as well.

@tsbinns tsbinns force-pushed the allow_multi_single_img branch from b36efc9 to 0cc1264 Compare October 2, 2024 17:09
Comment on lines +207 to +212
# Determine whether single-img should be converted to multi-img
convert_to_multi_image = True # default is to convert
if block_vars.get("multi_image") is not None: # block setting takes precedence
convert_to_multi_image = block_vars["multi_image"] != "single"
elif block_vars["file_conf"].get("multi_image") is not None: # then file setting
convert_to_multi_image = block_vars["file_conf"]["multi_image"] != "single"
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This in the scraper prioritises checking the block-specific setting, then falls back to the file-wide setting.

@tsbinns
Copy link
Copy Markdown
Contributor Author

tsbinns commented Oct 2, 2024

Will address the failing tests and start to think about new unit tests for these features tomorrow.

Comment on lines +5 to +7
This example demonstrates how to import a local module and how images are stacked when
two plots are created in one code block (see the :doc:`plot_9_multi_image_separate`
example for information on controlling this behaviour). The variable ``N`` from the
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed that the plot exp example discussed images being stacked side-by-side, so added a link to the new example mentioning this behaviour can be changed.

Comment on lines +2 to +7
Force plots to be displayed on separate lines
=============================================
This example demonstrates how the visualisation of multiple plots produced from a single
code block can be controlled. The default behaviour is to stack plots side-by-side,
however this can be overridden to display each plot created by the code block on a
separate line, preserving their size.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new example demonstrating the sphinx_gallery_multi_image and ..._block options.

Comment on lines +1010 to +1014
# Add `sphinx_gallery_multi_image_block` setting to block variables
# (extract config rather than just regex search since the option's value is needed)
script_vars["multi_image"] = py_source_parser.extract_file_config(
block.content
).get("multi_image_block")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be interested if others think there's a better way to handle this.

Of course just a regex search is possible, but unlike the defer_figures flag we need to extract the value of variable, which the extract_file_config() func can handle.

@tsbinns
Copy link
Copy Markdown
Contributor Author

tsbinns commented Oct 3, 2024

Have added entries for the new config options in the docs.

Also added to the unit tests, however they start from the point where save_figures() is called, which overlooks the code where sphinx_gallery_multi_image_block and file_conf are added to block_vars in execute_code_block(). Is this okay?

@larsoner
Copy link
Copy Markdown
Contributor

larsoner commented Oct 7, 2024

Also added to the unit tests, however they start from the point where save_figures() is called, which overlooks the code where sphinx_gallery_multi_image_block and file_conf are added to block_vars in execute_code_block(). Is this okay?

It looks like you added to tinybuild which should be able to test the full functionality, assuming you write the assert statements correctly I think

@tsbinns
Copy link
Copy Markdown
Contributor Author

tsbinns commented Oct 8, 2024

It looks like you added to tinybuild which should be able to test the full functionality, assuming you write the assert statements correctly I think

Okay, I added to test_full checks that the file-wide and block-specific options work using the html output from the examples I added to tinybuild.

@larsoner larsoner merged commit e7b7603 into sphinx-gallery:master Oct 8, 2024
@larsoner
Copy link
Copy Markdown
Contributor

larsoner commented Oct 8, 2024

Thanks @tsbinns !

@tsbinns tsbinns deleted the allow_multi_single_img branch October 8, 2024 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants