17

In JupyterLab, I want to send code from the editor to the Python console for execution, preferably with a keyboard shortcut. The documentation doesn't seem to offer a way to do this, but it's such a fundamental aspect of an IDE that I imagine it's probably possible.

MarianD
  • 13,096
  • 12
  • 42
  • 54
rsoren
  • 4,036
  • 3
  • 26
  • 37
  • For the newest version: https://stackoverflow.com/questions/56460834/how-to-execute-a-single-line-or-selected-code-in-a-jupyter-notebook-or-jupyterla – vestland Nov 20 '19 at 17:03

5 Answers5

20

The answer:

Select your desired line(s) and use Run > Run Selected Text or Current Line in Console, or define your own shortcut under Settings > Advanced Settings > Keyboard Shortcuts:

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

The details:

Option 1 - Send code from the editor to the Python console:

While the cell is active, click Run and select Run Selected Text or Current Line in Console.

enter image description here

Test run and output:

enter image description here

For those trying JupyterLab for the first time, this is opposed to the standard option of hitting ctrl+Enter and getting the output within JupyterLab itself:

enter image description here


Option 2 - Assign and use a keyboard shortcut:

There's no standard shortcut for this, but you can quite easily set it up yourself if you follow these few easy steps:

2.1 - Go to Settings and select Advanced Settings editor:

enter image description here

**

Step 2.2 for newer versions - Insert the following under User Preferences:

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

enter image description here

Step 2.2 for older versions- Write the following under User Overrides and type in your desired shortcut below keys:

// [missing schema title]
    // [missing schema description]
    "notebook:run-in-console": {
      "command": "notebook:run-in-console",
      "keys": [
        "F9"
      ],
      "selector": ".jp-Notebook.jp-mod-editMode",
      "title": "Run In Console",
      "category": "Notebook Cell Operations"
    }

enter image description here

As you can see, my preferred shortcut is F9.

2.3 - Click Save All under File.

And if you close and reopen your notebook, you'll see that you've assigned F9 as a shortcut in the menu itself:

enter image description here

2.4.1 - Run single line / Send single line to IPython console.

Just put your marker on the desired line and click F9:

enter image description here

2.4.2 - Run selected code / send selected text to IPython console:

Just select your desired code and click F9

enter image description here

vestland
  • 55,229
  • 37
  • 187
  • 305
  • 2
    This works for notebooks, and a big thanks for the explanation in this regard! However, this does not affect code from text editors, unfortunately. Is something like that possible for plain text editor as well? – Maxim.K May 29 '20 at 13:21
  • @Maxim.K have you found a way to do this, this would make my life so much easier as well1 – Topde Sep 24 '20 at 12:21
  • @Dee afraid not – Maxim.K Sep 25 '20 at 13:10
16

This feature has been released now. All you need to do is

  1. Right click the script and create a console editor
  2. Copy the whole code and press shift + Enter
sushmit
  • 4,369
  • 2
  • 35
  • 38
7

This may be a workaround, but what you can do is open an interactive console session. This uses IPython, so then you can use the magic %run to execute a .py file. So do something like

%run ./yourFile.py
Julius
  • 304
  • 4
  • 16
4

This is planned as a feature for the 1.0 release. See https://github.com/jupyter/jupyterlab/issues/450

rsoren
  • 4,036
  • 3
  • 26
  • 37
3

This can also be done for a single line by hitting shift + enter with the cursor on that line (as opposed to selecting the entire line, then hitting shift + enter).

Joshua Cook
  • 12,495
  • 2
  • 35
  • 31