Skip to content
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

nbsphinx TocTree broken monkey-patching #11589

Closed
AA-Turner opened this issue Aug 14, 2023 · 4 comments
Closed

nbsphinx TocTree broken monkey-patching #11589

AA-Turner opened this issue Aug 14, 2023 · 4 comments
Milestone

Comments

@AA-Turner
Copy link
Member

AA-Turner commented Aug 14, 2023

OK, I looked into it and it turns out that I was wrong. The document structure is not different, but my monkey-patching is broken.

In spatialaudio/nbsphinx#756 I'm trying to monkey-patch _resolve_toctree instead of TocTree.resolve (depending on Sphinx version), but it still doesn't work.

It seems like setting the property toctree['nbsphinx_gallery'] doesn't work anymore, do you have an idea why this might be the case?

Originally posted by @mgeier in #11565 (comment)

@AA-Turner AA-Turner added this to the 7.2.0 milestone Aug 14, 2023
@AA-Turner
Copy link
Member Author

This is odd, as the toctree.get('nbsphinx_gallery', False) call happens before the call to original_toctree_resolve.

A

@AA-Turner
Copy link
Member Author

AA-Turner commented Aug 14, 2023

import sphinx.environment.adapters.toctree
from sphinx import addnodes

original_toctree_resolve = sphinx.environment.adapters.toctree._resolve_toctree


def patched_toctree_resolve(env, docname, builder, toctree, *args, **kwargs):
    print(toctree.get('nbsphinx_gallery', ...))
    node = original_toctree_resolve(env, docname, builder, toctree, *args, **kwargs)

    return node


def doctree_resolved(app, doctree, fromdocname):
    for node in doctree.findall(addnodes.toctree):
        node['nbsphinx_gallery'] = True


def setup(app):
    app.connect('doctree-resolved', doctree_resolved)
    sphinx.environment.adapters.toctree._resolve_toctree = patched_toctree_resolve
    return {'version': 1, 'parallel_read_safe': True, 'parallel_write_safe': True}

@AA-Turner
Copy link
Member Author

AA-Turner commented Aug 17, 2023

@mgeier it turns out that the problem here is the patching itself--by changing from TocTree.resolve to a module-level function, when using a from ... import ... statement the patching doesn't apply to the copy in that module, and therefore fails. I've changed to look up the function on an imported module, which enables your use case.

See aecebce

A

@mgeier
Copy link
Contributor

mgeier commented Aug 27, 2023

Thanks a lot @AA-Turner for looking into this and for fixing it on the Sphinx side!

I've fixed nbsphinx at spatialaudio/nbsphinx#756, created a new release 0.9.3 and now the thumbnail galleries are displayed correctly again: https://nbsphinx.readthedocs.io/en/0.9.3/gallery/gallery-with-nested-documents.html

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants