Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This question has been asked and answered for Jupyter Notebooks here. There is one suggestion regarding JupyterLab there as well on how to hide cell output, but not to clear it.

This is easy enough using the menu under Edit > Clear Outputs. But how do you do it using a keyboard shortcut? Many other commands under Edit already have their own assigned shortcuts, but not this one:

enter image description here

like image 517
vestland Avatar asked Feb 20 '20 11:02

vestland


People also ask

How do you clear the output of a cell in Jupyter?

When you have Jupyter notebook opened, you can do this by selecting the Cell -> All Output -> Clear menu item.

How do you clear all outputs in Jupyter lab?

Press 'control-shift-p', that opens the command palette. Then type 'clear cell output'. That will let you select the command to clear the output.

How do you stop current cell in jupyter notebook?

Stopping a process or restarting a Jupyter Notebook To interrupt a cell execution, you can click the ■ “stop” button in the ribbon above the notebook, or select “Interrupt Kernel” from the Kernel menue.

How do you collapse the output in a jupyter notebook?

You can collapse the code cell in JupyterLab by pressing the blue vertical bar you'll see when you hover to the left of your code cell. When the notebook is reopened in JupyterLab, the setting will be respected.


1 Answers

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:

enter image description here

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:

enter image description here

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

enter image description here

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)
like image 115
vestland Avatar answered Oct 05 '22 07:10

vestland