Ipython Notebook Clear Cell Output in Code

ipython notebook clear cell output in code

You can use IPython.display.clear_output to clear the output of a cell.

from IPython.display import clear_output

for i in range(10):
clear_output(wait=True)
print("Hello World!")

At the end of this loop you will only see one Hello World!.

Without a code example it's not easy to give you working code. Probably buffering the latest n events is a good strategy. Whenever the buffer changes you can clear the cell's output and print the buffer again.

Keyboard shortcut to clear cell output in Jupyter notebook

You can setup your own shortcut in the UI (for the latest master version):

Sample Image

This menu can be found in Help > Keyboard Shortcuts in any open notebook.

How to clear Jupyter Notebook's output in all cells from the Linux terminal?

nbconvert 6.0 should fix --clear-output

The option had been broken for a long time previously, bug report with merged patch: https://github.com/jupyter/nbconvert/issues/822

Usage should be for in-place operation:

jupyter nbconvert --clear-output --inplace my_notebook.ipynb

Or to save to another file called my_notebook_no_out.ipynb:

jupyter nbconvert --clear-output \
--to notebook --output=my_notebook_no_out my_notebook.ipynb

This was brought to my attention by Harold in the comments.

Before nbconvert 6.0: --ClearOutputPreprocessor.enabled=True

Same usage as --clear-output:

jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace my_notebook.ipynb
jupyter nbconvert --ClearOutputPreprocessor.enabled=True \
--to notebook --output=my_notebook_no_out my_notebook.ipynb

Tested in Jupyter 4.4.0, notebook==5.7.6.

JupyterLab: How to clear output of current cell using a keyboard shortcut?

The answer:

You'll have to assign a custom shortcut key under Settings > Advanced Settings Editor by inserting the following under User Preferences:

{// List of Keyboard Shortcuts
"shortcuts": [
{
"command": "notebook:clear-cell-output",
"keys": [
"F10"
],
"selector": ".jp-Notebook.jp-mod-editMode"
},
]
}

I went for F10, but most other keys or combination of keys should work too. I've also used Ctrl Shift Enter.

Where to put it:

Sample Image

Some details:

If you've assigned other shortcuts, make sure to add it in the correct place in the list of other shortcuts.

{// List of Keyboard Shortcuts
"shortcuts": [
{
"command": "notebook:run-in-console",
"keys": [
"F9"
],
"selector": ".jp-Notebook.jp-mod-editMode"
},
{
"command": "notebook:clear-cell-output",
"keys": [
"F10"
],
"selector": ".jp-Notebook.jp-mod-editMode"
},
]
}

And a little mystery:

If you insert the exact same thing as in the second box, you'll see that the item Run > Run Selected Text or Current Line in Console has gotten a nice F9 right next to it:

Sample Image

This will not be the case for the item Edit > Clear Outputs, and I'll have to say that I don't know why.

Sample Image

To my knowledge the "command": "notebook:clear-cell-output" that you're assigning to your chosen keyboard shortcut should be that exact functionality. But the good thing is that it works perfectly all the same. At least it does for me.

Please note that this approach works best for newer versions of JupyterLab. The correct way will be a bit different for older versions.

Here's a python snippet to test it out right away:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
print(df)

ipython notebook clear all code

In Jupyter, do the following to clear all cells:

  1. Press Esc to enter command mode.
  2. Hold Shift. Select the first and last cells to select all cells.*
  3. Press d twice to delete all selected cells.

Alternatively, if you simply want to try out code, consider running the ipython console, which is purely interactive in the REPL and does not require creating a new file. In a command prompt, type:

> ipython

Demo

Click outside the textbox to select a cell in command mode (thanks @moondra's).

Command Mode (Yes)

Sample Image

Edit Mode (No)

Sample Image



Related Topics



Leave a reply



Submit