code: def config_inited_handler(app, config):
      gallery_dir = join(app.srcdir, config.bokeh_gallery_dir)
      examples_dir = join(app.srcdir, config.bokeh_examples_dir)
  
      gallery_file = f"{gallery_dir}.json"
  
      if not exists(gallery_file) and isfile(gallery_file):
          raise SphinxError(
              f"could not find gallery file {gallery_file!r} for configured gallery dir {gallery_dir!r}"
          )
  
      gallery_file_mtime = getmtime(gallery_file)
  
      ensuredir(gallery_dir)
      ensuredir(examples_dir)
      for subdir in config.bokeh_example_subdirs:
          ensuredir(join(examples_dir, subdir))
  
      # we will remove each file we process from this set and see if anything is
      # left at the end (and remove it in that case)
      extras = set(os.listdir(gallery_dir))
  
      details = get_details(app)
      details_iter = status_iterator(
          details,
          "creating gallery file entries... ",
          "brown",
          len(details),
          app.verbosity,
          stringify_func=lambda x: x["name"] + ".rst",
      )
  
      for detail in details_iter:
          detail_file_path = join(examples_dir, detail["rst_file_path"])
  
          if detail_file_path in extras:
              extras.remove(detail_file_path)
  
          # if the gallery detail file is newer than the gallery file, assume it is up to date
          if (
              exists(detail_file_path)
              and getmtime(detail_file_path) > gallery_file_mtime
          ):
              continue
  
          with open(detail_file_path, "w") as f:
              source_path = abspath(
                  join(app.srcdir, "..", "..", "..", detail["path"])
              )
              f.write(
                  GALLERY_DETAIL.render(
                      filename=detail["name"],
                      source_path=source_path,
                      ref=detail["ref"],
                  )
              )
  
      for extra_file in extras:
          os.remove(join(gallery_dir, extra_file))
  
imports: from __future__ import annotations
  import logging
  import json
  import os
  from os.path import abspath, dirname, exists, getmtime, isdir, isfile, join
  from pathlib import PurePath
  from typing import TypedDict
  from sphinx.errors import SphinxError
  from sphinx.util import ensuredir
  from sphinx.util.display import status_iterator
  from .None import PARALLEL_SAFE
  from .bokeh_directive import BokehDirective
  from .templates import GALLERY_DETAIL, GALLERY_PAGE
  from .util import _REPO_TOP
label_desc: Use image node_id 1 for calling a global function with example usage: config_inited_handler(app, config) without return types