Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPython notebook not loading: too much output

I have an iPython notebook file which is not loading, presumably because there is too much output in the file (thousands of lines of results printed, old computer).

I can edit the file with notepad without problems, but copying and then cleaning the code from there cell by cell is very time-consuming.

Is there a way to recover the code differently, or to ask iPython notebook to only load the code and not print all the past outputs when opening the file?

like image 822
Alexis Eggermont Avatar asked Nov 03 '14 03:11

Alexis Eggermont


1 Answers

Here is an output removal script I found on Github. Credits to the author.

      import sys
      import io
      from IPython.nbformat import current


      def remove_outputs(nb):
          for ws in nb.worksheets:
              for cell in ws.cells:
                  if cell.cell_type == 'code':
                      cell.outputs = []`


      if __name__ == '__main__':
          fname = sys.argv[1]
          with io.open(fname, 'r') as f:
              nb = current.read(f, 'json')
              remove_outputs(nb)
              print current.writes(nb, 'json')
like image 152
hui chen Avatar answered Oct 14 '22 03:10

hui chen