kynan / nbstripout

strip output from Jupyter and IPython notebooks
Other
1.19k stars 94 forks source link

Prevent committing notebooks with errors in cell outputs #180

Open janosh opened 1 year ago

janosh commented 1 year ago

Is it possible to detect whether a cell output is an error + stack trace and let users specify one of 3 options ignore, strip or error? The first would be current behavior of doing nothing, second would remove the cell output and third would exit nbstripout non-zero so that using it as a pre-commit hook aborts commits.

My goal is to prevent accidentally releasing notebooks like this:

Screenshot 2022-12-24 at 13 08 29

kynan commented 1 year ago

I would assume errors have some sort of metadata annotation. Do you have an example notebook with an error?

janosh commented 1 year ago

Here's one (zipped since GH doesn't allow uploading .ipynb):

index_error.ipynb.zip

[][0]

Screenshot 2022-12-26 at 07 25 05

janosh commented 1 year ago

This is the noteobook's relevant JSON:

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[0;32mIn [1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m [][\u001b[39m0\u001b[39;49m]\n",
      "\u001b[0;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "[][0]"
   ]
  }
 ]
}
janosh commented 1 year ago

So maybe the trigger could be

any(out["ename"].endswith("Error") for out in outputs)
kynan commented 1 year ago

An output_type of error is an indication of error output.

kynan commented 1 year ago

I won't be able to work on implementing this. Contributions welcome!