code: def write_notebook_rst(txt, res, fnm, pth):
      
  
      # Extended filename used for output images
      extfnm = fnm + '_files'
      # Directory into which output images are written
      extpth = os.path.join(pth, extfnm)
      # Make output image directory if it doesn't exist
      mkdir(extpth)
      # Iterate over output images in resources dict
      for r in res['outputs'].keys():
          # New name for current output image
          rnew = re.sub('output', fnm, r)
          # Partial path for current output image
          rpth = os.path.join(extfnm, rnew)
          # In RST text, replace old output name with the new one
          txt = re.sub('\.\. image:: ' + r, '.. image:: ' + rpth, txt, re.M)
          # Full path of the current output image
          fullrpth = os.path.join(pth, rpth)
          # Write the current output image to disk
          with open(fullrpth, 'wb') as fo:
              fo.write(res['outputs'][r])
  
      # Remove trailing whitespace in RST text
      txt = re.sub(r'[ \t]+$', '', txt, flags=re.M)
  
      # Write RST text to disk
      with open(os.path.join(pth, fnm + '.rst'), 'wt') as fo:
          fo.write(txt)
docs: Write the converted notebook text `txt` and resources `res` to
      filename `fnm` in directory `pth`.