def clean_cells()

in tools/tensorflow_docs/tools/nbfmt/__main__.py [0:0]


def clean_cells(data: Dict[str, Any], nb_source: str,
                remove_outputs: bool) -> None:
  """Remove empty cells and clean code cells.

  Args:
    data: Object representing a parsed JSON notebook.
    nb_source: JSON string of entire notebook contents.
    remove_outputs: Boolean True to remove code cell outputs, False to keep.
  """
  # Clear leading and trailing newlines.
  for cell in data["cells"]:
    cell_source = cell["source"]
    while cell_source and cell_source[0] == "\n":
      cell_source.pop(0)
    while cell_source and cell_source[-1] == "\n":
      cell_source.pop()
    cell["source"] = cell_source

  # Remove empty cells.
  data["cells"] = [cell for cell in data["cells"] if any(cell["source"])]

  # Clean cell metadata.
  cell_count = 0
  for cell in data["cells"]:
    cell_count += 1
    cell_metadata = cell.get("metadata", {})
    if "id" not in cell_metadata:
      cell_metadata["id"] = notebook_utils.generate_cell_id(
          cell["source"], cell_count)
    notebook_utils.del_entries_except(
        cell_metadata, keep=["id", "cellView", "colab"])
    _clean_metadata_colab(cell_metadata, remove_outputs)

    cell["metadata"] = cell_metadata

  # The presence of this field indicates that ouputs are already saved.
  has_outputs = True if '"output_type"' in nb_source else False

  for cell in data["cells"]:
    if cell["cell_type"] == "code":
      _clean_code_cell(cell, remove_outputs)

  if has_outputs and remove_outputs:
    notebook_utils.warn("Removed the existing output cells.")