Skip to content

ENH: allow rst files to pass through #1071

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 3 commits into from
Feb 14, 2023

Conversation

jklymak
Copy link
Contributor

@jklymak jklymak commented Jan 23, 2023

Closes #1067.

Ready for review 27 Jan...

This allows *.rst files in galleries to pass through as rst. So in /examples_with_rst/ rst_example1.rst is passed though to the build directory. Note that the toctree does not get an entry for this, so the user will need to add a toctree to their README.txt

Secondly, it allows index.rst to be in a gallery, and then generating the index.rst is not done. Obviously the user is responsible for creating the toctree by hand for this.

Again, the motivation for this is from Matplotlib, where we really like to write long tutorials with lots of plots as python files, but other narrative documentation pages are more natural as rst. Similarly, we are going to want manual control of our table of contents, so we would like to be able to write our own index.rst.

BTW, I tested that this works well with nested_sections=True as well (which is responsible for some of the code complexity)

Open to suggestions as to how to better structure this. I think I've put things at the natural place to inject the passthroughs, but it does lead to a couple large if/else blocks that could probably be optimized.

@jklymak
Copy link
Contributor Author

jklymak commented Jan 23, 2023

Hmm, this is close. It works fine when invoked from make html in doc/ but not when run as python setup.py build_sphinx -nW --keep-going from the project root. I must have something broken in specifying the root.

Should be fixed now...

@jklymak
Copy link
Contributor Author

jklymak commented Jan 23, 2023

OK, I think the CI failures are expected.

Before I do a bunch of code cleanup, and write more docs (other than the self-documenting of the examples):

  1. Does the user API/behaviour for this make sense? In particular: If the user adds an rst file it is put it somewhere in the TOC, versus the gallery adding it to the hidden TOC and perhaps using a no-image thumbnail. The implemented behaviour is my preference, but I don't think it's too difficult to add a thumbnail.
  2. Does the general code flow make sense? I found the generation of the gallery, thumbnails, and index pages pretty intertwined with each other, so I may not be doing the alternate routes as cleanly as possible.

Thanks!

@larsoner
Copy link
Contributor

So to me this PR is actually two separable things:

  1. Allow index.rst as a pass-through/replacement of README.txt
  2. Allow *.rst to be copied over

For (1), I like the current design -- it seems simple enough and I don't think it will break things for people. At least I don't think we allow the source and generated dirs to be the same -- if we did, that's a problem for README.txt->index.rst generation, as running make html once will create index.rst in the source dir in that case, thereby making it no longer be regenerated, which is bad.

For (2), I think we might want to think about a proper API. For example, people have asked about being able to copy other stuff (e.g., images) as well (e.g., #1063 (comment)) but my thought so far was "just do this yourself if you want it". But now that it's been brought up at least a couple of times, something like 'copyfile_regex: '' where you can give a non-empty regex of files to copy seems like it would solve both your use case and more general ones (e.g., images within the examples dir) as you could do 'copyfile_regex': r'.*\.rst'.

@jklymak
Copy link
Contributor Author

jklymak commented Jan 23, 2023

For (2), I think we might want to think about a proper API. For example, people have asked about being able to copy other stuff (e.g., images) as well (e.g., #1063 (comment)) but my thought so far was "just do this yourself if you want it". But now that it's been brought up at least a couple of times, something like 'copyfile_regex: '' where you can give a non-empty regex of files to copy seems like it would solve both your use case and more general ones (e.g., images within the examples dir) as you could do 'copyfile_regex': r'.*\.rst'.

Hmm, that is probably easy, and may actually simplify my exploratory code above now that I think of it that way. But even simpler, is there any reason to not copy everything in the gallery source directory over to the target directory? Maybe excluding README.*, particularly README.rst?

@larsoner
Copy link
Contributor

Yes, because examples can produce/write junk files, people can have crufty .orig files on their systems, etc. What you're talking about would be the equivalent of something like '.*' and I don't think it's a good/safe/backward compatible default

@jklymak
Copy link
Contributor Author

jklymak commented Jan 23, 2023

OK, fair. Adding a copyfile_regexp isn't that hard.

Edit: think I did that right, and the code is somewhat simpler as well!

@jklymak jklymak force-pushed the enh-pass-rst branch 3 times, most recently from 3871b5c to c7a7d57 Compare January 23, 2023 20:55
@jklymak
Copy link
Contributor Author

jklymak commented Jan 24, 2023

Assuming this is working the way folks are happy with, what is the advice on how to document/test.

Currently I've just added two new galleries: /examples_with_rst and /examples_rst_index. I didn't roll either of these into the main examples because you build with nested_sections = False, so I'd have to put the :toctree for the rst file at the top level. And of course it would not test /examples_rst_index at all, since there is no subsections by index levels if nested_sections=False.

I think there are two options - the first is keep nested_sections=False and add the two new galleries (probably could get away with one, but it's a little clearer as two). The other option is to actually change nested_sections=True which I believe means a little rewriting of the main example Readme, but nothing too major.

Finally, I've not dug into your testing yet, but I assume I can figure it out.

@timhoffm
Copy link
Contributor

API suggestion:

  1. Use wildcard expressions fnmatch instead of regular expressions. These are easier to read and write and very common for files.
  2. Use a list instead of a single item.

This reads for example

copy_files = ['document.rst', '*.png']

instead of

copyfile_regex = r'(document\.rst|.*\.png)'
  1. (optionally) If you really think regex capability is needed you can support passing a compiled regex: copy_files = ['simple.rst', 're.compile(r'\d{2}\.png')]. But that could always be added later if there is user request.

@jklymak
Copy link
Contributor Author

jklymak commented Jan 24, 2023

I'm fine with a list of fnmatch patterns as well if thats what the s-g maintainers prefer.

@larsoner
Copy link
Contributor

Looking through our existing config values there are several that use regex and none that use glob/fnmatch, so we should probably stick with regex for within-package consistency

@jklymak jklymak force-pushed the enh-pass-rst branch 6 times, most recently from c415924 to 0658cf6 Compare January 28, 2023 00:10
Comment on lines 30 to 46
N_TOT = 18 # examples (plot_*.py in examples/**)

N_FAILING = 2
N_GOOD = N_TOT - N_FAILING
N_RST = 17 + N_TOT + 1 # includes module API pages, etc.
N_RST = 17 + N_TOT + 1 + 12 # includes module API pages, etc.
N_RST = '(%s|%s)' % (N_RST, N_RST - 1) # AppVeyor weirdness
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll admit these numbers are completely empirical to me. I don't understand what is included here and what should not be. I added files to tinybuild and obviously they changed the totals, but I'm not clear where +12 comes from exactly, or how it relates to +5 in N_TOT. Perhaps whoever architected these tests can tell me how to make this deterministic, and I can try. (Note I also got screwed up for quite a while as I think you cannot run these tests standalone - you need to run the whole file?)

Copy link
Contributor

Choose a reason for hiding this comment

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

It's likely not up to date anymore, and I agree it's not clear (even though I think I came up with it!). I think we probably want something like

N_EXAMPLES = ...  # examples/*.py files that get turned to RST
N_FAILING = 2
N_GOOD = N_EXAMPLES - N_FAILING
N_INDEX = ...  # index.rst files that SG generates
N_OTHER = ...  # other pages (API/modules, etc.)
N_RST = N_EXAMPLES + N_INDEX + N_OTHER
N_RST  = '(%s|%s)' % (N_RST, N_RST - 1)  # AppVeyor weirdness

or so, would that make sense to you? It might be a good idea to do this in master first to make sure it's working properly, though... I can do this sometime this week or next unless you want to try @jklymak

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure let me try as I looked at it all recently. I tried doing what you have here but failed, but starting from master makes sense and then after that I can add one file at a time and get it to make sense.

@jklymak jklymak marked this pull request as ready for review January 28, 2023 00:13
@jklymak jklymak force-pushed the enh-pass-rst branch 2 times, most recently from a30af5b to 95c45de Compare February 1, 2023 03:55
@jklymak
Copy link
Contributor Author

jklymak commented Feb 1, 2023

.. this is rebased on #1072

@jklymak jklymak force-pushed the enh-pass-rst branch 2 times, most recently from 74f6072 to 4fa8f41 Compare February 2, 2023 18:45
# Write toctree with gallery items from gallery root folder
if len(this_toctree_items) > 0:
this_toctree = """
if this_content:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Everything from here to the else is just an indentation change of the original code...

Copy link
Contributor

Choose a reason for hiding this comment

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

Github "Hide whitespace " viewing mode agrees :)

@jklymak jklymak force-pushed the enh-pass-rst branch 4 times, most recently from ab21196 to a81b091 Compare February 9, 2023 16:48
@@ -447,6 +457,24 @@ def _prepare_sphx_glr_dirs(gallery_conf, srcdir):
return list(zip(examples_dirs, gallery_dirs))


def _format_toctree(items, includehidden=False):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I refactored this out because the lack of indentation was making it hard to follow.

@jklymak
Copy link
Contributor Author

jklymak commented Feb 9, 2023

Commit 2 (and 3) are a refactor of how I ignore the README.txt logic if the index.rst exists. I think I prefer the new way, but it's a bit more intertwined with the README.txt logic. The advantage is less duplication. Disadvantage is that the index.rst.new gets made even though it will never get used, which is a bit confusing.

@jklymak jklymak marked this pull request as ready for review February 9, 2023 17:30
@larsoner
Copy link
Contributor

larsoner commented Feb 9, 2023

Disadvantage is that the index.rst.new gets made even though it will never get used, which is a bit confusing.

Sounds worth it given the advantages. And it should be plenty fast anyway!

Copy link
Contributor

@larsoner larsoner left a comment

Choose a reason for hiding this comment

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

Just one minor adjustment to the docs (assuming my understanding is correct), otherwise LGTM!

@timhoffm can you see if you're happy with the API as well?

https://output.circle-artifacts.com/output/job/61f7264b-dd38-4f20-b705-51deb7651ad3/artifacts/0/rtd_html/configuration.html#manual-passthrough

@jklymak
Copy link
Contributor Author

jklymak commented Feb 9, 2023

Sounds worth it given the advantages. And it should be plenty fast anyway!

I think so, as the loop I was bailing out of also did a number of other things, like sg_execution_times, and zipped files.

Copy link
Contributor

@larsoner larsoner left a comment

Choose a reason for hiding this comment

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

Let's see if @timhoffm wants to look or comment, but otherwise let's merge!

@jklymak
Copy link
Contributor Author

jklymak commented Feb 13, 2023

@timhoffm has limited time currently due to family obligations. But happy to wait for him or other folks. I think the API here is relatively opt-in, so it should not affect anyone who doesn't want this pass-through to happen.

@larsoner larsoner merged commit 9090f5f into sphinx-gallery:master Feb 14, 2023
@larsoner
Copy link
Contributor

Let's try it -- thanks @jklymak !

@jklymak
Copy link
Contributor Author

jklymak commented Feb 14, 2023

Thanks for your help and work maintaining sphinx-gallery!

@GaelVaroquaux
Copy link
Contributor

GaelVaroquaux commented Feb 14, 2023 via email

@jklymak jklymak mentioned this pull request Feb 14, 2023
clrpackages referenced this pull request in clearlinux-pkgs/pypi-sphinx_gallery Mar 11, 2023
… to version 0.12.1

v0.12.1
-------

**Fixed bugs:**

-  BUG: Fix bug with show_api_usage `#1095 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1095>`__ (`larsoner <https://github.com/larsoner>`__)
-  FIX: Add blank line at end of table of contents block `#1094 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1094>`__ (`sdhiscocks <https://github.com/sdhiscocks>`__)

v0.12.0
-------
Support for Sphinx < 4 dropped in this release. Requirement is Sphinx >= 4.

**Implemented enhancements:**

-  ENH: allow rst files to pass through `#1071 <https://github.com/sphinx-gallery/sphinx-gallery/pull/1071>`__ (`jklymak <https://github.com/jklymak>`__)

(NEWS truncated at 15 lines)
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.

Passthrough bare rst, and possibly index.rst
4 participants