如何在 Jupyter Notebook 或 JupyterLab 单元中运行单行或选定的代码? [英] How to run a single line or selected code in a Jupyter Notebook or JupyterLab cell?

查看:431
本文介绍了如何在 Jupyter Notebook 或 JupyterLab 单元中运行单行或选定的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 JupyterLab 和 Jupyter Notebook 中,您都可以使用 ctrl + Enter 执行单元格:

代码:

print('line 1')打印('第2行')打印('第3行')

单元格和输出:

但是你怎么能只运行 line 2?或者甚至在不运行整个单元格的情况下选择单元格内的行?当然,您可以只插入带有单行或多行选择的单元格,但这会很快变得非常麻烦和混乱.那么有没有更好的方法来做到这一点?

解决方案

更新答案

自从我第一次回答以来,JupyterLab 有一些更新(我现在在 1.1.4),并且有人说

这是在设置>中设置快捷方式的方法;高级设置键盘快捷键:

这是您需要在 Settings > 下添加的内容键盘快捷键 >用户偏好 >:

<代码>{//键盘快捷键列表快捷方式":[{命令":笔记本:控制台运行",键":[F9"],选择器":.jp-Notebook.jp-mod-editMode"},]}

快捷方式甚至会显示在菜单中.我选择使用 F9


旧版本的原始答案:


简答:

Jupyter 笔记本:

  1. qtconsole
  2. 便笺本

JupyterLab:

  1. qtconsole
  2. 运行 >在控制台中运行选定的文本或当前行,可选择使用键盘快捷键

看看下面的详细信息,以及答案最后编辑中的一些特殊情况.


详情:

Jupyter Notebook 选项 1: qtconsole

可以说是插入新单元格最灵活的替代方法是使用魔术函数打开 IPython 控制台

%qtconsole

对于更花哨的控制台,您可以使用

%qtconsole --style vim

在此控制台中执行的行的结果也可用于 Jupyter Notebook,因为它仍然是正在运行的内核.一个缺点是您必须在控制台中复制并粘贴或键入所需的行.

[

Jupyter Notebook 选项 2:

JupyterLab 选项 1: %qtconsole

工作方式与笔记本相同

JupyterLab 选项 2: 运行 >在控制台中运行选定的文本或当前行

新版本的 JupyterLab 内置了一个与 qtconsole 类似的选项,但可以说更优雅.现在您可以将标记放在一行上,或突出显示一个选择,然后使用菜单选项 Run >在控制台中运行选定的文本或当前行:

您仍将在 IPython 控制台中获得结果,但您不必使用 %qtconsole 添加额外的行,而且在一个单元格:

您可以通过分配键盘快捷键使事情变得更容易到菜单选项 Run >在控制台中运行选定的文本或当前行,如下所示:

1 - 转到 Settings 并选择 Advanced Settings editor:

2 -Keyboard shortcuts 标签 下,执行 ctrl+F 搜索 run-in-console> 找到以下部分:

//[缺少架构标题]//[缺少架构描述]笔记本:在控制台运行":{命令":笔记本:控制台运行",键":["],选择器":.jp-Notebook.jp-mod-editMode",标题":在控制台中运行",类别":笔记本细胞操作"}

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

<代码>[...]键":[F9"],[...]

4 - 点击 File 下的 Save All.

5 - 如果过程顺利,您会看到菜单选项已更改:

6 - 您可能需要重新启动 JupyterLab,但现在您可以使用所需的快捷方式轻松运行一行或选择几行.

##特殊情况

您的首选方法将取决于相关行的输出性质.下面是一个

1.2 - 使用突出显示的行和键盘快捷键与 JupyterLab 控制台一起绘制:

In both JupyterLab and Jupyter Notebook you can execute a cell using ctrl + Enter:

Code:

print('line 1')
print('line 2')
print('line 3')

Cell and output:

But how can you run only line 2? Or even a selection of lines within a cell without running the entire cell? Sure you could just insert a cell with that single line or selection of lines, but that gets really cumbersome and messy really quick. So are there better ways of doing this?

解决方案

Updated answer

As there have been a few updates of JupyterLab since my first answer (I'm now on 1.1.4), and it has been stated that JupyterLab 1.0 will eventually replace the classic Jupyter Notebook, here's what I think is the best approach right now and even more so in the time to come:

In JupyterLab use Run > Run selected line or highlighted text with an assigned keyboard shortcut to run code in the console.

Here's how it will look like when you run the three print statements line by line using a keyboard shortcut:

Here's how you set up a shortcut in Settings > Advanced Settings > Keyboard shortcuts:

And here's what you need to add under Settings > Keyboard Shortcuts > User preferences > :

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

The shortcut will even show in the menu. I've chosen to use F9


Original answer for older versions:


Short answer:

Jupyter notebook:

  1. qtconsole
  2. scratchpad

JupyterLab:

  1. qtconsole
  2. Run > Run Selected Text or Current Line in Console, optionally with a keyboard shortcut

Have a look at the details below, as well as some special cases in an edit at the very end of the answer.


The details:

Jupyter Notebook option 1: qtconsole

The arguably most flexible alternative to inserting new cell is to open an IPython console using the magic function

%qtconsole

For a bit more fancy console you can use

%qtconsole --style vim

The results of the lines executed in this console will also be available to the Jupyter Notebook since it's still the same kernel that's running. One drawback is that you'll have to copy&paste or type the desired lines into the console.

[

Jupyter Notebook option 2: Scratchpad Notebook Extension

With a successful installation you can launch a Scratchpad with ctrl + B:

JupyterLab option 1: %qtconsole

Works the same way as for a Notebook

JupyterLab option 2: Run > Run Selected Text or Current Line in Console

A similar option to a qtconsole, but arguably more elegant, has been built in for newer versions of JupyterLab. Now you canput your marker on a single line, or highlight a selection, and use the menu option Run > Run Selected Text or Current Line in Console:

You're still going to get your results in an IPython console, but you don't have to add an extra line with %qtconsole and it's much easier to run a selection of lines within a cell:

You can make things even easier by assigning a keyboard shortcut to the menu option Run > Run Selected Text or Current Line in Console like this:

1 - Go to Settings and select Advanced Settings editor:

2 - Under the Keyboard shortcuts tab, do a ctrl+F search for run-in-console to locate the following section:

// [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 - Copy that part and paste it under User Overrides and type in your desired shortcut below keys like so:

[...]
"keys": [
  "F9"
],
[...]

4 - Click Save All under File.

5 - If the process went smoothly, you'll see that your menu option has changed:

6 - You may have to restart JupyterLab, but now you can easily run a single line or selection of lines with your desired shortcut.

##EDIT: Special cases

Your preferred approach will depend on the nature of the output of the lines in question. Below is an example with plotly. More examples will possibly be added with time.

1. - plotly

plotly figures will not be displayed directly in a Jupyter QtConsole (possibly related to this), but both the Scratchpad in a Jupyter Notebook and the integrated console in Jupyterlab using Run > Run Selected Text or Current Line in Console will handle plotly figures just fine.

Snippet:

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
init_notebook_mode(connected=True)

trace0 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[10, 15, 13, 17]
)

fig = go.Figure([trace0])
iplot(fig)

1.1 - plotly with scratchpad

1.2 - plotly with JupyterLab console using highlighted line and keyboard shortcut:

这篇关于如何在 Jupyter Notebook 或 JupyterLab 单元中运行单行或选定的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆