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

左心房为你撑大大i 提交于 2020-05-15 20:58:07

问题


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?


回答1:


I've opened a issue in GitHub, but it seems they don't have intention to do this.

I'm putting a workaround, for other users:

1. Have "Select By" extension:

https://marketplace.visualstudio.com/items?itemName=rioj7.select-by
https://github.com/rioj7/select-by

2. Have "macro" extension:

https://marketplace.visualstudio.com/items?itemName=ctf0.macros
https://github.com/ctf0/macros

3. Put the bellow in settings.json:

"selectby.regexes": {
    "regex1": {
        "flags": "i",
        "backward": "#.*%%.*\r",
        "forward": "#.*%%.*\r",
        "backwardInclude": false,
        "forwardInclude": false
    },
},
"macros.list": {
    "RunCellinTerminal": [
        "cursorEnd",
        "selectby.regex1",
        "workbench.action.terminal.runSelectedText",
        "cursorEnd",
    ]
}

4. Execute the macro "RunCellinTerminal"

With the cursor inside a desired cell, when the macro is executed, the cell's region will be selected and the text will be send to the opened active terminal.

Here's a example: (it is possible to add a key-bind)



来源:https://stackoverflow.com/questions/61275551/python-in-vs-code-can-i-run-cell-in-the-integrated-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!