在Jupyter实验室中,在Python控制台中执行编辑器代码

问题描述 投票:3回答:4

Jupyter Lab中,我想将编辑器中的代码发送到Python控制台执行,最好使用键盘快捷键。文档似乎没有提供这样做的方法,但它是IDE的一个基本方面,我想它可能是可能的。

python anaconda jupyter jupyter-notebook
4个回答
3
投票

这计划作为1.0版本的功能。见https://github.com/jupyter/jupyterlab/issues/450


3
投票

此功能现已发布。你需要做的就是

  1. 右键单击脚本并创建控制台编辑器
  2. 复制整个代码并按shift + Enter

2
投票

这可能是一种解决方法,但您可以做的是打开交互式控制台会话。这使用IPython,因此您可以使用魔法%run来执行.py文件。所以做点什么

%run ./yourFile.py

1
投票

答案:

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


细节:

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

当单元格处于活动状态时,单击Run并选择Run Selected Text or Current Line in Console

enter image description here

测试运行和输出:

enter image description here

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

enter image description here


2.如何使用键盘快捷键进行操作

没有标准的快捷方式,但如果您按照以下几个简单的步骤操作,您可以轻松自行设置:

1 - 去Settings并选择Advanced Settings editor

enter image description here

2 - 在出现的Settings标签下,运行ctrl+F搜索run-in-console以找到以下部分:

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

3 - 复制该部分并将其粘贴在User Overrides下,然后在keys下面输入所需的快捷方式:

enter image description here

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

4 - 点击Save All下的File

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

enter image description here

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

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

enter image description here

5.2 - 运行所选代码/将所选文本发送到IPython控制台:

只需选择所需的代码,然后单击F9

enter image description here


0
投票

这也可以通过在该行上用光标击中shift + enter来完成单行(而不是选择整行,然后按shift + enter)。

© www.soinside.com 2019 - 2024. All rights reserved.