简体   繁体   中英

Python in VS Code: Can I run cell in the integrated terminal?

In VS Code with Python, we can run a "cell" (block that starts with #%% ) in the "Python Interactive Window"

Can we do the same thing on the Integrated Terminal ?

I know we can do this in Spyder , where the terminal is generally always an IPython terminal

Matlab works in the same way with its terminal.

Can we do this in VS Code?

I've opened a issue in GitHub , @AdamAL also opened , but it seems they don't have intention to do this. Here is a workaround to other users.

EDIT:

I've answered before with a workaround that takes 2 VSCode extensions and not use the smart command jupyter.selectCellContents .
@AdamAL shared a better solution using this command, but with the caveat that the cursor loses position and focusing only in Python terminal.
@Maxime Beau pointed that out expanding @AdamAL solution to the active terminal (that could be IPython, for instance)

Now I'm taking all answers and posting a general solution. This solution don't loose the cursor position when running only one cell. Also there's another command that runs the cell and advance to the next cell (as in Jupyter) and it is general to the active terminal (that could be IPython).

1. Installmacros extension:

This extension have some additional commands that multi-command didn't have (like the delay command)

2. In settings.json

"macros.list": {
    "runCellinTerminal": [
        "jupyter.selectCellContents",
        "workbench.action.terminal.runSelectedText",
        "cursorUndo",
        {"command": "$delay","args": {"delay": 100}},
        {"command": "workbench.action.terminal.sendSequence","args": { "text": "\n" }},
    ],
    "runCellinTerminaladvance": [
        "jupyter.selectCellContents",
        "workbench.action.terminal.runSelectedText",
        "cursorDown",
        {"command": "$delay","args": {"delay": 100}},
        {"command": "workbench.action.terminal.sendSequence","args": { "text": "\n" }},
    ],
}

OBS: cursorUndo takes the cursor back to the right position. cursorDown takes the cursor to the next cell. The commands delay and sendSequence \n are usefull when the terminal is an IPython terminal.

3. In keybinding.json:

{
    "key": "ctrl+alt+enter",
    "command": "macros.runCellinTerminal",
    "when": "editorTextFocus && jupyter.hascodecells"
},
{
    "key": "shift+alt+enter",
    "command": "macros.runCellinTerminaladvance",
    "when": "editorTextFocus && jupyter.hascodecells"
},

There is a new feature request for this here . Please, go and vote there too. @Diogo's feature request is closed.

I found another workaround, similar to Diogo's answer , though slightly less involved, requiring only one additional extension (assuming MS's Python is installed already).

The caveat is that you lose the cursor position (jumps to the end of block after execution).

  1. Install multi-command

  2. In settings.json :

    "multiCommand.commands": [
        {
            "command": "multiCommand.runCellInPythonTerminal",
            "label": "Run cell in python terminal",
            "sequence": [
                "jupyter.selectCellContents",
                "python.execSelectionInTerminal",
                "cursorDown"
            ]
        }
    ]
  1. In keybinding.json :
    {
        "key": "ctrl+shift+enter",
        "command": "multiCommand.runCellInPythonTerminal",
        "when": "editorTextFocus && jupyter.hascodecells"
    }

If you wish to execute the cell contents in the CURRENTLY ACTIVE terminal, you need to tweak AdamAL's solution:

  1. Install multi-command

  2. In settings.json :

    "multiCommand.commands": [
        {
            "command": "multiCommand.runCellInPythonTerminal",
            "label": "Run cell in python terminal",
            "sequence": [
                "jupyter.selectCellContents",
                "workbench.action.terminal.runSelectedText",
            ]
        }
    ]
  1. In keybinding.json (same location as settings.json):
   {
       "key": "ctrl+shift+enter",
       "command": "multiCommand.runCellInPythonTerminal",
       "when": "editorTextFocus && jupyter.hascodecells"
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM