code: def main(app):
      # Get paths for files
      working_dir = os.getcwd()
      os.chdir(app.builder.srcdir)
      static_dir = op.join(app.builder.srcdir, "..", "build", "_static")
      target_dir_orig = op.join(app.builder.srcdir, "examples")
  
      if not op.exists(static_dir):
          os.makedirs(static_dir)
  
      # Map each backend to their respective paths
      path_dict = {}
      backends = ("matplotlib", "bokeh")
      backend_prefixes = ("mpl", "bokeh")
      for backend in backends:
          target_dir = op.join(target_dir_orig, backend)
          image_dir = op.join(target_dir, "_images")
          source_dir = op.abspath(
              op.join(
                  app.builder.srcdir, "..", "..", "examples", backend
              )
          )
  
          if not op.exists(source_dir):
              os.makedirs(source_dir)
  
          if not op.exists(target_dir):
              os.makedirs(target_dir)
  
          if not op.exists(image_dir):
              os.makedirs(image_dir)
  
          path_dict[backend] = {
              "source_dir": source_dir,
              "target_dir": target_dir,
              "image_dir": image_dir,
          }
  
      # Write individual example files
      files = sorted(
          glob.glob(
              op.join(path_dict["matplotlib"]["source_dir"], "*.py")
          )
      )
      for filename in files:
          base_filename = op.split(filename)[1].split("_", 1)[1]
          example_contents = ""
          for backend, prefix in zip(backends, backend_prefixes):
              source_dir = path_dict[backend]["source_dir"]
              target_dir = path_dict[backend]["target_dir"]
              expected_filename = op.join(
                  source_dir, f"{prefix}_{base_filename}"
              )
  
              if not op.exists(expected_filename):
                  if backend == "matplotlib":
                      raise ValueError(
                          "All examples must have a matplotlib counterpart."
                      )
                  continue
  
              ex = ExampleGenerator(
                  expected_filename,
                  target_dir,
                  backend,
                  target_dir_orig,
              )
  
              shutil.copyfile(
                  expected_filename, op.join(target_dir, ex.pyfilename)
              )
              output = RST_TEMPLATES[backend].format(
                  sphinx_tag=ex.sphinxtag,
                  docstring=ex.docstring,
                  end_line=ex.end_line,
                  fname=op.join(backend, ex.pyfilename),
                  absfname=op.join(target_dir, ex.pyfilename),
                  img_file=op.join(backend, ex.pngfilename),
                  api_name=ex.apiname,
              )
              example_contents += output
  
              # Add plot to table of contents and content card if matplotlib
              if backend == "matplotlib":
                  categorized_contents.get("toctree").get(
                      ex.gallery_category
                  ).append(ex.toctree_entry())
                  categorized_contents.get("contents").get(
                      ex.gallery_category
                  ).append(ex.contents_entry())
  
          with open(op.join(target_dir_orig, ex.rstfilename), "w") as f:
              f.write(example_contents)
  
      # Begin templates for table of contents and content cards
      toctree = ""
      contents = ""
  
      # Sort and write toctree
      for category, entries in categorized_contents.get(
          "toctree"
      ).items():
          if len(entries) > 0:
              toctree += TOCTREE_START.format(category=category)
              entries.sort()
              for entry in entries:
                  toctree += entry
      # Sort and write contents (cards in example gallery)
      for category, entries in categorized_contents.get(
          "contents"
      ).items():
          if len(entries) > 0:
              contents += "\n{category}\n{underline}\n{start}\n".format(
                  category=category,
                  underline="-" * len(category),
                  start=CONTENTS_START,
              )
              entries.sort()
              for entry in entries:
                  contents += entry
  
      # Write index file
      index_file = op.join(target_dir, "..", "index.rst")
      with open(index_file, "w") as index:
          index.write(
              INDEX_TEMPLATE.format(
                  sphinx_tag="example_gallery",
                  toctree=toctree,
                  contents=contents,
                  examples_source=source_dir,
              )
          )
  
      os.chdir(working_dir)
  
imports: import glob
  import os
  import os.path
  import re
  import shutil
  import token
  import tokenize
  from typing import Optional
  import matplotlib
  import matplotlib.pyplot
  from arviz.rcparams import rc_context
  from arviz import rcParams
label_desc: Use image node_id 1 for calling a global function with example usage: main(app) without return types