code: def generate_gallery_rst(app):
      """Generate the Main examples gallery reStructuredText
  
      Start the sphinx-gallery configuration and recursively scan the examples
      directories in order to populate the examples gallery
      """
      logger.info("Generating gallery...", color="white")
      gallery_conf = parse_config(app)
  
      clean_gallery_out(app.builder.outdir)
  
      seen_backrefs = set()
  
      computation_times = []
      workdirs = _prepare_sphx_glr_dirs(
          gallery_conf, app.builder.srcdir
      )
  
      for examples_dir, gallery_dir in workdirs:
          examples_dir = os.path.join(app.builder.srcdir, examples_dir)
          gallery_dir = os.path.join(app.builder.srcdir, gallery_dir)
  
          if not os.path.exists(
              os.path.join(examples_dir, "README.txt")
          ):
              raise FileNotFoundError(
                  "Main example directory {0} does not "
                  "have a README.txt file. Please write "
                  "one to introduce your gallery.".format(examples_dir)
              )
  
          # Here we don't use an os.walk, but we recurse only twice: flat is
          # better than nested.
  
          this_fhindex, this_computation_times = generate_dir_rst(
              examples_dir, gallery_dir, gallery_conf, seen_backrefs
          )
  
          computation_times += this_computation_times
  
          # we create an index.rst with all examples
          with codecs.open(
              os.path.join(gallery_dir, "index.rst"),
              "w",
              encoding="utf-8",
          ) as fhindex:
              # :orphan: to suppress "not included in TOCTREE" sphinx warnings
              fhindex.write(":orphan:\n\n" + this_fhindex)
  
              for subsection in get_subsections(
                  app.builder.srcdir,
                  examples_dir,
                  gallery_conf["subsection_order"],
              ):
                  src_dir = os.path.join(examples_dir, subsection)
                  target_dir = os.path.join(gallery_dir, subsection)
                  (
                      this_fhindex,
                      this_computation_times,
                  ) = generate_dir_rst(
                      src_dir, target_dir, gallery_conf, seen_backrefs
                  )
                  fhindex.write(this_fhindex)
                  computation_times += this_computation_times
  
              if gallery_conf["download_all_examples"]:
                  download_fhindex = generate_zipfiles(gallery_dir)
                  fhindex.write(download_fhindex)
  
              fhindex.write(SPHX_GLR_SIG)
  
      if gallery_conf["plot_gallery"]:
          logger.info("Computation time summary:", color="white")
          for time_elapsed, fname in sorted(computation_times)[::-1]:
              if time_elapsed is not None:
                  logger.info("\t- %s : %.2g sec", fname, time_elapsed)
              else:
                  logger.info("\t- %s : not run", fname)
  
file_path: tensorly/doc/sphinx_ext/sphinx_gallery/gen_gallery.py
imports: from __future__ import division, print_function, absolute_import
  import codecs
  import copy
  import re
  import os
  from .None import glr_path_static
  from .None import sphinx_compatibility
  from .gen_rst import generate_dir_rst, SPHX_GLR_SIG
  from .docs_resolv import embed_code_links
  from .downloads import generate_zipfiles
label_desc: Use image node_id 5 for calling a global function with example usage: generate_gallery_rst(app) without return types