code: def generate_example_rst(app):
      """Generate the list of examples, as well as the contents of
      examples.
      """
      root_dir = os.path.join(app.builder.srcdir, "auto_examples")
      example_dir = os.path.abspath(
          os.path.join(app.builder.srcdir, "..", "examples")
      )
      generated_dir = os.path.abspath(
          os.path.join(app.builder.srcdir, "modules", "generated")
      )
  
      code_samples_dir = os.path.abspath(
          os.path.join(app.builder.srcdir, "modules", "code_samples")
      )
  
      try:
          plot_gallery = eval(app.builder.config.plot_gallery)
      except TypeError:
          plot_gallery = bool(app.builder.config.plot_gallery)
      if not os.path.exists(example_dir):
          os.makedirs(example_dir)
      if not os.path.exists(root_dir):
          os.makedirs(root_dir)
      if not os.path.exists(generated_dir):
          os.makedirs(generated_dir)
  
      # we create an index.rst with all examples
      fhindex = open(os.path.join(root_dir, "index.rst"), "w")
      # Note: The sidebar button has been removed from the examples page for now
      #      due to how it messes up the layout. Will be fixed at a later point
      fhindex.write(
          """\
  :orphan:
  
  
  .. raw:: html
  
  
      <style type="text/css">
      div#sidebarbutton {
          /* hide the sidebar collapser, while ensuring vertical arrangement */
          display: none;
      }
      </style>
  
  .. _examples-index:
  
  Examples
  ========
  
  """
      )
      # Here we don't use an os.walk, but we recurse only twice: flat is
      # better than nested.
      seen_backrefs = set()
      generate_dir_rst(
          ".",
          fhindex,
          example_dir,
          root_dir,
          plot_gallery,
          seen_backrefs,
      )
      for directory in sorted(os.listdir(example_dir)):
          if os.path.isdir(os.path.join(example_dir, directory)):
              generate_dir_rst(
                  directory,
                  fhindex,
                  example_dir,
                  root_dir,
                  plot_gallery,
                  seen_backrefs,
              )
  
      generate_dir_rst(
          ".",
          fhindex,
          code_samples_dir,
          root_dir,
          plot_gallery,
          seen_backrefs,
          display=False,
      )
      for dirpath, dirnames, _ in os.walk(code_samples_dir):
          for dirname in dirnames:
              generate_dir_rst(
                  dirname,
                  fhindex,
                  code_samples_dir,
                  root_dir,
                  plot_gallery,
                  seen_backrefs,
                  display=False,
              )
      fhindex.flush()
  
imports: from __future__ import division, print_function
  from time import time
  import ast
  import os
  import re
  import shutil
  import traceback
  import glob
  import sys
  import gzip
  import posixpath
  import subprocess
  import warnings
  from sklearn.externals import six
  from docutils.core import publish_string
  import token
  import tokenize
  import numpy
  import joblib
label_desc: Use image node_id 4 for calling a global function with example usage: generate_example_rst(app) without return types