pydata-sphinx-theme
pydata-sphinx-theme copied to clipboard
Generate a page's navigation links one time and re-use in the navbar / sidebar
Context
Right now we have have this function to generate either the navbar navigation or the sidebar navigation:
https://github.com/pydata/pydata-sphinx-theme/blob/06a35adef9bd23ccd15ca7f31114472e483ef662/src/pydata_sphinx_theme/init.py#L200-L209
And we then re-use it in a few places to do the actual HTML generation:
- In the navbar: https://github.com/pydata/pydata-sphinx-theme/blob/e1d40097204b3617740c9f86112ab0071654d8d2/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/components/navbar-nav.html (two times, once for navbar, once for sidebar)
- In the sidebar: https://github.com/pydata/pydata-sphinx-theme/blob/f57ca0d33fc1ce3d8547a7ff066c7c3fd79af361/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/layout.html
It seems like this function in particular is time-consuming, and I wonder if this is true regardless of whether it's the sidebar/navbar variety. E.g.:
- https://github.com/pydata/pydata-sphinx-theme/issues/855
Idea
If indeed this function is what is causing all of the slowness, how about instead of calling it multiple times, we generate the full HTML structure of the page's toctree one time, we store it in the page context as a beautifulsoup object, and then we re-use that object in building the actual sidebar / navbar HTML.
I think this might be a way to reduce redundant computation and maybe speed things up, it also feels a bit simpler to me.