In Jupyter Lab, execute editor code in Python console

后端 未结 5 791
深忆病人
深忆病人 2021-01-02 01:49

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 t

5条回答
  •  独厮守ぢ
    2021-01-02 02:35

    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.

    Test run and output:

    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:


    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:

    **

    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"
            },
        ]
    }
    

    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"
        }
    

    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:

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

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

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

    Just select your desired code and click F9

提交回复
热议问题