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

Skip to content

Add the ability to optionally include images in a gallery that were saved to disk #208

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

Closed
wants to merge 2 commits into from

Conversation

ngoldbaum
Copy link

@ngoldbaum ngoldbaum commented Mar 8, 2017

This resolves #206. I'll add documentation and a test for this feature and the new configuration option if this approach is copacetic.

This change makes it so that we can include images in a gallery for code that does not save image via pyplot or mayavi. It's enabled via a new configuration option find_saved_figures, which defaults to False.

If it's True, we inspect the directory containing the example script (i.e. the directory where the script was being run) for png images. If we find any, we move them to the output directory for the gallery and give them names following the template used for pyplot figures.

I've had to modify the API of the internal save_figures function slightly. As far as I can tell this function only has consumers inside of sphinx-gallery so it should be ok to change the API, but please let me know if that's incorrect.

There are a couple things that could be improved:

  • Search for png images that already exist before running the script so we can find the list of images created just by the script
  • Search for images that aren't in png format or somehow allow users to specify the image format.

@lesteve
Copy link
Member

lesteve commented Mar 8, 2017

I haven't tested this but I think this will be a problem with the notebook-style examples where you have multiple cells and not all figures are saved in the same cell. I believe some figures will be repeated in the HTML.

Also the order of saved figures will not be related with the order of the example execution which may be confusing if you have multiple figures.

Rather than trying to treat similarly saved png and matplotlib figures, maybe we could have an additional "Saved Files" section, which shows saved files by name. This way it is more clear where the image comes from in the rendered HTML. I am thinking aloud here and I don't have a good solution for what you want to do right now.

@lesteve
Copy link
Member

lesteve commented Mar 9, 2017

I haven't tested this but I think this will be a problem with the notebook-style examples where you have multiple cells and not all figures are saved in the same cell. I believe some figures will be repeated in the HTML.

Actually I just quickly tested it and this looks fine, I am guessing because you are moving images out of the way as soon as you discover them.

Also the order of saved figures will not be related with the order of the example execution which may be confusing if you have multiple figures.

I found an example of this in your gallery actually:
http://yt-project.org/docs/dev/cookbook/simple_plots.html#showing-and-hiding-axis-labels-and-colorbars
In the snippet you first have no_axes_sliceplot and then no_axes_no_colorbar_sliceplot but in the images it is the other way around.

I think we should sort the result of glob.glob using os.path.getctime or something similar.

Rather than trying to treat similarly saved png and matplotlib figures, maybe we could have an additional "Saved Files" section, which shows saved files by name. This way it is more clear where the image comes from in the rendered HTML.

I thought a bit more about this and I stick to my proposal. So basically on top of the two things we already have as the output of a cell, i.e. interactive figures and standard output, we would have "saved images". It would be good to display the filename reasonably prominently to make it easier to track which image is which file and understand where it comes from in the example.

By the way this feature could be a way of supporting matplotlib animations #150 by saving to a .gif in the example.

@Titan-C @GaelVaroquaux any opinions?

@GaelVaroquaux
Copy link
Contributor

GaelVaroquaux commented Mar 9, 2017 via email

@lesteve
Copy link
Member

lesteve commented Mar 9, 2017

I am quite uncomfortable with that, because it is moving us further away from the notebook model

Yeah I thought the same too actually. It should be possible to add some kind of formatting in the rendered HTML to make it clear that those are saved image files. Differentiating between interactive figures and saved images is actually the point of adding another section in the output.

@GaelVaroquaux
Copy link
Contributor

GaelVaroquaux commented Mar 9, 2017 via email

@lesteve
Copy link
Member

lesteve commented Mar 9, 2017

I haven't fully understood the compeling reason for saving to disk.

The main point is that it makes sphinx-gallery usable by packages that saves images to disk (as seems to be the case of yt) and do not have figures e.g. matplotlib.pyplot does.

One thing to keep in mind also is that the changes needed to make it work are fairly small. It is not a conclusive argument by itself but still certainly something to take into account.

@GaelVaroquaux
Copy link
Contributor

GaelVaroquaux commented Mar 9, 2017 via email

@ngoldbaum
Copy link
Author

ngoldbaum commented Mar 9, 2017

yt does use matplotlib but not pyplot.

There is no global list of figures created by a yt plotting session (since we're not using pyplot) so the approach used by sphinx-gallery for mayavi and pyplot simply won't work, there's no way to figure after a script has run which figures it produced except by inspecting a folder for images saved to disk. If I were using a plugin here the plugin would just be looking for images saved to disk.

What if I were trying to document a library like Pillow which saves images to disk completely independent of matplotlib and also does not have a global registry of figures? I'd need something exactly like what's in this pull request. This is a sufficiently general desire that I think it would be very helpful to include in the library itself.

@GaelVaroquaux
Copy link
Contributor

Based on @choldgraf 's feedback in #206, I think that we should implement this functionality.

A bit more work is needed on this PR, mainly:

  • The option is the conf.py should not be a True/False, but a regex to decide which files are matched. That way it would be possible to include other files than pngs, but also to be more restrictive than capturing all the files

  • There needs to be narrative documentation added, so that users discover this functionality

  • There should be a bit of formatting / CSS work so that the images appear as different from the ones produced inline. Something like a light title saying "Files produced" and maybe a box with a light border.

@Titan-C
Copy link
Member

Titan-C commented Mar 13, 2017 via email

@alexhuth
Copy link

alexhuth commented Mar 15, 2017

I don't have any comments about implementation, but I just want to chime in and add that this would be a very helpful feature for pycortex. A lot of our plotting is actually done using webGL in a browser, and we really want to make a pretty gallery to show off the pretty outputs. Being able to insert screenshots as thumbnails would be a great help!

@lesteve
Copy link
Member

lesteve commented Mar 16, 2017

Being able to insert screenshots as thumbnails would be a great help!

Are the screenshots generated by the example or are they done manually ?

@alexhuth
Copy link

I'm hoping to do the screenshots manually so they could include the GUI.

Currently it's possible to automatically screenshot just the webGL canvas (with no GUI). One workaround that I hadn't considered before would be to load the automatic screenshot and then display it using matplotlib in order to generate a thumbnail for the gallery. That would be possible, but ugly (and would end up with an example showing the user how to do something stupid that they would never want to do).

@lesteve
Copy link
Member

lesteve commented Mar 16, 2017

I'm hoping to do the screenshots manually so they could include the GUI.

This is what I thought. Your use case is slightly different than the yt case then. One of the point of sphinx-gallery is to make sure that the script and its outputs are in sync by running the example. I think the goal of this PR is to allow capturing saved images generated by the example.

Note that notebook-style cells allow you to write any rst as comments so you can include images if you have a screenshot somewhere. It's just that they will not be used for the thumbnail in the gallery.

@tacaswell
Copy link
Contributor

attn @choldgraf

@choldgraf
Copy link
Contributor

what's the status of this PR? it sounds like there's still debate as to whether to implement this feature in the first place... @ngoldbaum or @lesteve any thoughts on this? I think we should decide and get this PR either merged or closed :-)

@ngoldbaum
Copy link
Author

I don't have any time to spend on this. If it's causing noise to have this open, please close it.

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.

Images saved outside of pyplot are not picked up in the gallery
7 participants