code: def main(app):
      gallery_dir = app.builder.config.altair_gallery_dir
      target_dir = os.path.join(app.builder.srcdir, gallery_dir)
      image_dir = os.path.join(app.builder.srcdir, "_images")
  
      gallery_ref = app.builder.config.altair_gallery_ref
      gallery_title = app.builder.config.altair_gallery_title
      examples = populate_examples(
          gallery_ref=gallery_ref, code_below=True, strict=False
      )
  
      if not os.path.exists(target_dir):
          os.makedirs(target_dir)
  
      examples = sorted(examples, key=lambda x: x["title"])
      examples_toc = collections.OrderedDict(
          {
              "Simple Charts": [],
              "Bar Charts": [],
              "Line Charts": [],
              "Area Charts": [],
              "Circular Plots": [],
              "Scatter Plots": [],
              "Uncertainties And Trends": [],
              "Distributions": [],
              "Tables": [],
              "Maps": [],
              "Interactive Charts": [],
              "Advanced Calculations": [],
              "Case Studies": [],
          }
      )
      for d in examples:
          examples_toc[d["category"]].append(d)
  
      # Write the gallery index file
      with open(os.path.join(target_dir, "index.rst"), "w") as f:
          f.write(
              GALLERY_TEMPLATE.render(
                  title=gallery_title,
                  examples=examples_toc.items(),
                  image_dir="/_static",
                  gallery_ref=gallery_ref,
              )
          )
  
      # save the images to file
      save_example_pngs(examples, image_dir)
  
      # Write the individual example files
      for prev_ex, example, next_ex in prev_this_next(examples):
          if prev_ex:
              example["prev_ref"] = "gallery_{name}".format(**prev_ex)
          if next_ex:
              example["next_ref"] = "gallery_{name}".format(**next_ex)
          target_filename = os.path.join(
              target_dir, example["name"] + ".rst"
          )
          with open(
              os.path.join(target_filename), "w", encoding="utf-8"
          ) as f:
              f.write(EXAMPLE_TEMPLATE.render(example))
  
imports: import hashlib
  import os
  import json
  import random
  import collections
  from operator import itemgetter
  import warnings
  import shutil
  import jinja2
  from docutils import nodes
  from docutils.statemachine import ViewList
  from docutils.parsers.rst import Directive
  from docutils.parsers.rst.directives import flag
  from sphinx.util.nodes import nested_parse_with_titles
  from .utils import get_docstring_and_rest, prev_this_next, create_thumbnail, create_generic_image
  from altair.utils.execeval import eval_block
  from tests.examples_arguments_syntax import iter_examples_arguments_syntax
  from tests.examples_methods_syntax import iter_examples_methods_syntax
label_desc: Use image node_id 3 for calling a global function with example usage: main(app) without return types