python - 在 Jupyter Lab 中,在 Python 控制台中执行编辑器代码

标签 python anaconda jupyter jupyter-notebook

JupyterLab , 我想将代码从编辑器发送到 Python 控制台执行,最好使用键盘快捷键。文档似乎没有提供执行此操作的方法,但它是 IDE 的一个基本方面,我认为它可能是可能的。

最佳答案

答案:

选择您想要的行并使用Run > Run Selected Text or Current Line in Console,或者在Settings > Advanced Settings > Keyboard Shortcuts下定义您自己的快捷方式:

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

详情:

选项 1 - 将代码从编辑器发送到 Python 控制台:

当单元格处于事件状态时,单击运行并选择在控制台中运行选定的文本或当前行

enter image description here

测试运行和输出:

enter image description here

对于那些第一次尝试 JupyterLab 的人来说,这与点击 ctrl+Enter 并在 JupyterLab 本身中获取输出的标准选项相反:

enter image description here


选项 2 - 分配和使用键盘快捷键:

这没有标准的快捷方式,但如果您遵循以下几个简单的步骤,您可以很容易地自行设置它:

2.1 -转到设置并选择高级设置编辑器:

enter image description here

**

新版本的步骤 2.2 -User Preferences 下插入以下内容:

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

enter image description here

旧版本的步骤 2.2-User Overrides 下写入以下内容,并在 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

如您所见,我首选的快捷键是 F9

2.3 - 点击 File 下的 Save All

如果您关闭并重新打开您的笔记本,您会看到您已将 F9 指定为菜单本身的快捷方式:

enter image description here

2.4.1 - 运行单行/将单行发送到 IPython 控制台。

只需将标记放在所需的行上,然后单击 F9:

enter image description here

2.4.2 - 运行选定的代码/将选定的文本发送到 IPython 控制台:

只需选择您想要的代码并单击F9

enter image description here

关于python - 在 Jupyter Lab 中,在 Python 控制台中执行编辑器代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38648286/

相关文章:

python - qt 和 PyQt4 的区别

python - 无法使用 python 3.9 env 运行 Jupyter Notebook

python - Pip 在 Anaconda Python 中搞砸了

jupyter - %stored Jupyter 文件位于何处?

python - python中没有重复字符的最长子串

python - 请问如何从目录中加载 XML 文件

python - 在Elasticsearch中查询嵌套对象

cygwin - 在 Windows 10 Python 3 上安装 Fasttext 时出错

visual-studio-code - vscode 中的 Jupyter - 转换为原始 NBCovert 或停用单元格?

matplotlib - Jupyter Notebook 中的输出图形大小