code: def preload_sidebar_nav(
      get_toctree: Callable[[Any], str],
      pathto: Callable[[str], str],
      root_doc: str,
      pagename: str,
  ) -> bs4.BeautifulSoup:
      """Return the navigation link structure in HTML.
  
      This function is modified from the pydata_sphinx_theme function
      `generate_toctree_html`. However, for ray we only produce one
      sidebar for all pages. We therefore can call this function just once (per worker),
      cache the result, and reuse it.
  
      The result of this function is equivalent to calling
  
          pydata_sphinx_theme.toctree.generate_toctree_html(
              "sidebar",
              startdepth=0,
              show_nav_level=0,
              collapse=False,
              includehidden=True,
              maxdepth=4,
              titles_only=True
          )
  
      Here we cache the result on this function itself because the `get_toctree`
      function is not the same instance for each `html-page-context` event, even
      if the toctree content is identical.
  
      Parameters
      ----------
      get_toctree : Callable[[Any], str]
          The function defined in context["toctree"] when html-page-context is triggered
  
      Returns
      -------
      bs4.BeautifulSoup
          Soup to display in the side navbar
      """
      if hasattr(preload_sidebar_nav, "cached_toctree"):
          # Need to retrieve a copy of the cached toctree HTML so as not to modify
          # the cached version when we set the "checked" state of the inputs
          soup = copy.copy(preload_sidebar_nav.cached_toctree)
      else:
          toctree = get_toctree(
              collapse=False,
              includehidden=True,
              maxdepth=4,
              titles_only=True,
          )
  
          soup = bs4.BeautifulSoup(toctree, "html.parser")
  
          # Remove "current" class since this toctree HTML is being reused
          # from some other page
          for li in soup("li", {"class": "current"}):
              li["class"].remove("current")
  
          # Remove sidebar links to sub-headers on the page
          for li in soup.select("li"):
              # Remove
              if li.find("a"):
                  href = li.find("a")["href"]
                  if "#" in href and href != "#":
                      li.decompose()
  
          # Add bootstrap classes for first `ul` items
          for ul in soup("ul", recursive=False):
              ul.attrs["class"] = ul.attrs.get("class", []) + [
                  "nav",
                  "bd-sidenav",
              ]
  
          # Add collapse boxes for parts/captions.
          # Wraps the TOC part in an extra <ul> to behave like chapters with toggles
          # show_nav_level: 0 means make parts collapsible.
          partcaptions = soup.find_all("p", attrs={"class": "caption"})
          if len(partcaptions):
              new_soup = bs4.BeautifulSoup(
                  "<ul class='list-caption'></ul>", "html.parser"
              )
              for caption in partcaptions:
                  # Assume that the next <ul> element is the TOC list
                  # for this part
                  for sibling in caption.next_siblings:
                      if sibling.name == "ul":
                          toclist = sibling
                          break
                  li = soup.new_tag("li", attrs={"class": "toctree-l0"})
                  li.extend([caption, toclist])
                  new_soup.ul.append(li)
              soup = new_soup
  
          # Add icons and labels for collapsible nested sections
          add_collapse_checkboxes(soup)
  
          # Cache a fresh copy of the toctree HTML
          preload_sidebar_nav.cached_toctree = copy.copy(soup)
  
      # All the URIs are relative to the current document location, e.g.
      # "../../<whatever.html>". There's no need for this, and it messes up the build if
      # we reuse the same sidebar (with different relative URIs for various pages) on
      # different pages. Replace the leading "../" sequences in the hrefs with the correct
      # number of "../" for the given rootdoc
      if pagename == root_doc:
          to_root_prefix = "./"
      else:
          to_root_prefix = re.sub(
              f"{root_doc}.html", "", pathto(root_doc)
          )
  
      for a in soup.select("a"):
          absolute_href = re.sub(r"^(\.\.\/)*", "", a["href"])
          a["href"] = to_root_prefix + absolute_href
  
          if absolute_href == f"{pagename}.html":
              # Add a current-page class to the parent li element for styling
              parent_li = a.find_parent("li")
              parent_li["class"] = parent_li.get("class", []) + [
                  "current-page"
              ]
  
              # Open the dropdowns of every parent li for the active page
              for parent_li in a.find_parents(
                  "li", attrs={"class": "has-children"}
              ):
                  el = parent_li.find("input")
                  if el:
                      el.attrs["checked"] = True
  
      return soup
  
imports: import re
  import sphinx
  from typing import List, Dict, Union, Callable, Any
  import copy
  import yaml
  import bs4
  import logging
  import logging.handlers
  import pathlib
  import urllib
  import urllib.request
  from queue import Queue
  import requests
  from preprocess_github_markdown import preprocess_github_markdown_file
  from sphinx.util import logging
  from sphinx.util.console import red
  from sphinx.util.nodes import make_refnode
  from docutils import nodes
  from functools import lru_cache
  from pydata_sphinx_theme.toctree import add_collapse_checkboxes
  from pygments import highlight
  from pygments.lexers import PythonLexer
  from pygments.formatters import HtmlFormatter
label_desc: Use image node_id 4 for calling a global function with example usage: preload_sidebar_nav(get_toctree, pathto, root_doc, pagename) and returns: soup