-
Notifications
You must be signed in to change notification settings - Fork 207
Allow user to choose thumbnail image #114
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
Conversation
This is a trial implementation to solve #110 |
Using the docstring for this seems as good as other ideas that were floated to be honest ! Some tests are failing though, haven't looked into it in more details. We would need some tests for the extract_thumbnail_number function. If you want to cater for the more general case (although this is probably YAGNI unless you have something in mind already), you could use something like: """
docstring
metadata={'thumbail_figure_number': 1}
""" |
I'd rather use a comment, because if we use the docstring, the info will
be printed in the text of the example.
|
Fair point actually for examples that do |
But also because it's rendered in the html. |
I think the |
Yea, the line is just deleted from of the docstring before displaying with a regular expression. If we change the key name from |
@adrn have you managed to investigate the Travis errors? We chatted with @Titan-C and think it's better to put this in a comment that can be anywhere in the file even if that means having to read the file entirely once more. We favour something like (better naming suggestions welcome):
|
@@ -0,0 +1,39 @@ | |||
# -*- coding: utf-8 -*- |
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.
If you could name this file plot_choose_thumbnail.py
with underscores, it would be more consistent.
Cool, working on this now. Any thoughts on 0-indexing? e.g., should the default / first plot be Edit: never mind -- it should be the latter because it corresponds to the number in the saved plot file. |
Fixed the errors and made it search in a comment instead of the docstring as you suggested. Let me know if you think this needs anything else! |
""" Pull out the thumbnail image number specified in the docstring. """ | ||
|
||
# check whether the user has specified a specific thumbnail image | ||
pattr = re.compile("^#\s*sphinx_gallery_thumbnail_number:\s*([0-9]+)\s*$", flags=re.MULTILINE) |
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.
I would use = rather than :.
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.
Really? this looks funny to me:
# sphinx_gallery_thumbnail_number = 2
# Code source: Óscar Nájera
# License: BSD 3 clause
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.
Hmmm then maybe move the sphinx_gallery_thumbnail_number it somewhere else in the file ;-)
docstring, rest_of_content = get_docstring_and_rest(source_file) | ||
rest_of_content, thumbnail_number = extract_thumbnail_number(rest_of_content) |
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.
You don't change rest_of_content
anymore so you only need to return the thumbnail number from extract_thumbnail_number
.
f.write('\n'.join(['"Docstring"', | ||
test_str])) | ||
f.flush() | ||
_,cfg = sg.split_code_and_text_blocks(f.name) |
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.
Another PEP8 with space after the comma here.
Could you restart the travis build? |
You can do |
It's a single button to package maintainers -- in other projects I contribute to, that is the desired way to trigger new builds...but ok, I can force-push if that is what you prefer here (mainly to avoid any accidents with force-push) |
looks good to me |
|
||
docstring, rest_of_content = get_docstring_and_rest(source_file) |
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.
Avoiding unnecessary changes like this one is always appreciated.
@@ -203,6 +203,23 @@ file. You need to add to the configuration dictionary a key called | |||
'default_thumb_file' : 'path/to/thumb/file.png'}} | |||
|
|||
|
|||
choosing the thumbnail image from multiple figures |
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.
Capital needed for the title
|
||
sphinx_gallery_thumbnail_number = 2 | ||
|
||
The default behavior is ``sphinx_gallery_thumbnail_number = 1``. See |
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.
I would remove "The default behavior is sphinx_gallery_thumbnail_number = 1
" since you already mention that the default is to pick the first figure above.
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.
Said in words and said in code. I disagree.
LGTM, merging, thanks a lot for this, in particular tackling all the minor comments that quickly ! |
👍 thanks for reviewing! |
This implements a way for the example-writer to specify which figure is displayed as the thumbnail when an example script generates multiple figures.
This is a bit of a hack and may not make sense to merge in as is, but it's at least something to start discussion. The basic idea with this implementation is that inside the docstring of a given example script, you can add a key:value pair, e.g.,
thumbnail: 3
that specifies which figure in a given example to display as the thumbnail.Like I said: this is a hack. Right now, this uses a regexp to pick out the "thumbnail:" block. A nicer way to move forward might be to think about how you want to represent module-level, arbitrary configuration data or metadata in example files. Then, this thumbnail tag could just fit in that way. Thoughts welcome!
cc @keflavich