Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 22 commits into from
Apr 14, 2016

Conversation

adrn
Copy link
Contributor

@adrn adrn commented Mar 26, 2016

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

@adrn
Copy link
Contributor Author

adrn commented Mar 30, 2016

This is a trial implementation to solve #110

@lesteve
Copy link
Member

lesteve commented Mar 31, 2016

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}
"""

@GaelVaroquaux
Copy link
Contributor

GaelVaroquaux commented Mar 31, 2016 via email

@lesteve
Copy link
Member

lesteve commented Mar 31, 2016

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 print(__doc__) ...

@GaelVaroquaux
Copy link
Contributor

Fair point actually for examples that do print(doc) ...

But also because it's rendered in the html.

@lesteve
Copy link
Member

lesteve commented Mar 31, 2016

But also because it's rendered in the html.

I think the thumbnail: line is stripped off the docstring by this PR so it should not appear in the HTML.

@adrn
Copy link
Contributor Author

adrn commented Mar 31, 2016

Yea, the line is just deleted from of the docstring before displaying with a regular expression. If we change the key name from thumbnail to something like sphinx_gallery_thumbnail_number then I think it'd be sufficiently specific that we can do that. I'm fine with putting it in a comment too though. I'll add some tests and fix the build error. Thanks for the comments!

@lesteve
Copy link
Member

lesteve commented Apr 13, 2016

@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):

# sphinx_gallery_thumbnail_number = 3

@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
Copy link
Member

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.

@adrn
Copy link
Contributor Author

adrn commented Apr 13, 2016

Cool, working on this now. Any thoughts on 0-indexing? e.g., should the default / first plot be sphinx_gallery_thumbnail_number = 0 or sphinx_gallery_thumbnail_number = 1?

Edit: never mind -- it should be the latter because it corresponds to the number in the saved plot file.

@adrn
Copy link
Contributor Author

adrn commented Apr 13, 2016

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)
Copy link
Member

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 :.

Copy link
Contributor Author

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

Copy link
Member

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)
Copy link
Member

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)
Copy link
Member

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.

@adrn
Copy link
Contributor Author

adrn commented Apr 13, 2016

Could you restart the travis build?

@lesteve
Copy link
Member

lesteve commented Apr 13, 2016

Could you restart the travis build?

You can do git commit --amend and then force-push to trigger a new build.

@adrn
Copy link
Contributor Author

adrn commented Apr 13, 2016

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)

@adrn adrn force-pushed the choose-thumbnail branch from c2d9a08 to 84bdea7 Compare April 13, 2016 15:52
@Titan-C
Copy link
Member

Titan-C commented Apr 13, 2016

looks good to me
Could you please write a line in CHANGES.rst, and a documentation section in doc/advanced_configuration.rst narrating how to use this feature and the link to the example using it.


docstring, rest_of_content = get_docstring_and_rest(source_file)
Copy link
Member

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.

@adrn
Copy link
Contributor Author

adrn commented Apr 14, 2016

@Titan-C Added documentation and addressed comments from @lesteve

@@ -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
Copy link
Member

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
Copy link
Member

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.

Copy link
Contributor Author

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.

@lesteve
Copy link
Member

lesteve commented Apr 14, 2016

LGTM, merging, thanks a lot for this, in particular tackling all the minor comments that quickly !

@lesteve lesteve merged commit 3357bca into sphinx-gallery:master Apr 14, 2016
@adrn
Copy link
Contributor Author

adrn commented Apr 14, 2016

👍 thanks for reviewing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants