How to clear notebook outputs without editing them

We generally recommend saving notebooks without large amounts of data in outputs. In addition to preventing users from opening notebooks in preview mode, large notebooks can infrequently trigger egress alerts that require follow-up from our security team. Users may rarely encounter a situation where even opening a notebook for editing triggers an egress alert due to the size of outputs, making it difficult to easily remove these outputs from them. 

 

In case this happens with one of your notebooks, the following protocol and code can be used to create a duplicate notebook in the workspace without outputs.

Guide

1. Start by making a new notebook in the workspace with the large notebook. It can be named anything.

 

2. Copy and paste the following code in the notebook. 

 

import sys

import io

import os

import argparse

import nbformat

 

def remove_outputs(nb):

    for cell in nb.cells:

        if cell.cell_type == 'code':

            cell.outputs = []

 

def clear_notebook(old_ipynb, new_ipynb):

    with io.open(old_ipynb, 'r') as f:

        nb = nbformat.read(f, nbformat.NO_CONVERT)

 

    remove_outputs(nb)

 

    with io.open(new_ipynb, 'w', encoding='utf8') as f: 

        nbformat.write(nb, f, nbformat.NO_CONVERT)

 

#change notebook name here

#old_ipynb is the name of the large notebook

old_ipynb = "test.ipynb"

new_ipynb = "new.ipynb"

 

clear_notebook(old_ipynb, new_ipynb)

 

Note: it must be formatted identically to the image shown below for it to work effectively.

Picture1.png

 

3. Change the name of the notebook listed in “old_ipynb = ‘test.ipynb’” to the name of the large notebook you’d like to duplicate without outputs. You can find the name of the workspace by clicking the notebook in your workspace and checking the URL; it will be the last part as shown below.

 

Picture2.png

Picture3.png

 

4. You can also change the name of the new duplicated notebook (“new.ipynb”), but this isn’t required unless you already have an existing notebook called “new.ipynb”. If this is the case, then please change it so it’s not overwritten.

 

5. Run the code by going to Cell > Run All. 

Picture4.png

 

6. After running the code, you should get a new notebook called “new.ipynb“. If you can’t find the notebook in your workspace-analysis page, please go to “File-open“ to find and save it as a copy (new_5.ipynb) after double clicking to open it. This is shown below. 

Picture5.png

Picture12.png

Picture7.png

Picture8.png

Picture17.png

Picture18.png

 

7. After this step you should be able to find the new notebook (new5.ipynb) just saved in your workspace.

 

 

 

 

 

 

Was this article helpful?

0 out of 0 found this helpful

Have more questions? Submit a request