code: def generate_file_rst(fname, target_dir, src_dir, gallery_conf):
      """Generate the rst file for a given example.
  
      Returns
      -------
      intro: str
          The introduction of the example
      amount_of_code : int
          character count of the corresponding python script in file
      time_elapsed : float
          seconds required to run the script
      """
  
      src_file = os.path.normpath(os.path.join(src_dir, fname))
      example_file = os.path.join(target_dir, fname)
      shutil.copyfile(src_file, example_file)
      file_conf, script_blocks = split_code_and_text_blocks(src_file)
      amount_of_code = sum(
          [
              len(bcontent)
              for blabel, bcontent, lineno in script_blocks
              if blabel == "code"
          ]
      )
      intro = extract_intro(fname, script_blocks[0][1])
  
      if md5sum_is_current(example_file):
          return intro, amount_of_code, 0
  
      image_dir = os.path.join(target_dir, "images")
      if not os.path.exists(image_dir):
          os.makedirs(image_dir)
  
      base_image_name = os.path.splitext(fname)[0]
      image_fname = "sphx_glr_" + base_image_name + "_{0:03}.png"
      build_image_dir = os.path.relpath(
          image_dir, gallery_conf["src_dir"]
      )
      image_path_template = os.path.join(image_dir, image_fname)
  
      ref_fname = os.path.relpath(example_file, gallery_conf["src_dir"])
      ref_fname = ref_fname.replace(os.path.sep, "_")
      example_rst = """\n\n.. _sphx_glr_{0}:\n\n""".format(ref_fname)
  
      filename_pattern = gallery_conf.get("filename_pattern")
      execute_script = (
          re.search(filename_pattern, src_file)
          and gallery_conf["plot_gallery"]
      )
      example_globals = {
          # A lot of examples contains 'print(__doc__)' for example in
          # scikit-learn so that running the example prints some useful
          # information. Because the docstring has been separated from
          # the code blocks in sphinx-gallery, __doc__ is actually
          # __builtin__.__doc__ in the execution context and we do not
          # want to print it
          "__doc__": "",
          # Examples may contain if __name__ == '__main__' guards
          # for in example scikit-learn if the example uses multiprocessing
          "__name__": "__main__",
          # Don't ever support __file__: Issues #166 #212
      }
  
      # A simple example has two blocks: one for the
      # example introduction/explanation and one for the code
      is_example_notebook_like = len(script_blocks) > 2
      time_elapsed = 0
      block_vars = {
          "execute_script": execute_script,
          "fig_count": 0,
          "image_path": image_path_template,
          "src_file": src_file,
      }
  
      argv_orig = sys.argv[:]
      if block_vars["execute_script"]:
          # We want to run the example without arguments. See
          # https://github.com/sphinx-gallery/sphinx-gallery/pull/252
          # for more details.
          sys.argv[0] = src_file
          sys.argv[1:] = []
  
      for blabel, bcontent, lineno in script_blocks:
          if blabel == "code":
              code_output, rtime = execute_code_block(
                  src_file,
                  bcontent,
                  lineno,
                  example_globals,
                  block_vars,
                  gallery_conf,
              )
  
              time_elapsed += rtime
  
              if not file_conf.get(
                  "line_numbers",
                  gallery_conf.get("line_numbers", False),
              ):
                  lineno = None
  
              if is_example_notebook_like:
                  example_rst += (
                      codestr2rst(bcontent, lineno=lineno) + "\n"
                  )
                  example_rst += code_output
              else:
                  example_rst += code_output
                  if "sphx-glr-script-out" in code_output:
                      # Add some vertical space after output
                      example_rst += "\n\n|\n\n"
                  example_rst += (
                      codestr2rst(bcontent, lineno=lineno) + "\n"
                  )
  
          else:
              example_rst += bcontent + "\n\n"
  
      sys.argv = argv_orig
      clean_modules()
  
      # Writes md5 checksum if example has build correctly
      # not failed and was initially meant to run(no-plot shall not cache md5sum)
      if block_vars["execute_script"]:
          with open(example_file + ".md5", "w") as file_checksum:
              file_checksum.write(get_md5sum(example_file))
  
      save_thumbnail(
          image_path_template, src_file, file_conf, gallery_conf
      )
  
      time_m, time_s = divmod(time_elapsed, 60)
      example_nb = jupyter_notebook(script_blocks)
      save_notebook(example_nb, example_file.replace(".py", ".ipynb"))
      with codecs.open(
          os.path.join(target_dir, base_image_name + ".rst"),
          mode="w",
          encoding="utf-8",
      ) as f:
          example_rst += (
              "**Total running time of the script:**"
              " ({0: .0f} minutes {1: .3f} seconds)\n\n".format(
                  time_m, time_s
              )
          )
          example_rst += CODE_DOWNLOAD.format(
              fname, fname.replace(".py", ".ipynb")
          )
          example_rst += SPHX_GLR_SIG
          f.write(example_rst)
  
      if block_vars["execute_script"]:
          logger.debug(
              "%s ran in : %.2g seconds", src_file, time_elapsed
          )
  
      return intro, amount_of_code, time_elapsed
  
imports: from __future__ import division, print_function, absolute_import
  from time import time
  import ast
  import codecs
  import hashlib
  import os
  import re
  import shutil
  import subprocess
  import sys
  import traceback
  from distutils.version import LooseVersion
  from io import StringIO
  import matplotlib
  import matplotlib.pyplot
  import sphinx
  from .None import glr_path_static
  from .None import sphinx_compatibility
  from .backreferences import write_backreferences, _thumbnail_div
  from .downloads import CODE_DOWNLOAD
  from .py_source_parser import split_code_and_text_blocks
  from .notebook import jupyter_notebook, save_notebook
label_desc: Use image node_id 13 for calling a global function with example usage: generate_file_rst(fname, target_dir, src_dir, gallery_conf) and returns: intro,  amount_of_code,  time_elapsed, intro,  amount_of_code,  int