code: def main(app):
      static_dir = os.path.join(app.builder.srcdir, "_static")
      target_dir = os.path.join(app.builder.srcdir, "examples")
      source_dir = os.path.abspath(
          os.path.join(app.builder.srcdir, "..", "examples")
      )
      if not os.path.exists(static_dir):
          os.makedirs(static_dir)
  
      if not os.path.exists(target_dir):
          os.makedirs(target_dir)
  
      banner_data = []
  
      toctree = "\n\n" ".. toctree::\n" "   :hidden:\n\n"
      contents = "\n\n"
  
      # Write individual example files
      for filename in glob.glob(os.path.join(source_dir, "*.py")):
          ex = ExampleGenerator(filename, target_dir)
  
          banner_data.append(
              {
                  "title": ex.pagetitle,
                  "url": os.path.join("examples", ex.htmlfilename),
                  "thumb": os.path.join(ex.thumbfilename),
              }
          )
          shutil.copyfile(
              filename, os.path.join(target_dir, ex.pyfilename)
          )
          output = RST_TEMPLATE.format(
              sphinx_tag=ex.sphinxtag,
              docstring=ex.docstring,
              end_line=ex.end_line,
              fname=ex.pyfilename,
              img_html=indent(ex.html, 4),
          )
          with open(os.path.join(target_dir, ex.rstfilename), "w") as f:
              f.write(output)
  
          toctree += ex.toctree_entry()
          contents += ex.contents_entry()
  
      if len(banner_data) < 10:
          banner_data = (4 * banner_data)[:10]
  
      # write index file
      index_file = os.path.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,
              )
          )
  
      # write javascript include for front page
      js_file = os.path.join(static_dir, "banner_data.js")
      with open(js_file, "w") as js:
          js.write(
              BANNER_JS_TEMPLATE.format(
                  banner_data=json.dumps(banner_data)
              )
          )
  
imports: import sys
  import os
  import glob
  import token
  import tokenize
  import shutil
  import json
  import matplotlib
  import mpld3
  from matplotlib import image
  from matplotlib.figure import Figure
label_desc: Use image node_id 3 for calling a global function with example usage: main(app) without return types