-
Notifications
You must be signed in to change notification settings - Fork 207
[ENH] Add custom thumbnails for failing examples #1313
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
[ENH] Add custom thumbnails for failing examples #1313
Conversation
It would be best to keep backward compat with existing behavior. So in other words, if nobody changes anything, the code should behave as before. So one option would be to make any new behavior as opt-in. For example if you add:
to your example, even if the example fails sphinx-gallery could use the standard, non-failing thumbnail processing path. So if you have matplotlib outputs or set |
Makes sense, then the change is also super simple: sphinx-gallery/sphinx_gallery/gen_rst.py Lines 394 to 397 in f066eb5
And in the example: sphinx-gallery/examples/no_output/plot_raise_thumbnail.py Lines 1 to 19 in f066eb5
If people are happy with this, should the new variable be listed in the "Configurations inside examples" section of the User guide? |
Yeah I think that makes sense! |
Sorry, just to clarify, is this a thumbs up just for the new solution or also for adding to the user guide? |
To both -- any new (inline or otherwise) configuration option should be documented in our docs 👍 |
Updated the name of the new example to better reflect what it now demonstrates. Also added an entry into the user guide explaining the new option. |
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.
Rendered HTML and docs look good, thanks @tsbinns !
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.
Argh actually I just realized that there is no modified test. One option would be to modify an example in sphinx_gallery/tests/tinybuild
to have this new line and then assert that "broken"
does not show up in the rendered HTML for example
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.
Nice addition, LGTM, just needs the test as Eric suggested.
Co-authored-by: Lucy Liu <[email protected]>
@tsbinns don't forget about the test when you get a chance! |
@larsoner Sorry, have it on my to-do list but haven't gotten around to it yet. Will try to sort soon! |
for more information, see https://pre-commit.ci
Fixed a merge conflict - hopefully correctly! |
Sorry again for the delay. Have now added two examples to
The thumbnails are tested here: sphinx-gallery/sphinx_gallery/tests/test_full.py Lines 333 to 371 in 7c33f22
I can't seem to properly run Also added |
@@ -1239,6 +1239,5 @@ def test_newlines(log_collector_wrap): | |||
assert tee.newlines == tee.output.newlines | |||
|
|||
|
|||
# TODO: test that broken thumbnail does appear when needed |
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.
Since the new test also looks at the default behaviour for expected failing examples, can remove this TODO
from elsewhere.
assert len(suite) == 3 | ||
assert suite[0].attrib["classname"] == "plot_numpy_matplotlib" | ||
assert suite[0][0].tag == "failure", suite[0].attrib["classname"] | ||
assert suite[0][0].attrib["message"].startswith("RuntimeError: Forcing") | ||
assert suite[1].attrib["classname"] == "plot_scraper_broken" | ||
assert suite[1][0].tag == "skipped", suite[1].attrib["classname"] | ||
assert suite[2].attrib["classname"] == "plot_future_imports_broken" | ||
assert suite[2][0].tag == "failure", suite[2].attrib["classname"] | ||
assert suite[2][0].attrib["message"] == "Passed even though it was marked to fail" | ||
skips_and_fails = [ | ||
{ | ||
"classname": "plot_failing_example", | ||
"tag": "skipped", | ||
"message": None, | ||
}, | ||
{ | ||
"classname": "plot_failing_example_thumbnail", | ||
"tag": "skipped", | ||
"message": None, | ||
}, | ||
{ | ||
"classname": "plot_numpy_matplotlib", | ||
"tag": "failure", | ||
"message": "RuntimeError: Forcing", | ||
}, | ||
{ | ||
"classname": "plot_scraper_broken", | ||
"tag": "skipped", | ||
"message": None, | ||
}, | ||
{ | ||
"classname": "plot_future_imports_broken", | ||
"tag": "failure", | ||
"message": "Passed even though it was marked to fail", | ||
}, | ||
] | ||
assert len(suite) == len(skips_and_fails) | ||
for this_suite, this_example in zip(suite, skips_and_fails): | ||
assert this_suite.attrib["classname"] == this_example["classname"] | ||
assert this_suite[0].tag == this_example["tag"], this_suite.attrib["classname"] | ||
if this_example["message"] is not None: | ||
assert this_suite[0].attrib["message"].startswith(this_example["message"]) |
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.
Needed to add new items for the 2 new examples. Thought perhaps this was easier to read.
That should now be all the tests working. |
Looks good marking for merge-when-green, thanks in advance @tsbinns ! |
… to version 0.17.0 v0.17.0 ------- Support for Python 3.8 and Sphinx 4 dropped in this release. Requirement is now Python >= 3.9 and Sphinx >= 5. **Implemented enhancements:** - Introduction tooltip corresponds to the first paragraph `#1344 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1344>`__ (`fgmacedo <https://github.com/fgmacedo>`__) - FIX Jupyterlite in CircleCI artifact `#1336 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1336>`__ (`lesteve <https://github.com/lesteve>`__) - MNT: Rename README.rst to GALLERY_HEADER.rst `#1321 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1321>`__ (`timhoffm <https://github.com/timhoffm>`__) - [ENH] Add custom thumbnails for failing examples `#1313 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1313>`__ (`tsbinns <https://github.com/tsbinns>`__) - ENH integrate download/launcher links into ``pydata-sphinx-theme`` secondary sidebar `#1312 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1312>`__ (`Charlie-XIAO <https://github.com/Charlie-XIAO>`__) - add option for zip downloads `#1299 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1299>`__ (`jamiecook <https://github.com/jamiecook>`__) - Allow setting animation format from gallery config `#1243 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1243>`__ (`QuLogic <https://github.com/QuLogic>`__) (NEWS truncated at 15 lines)
Problem
Belated follow-up of #1220.
In short, currently expected failing examples use the "BROKEN" stamp as their thumbnail, which cannot be overwitten by including e.g.
# sphinx_gallery_thumbnail_path = '_static/my_preferred_thumbnail.png'
in the example file.Setting a different thumbnail could be useful if there is only one part of your example which is designed to fail, but you don't want it to appear as if the entire example is about code failure from the thumbnail.
Possible Solution
This is just one idea I had. I am very open to suggestions if someone thinks there is a better solution.
In the
save_thumbnail()
function ofgen_rst.py
, the path to the thumbnails is set for the examples.Currently, in the case where it is an expected failing example, the
sphinx_gallery_thumbnail_path
specified in the file will be carried over and the proper path to the thumbnail will be generated, however this will be ignored and the path to the "BROKEN" stamp file will be set.Summary of the changes:
ignore_thumbnail
that is defaultFalse
sphinx-gallery/sphinx_gallery/gen_rst.py
Line 372 in fc1d423
ignore_thumbnail
is set toTrue
sphinx-gallery/sphinx_gallery/gen_rst.py
Lines 380 to 381 in fc1d423
ignore_thumbnail==True
(i.e. no thumbnail was specified), the "BROKEN" stamp is usedsphinx-gallery/sphinx_gallery/gen_rst.py
Lines 397 to 398 in fc1d423
ignore_thumbnail==False
(i.e. a custom thumbnail was specified), this thumbnail will be used in place of the "BROKEN" stampsphinx-gallery/sphinx_gallery/gen_rst.py
Lines 399 to 400 in fc1d423
In the case that a thumbnail is specified for a failing example and the image does not exist, the default "no_image.png" is used, like it would for a non-failing example.
No changes have been introduced for non-failing examples.
Not sure if this is worthy of a new example, but I included one since it helps here to demonstrate the behaviour. I have no problem if you want this removed before merging.
sphinx-gallery/examples/no_output/plot_raise_thumbnail.py
Lines 1 to 28 in fc1d423
Unit tests
The big thing this is missing is unit tests. I am not super familiar with the inner workings of
sphinx-gallery
so I do not know where to start with this.I noticed in
test_gen_rst.py
the following notes, so it seems like there is not an existing test which I could use as a template for checking the new behaviour (but I may have missed something). Would anyone have some ideas for this?sphinx-gallery/sphinx_gallery/tests/test_gen_rst.py
Lines 1225 to 1227 in fc1d423