docs: def extract_workflow(notebook):
      '''Extract workflow from a notebook file or notebook JSON instance'''
      if isinstance(notebook, str):
          nb = nbformat.read(notebook, nbformat.NO_CONVERT)
      else:
          nb = notebook
      cells = nb.cells
      content = '#!/usr/bin/env sos-runner\n#fileformat=SOS1.0\n\n'
      for cell in cells:
          if cell.cell_type != "code":
              continue
          # Non-sos code cells are also ignored
          if 'kernel' in cell.metadata and cell.metadata['kernel'] not in ('sos',
                                                                           'SoS',
                                                                           None):
              continue
          lines = cell.source.split('\n')
          valid_cell = False
          for idx, line in enumerate(lines):
              if valid_cell or (line.startswith('%include') or
                                line.startswith('%from')):
                  content += line + '\n'
              elif SOS_SECTION_HEADER.match(line):
                  valid_cell = True
                  # look retrospectively for comments
                  c = idx - 1
                  comment = ''
                  while c >= 0 and lines[c].startswith('#'):
                      comment = lines[c] + '\n' + comment
                      c -= 1
                  content += comment + line + '\n'
          if valid_cell:
              content += '\n'
      return content