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

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

问题描述

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

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

代码:

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

单元格和输出:

但是如何只运行 line 2?甚至在不运行整个单元的情况下选择单元中的某些线?当然,您可以只插入带有单行或选定行的单元格,但这确实很麻烦,而且很快就会变得凌乱.那么有更好的方法吗?

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?

推荐答案

更新的答案

自从我的第一个答案以来,JupyterLab已有一些更新(我现在在1.1.4上),并且有人指出

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:

使用Run > Run selected line or highlighted text和指定的键盘快捷键在控制台中运行代码.

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:

以下是在Settings > Advanced Settings > Keyboard shortcuts中设置快捷方式的方法:

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

这是您需要在Settings > Keyboard Shortcuts > User preferences >下添加的内容:

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"
        },
    ]
}

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

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

旧版本的原始答案:

Jupyter笔记本

  1. qtconsole
  2. 便签本

JupyterLab:

  1. qtconsole
  2. Run > Run Selected Text or Current Line in Console,可以选择使用键盘快捷键
  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.

Jupyter Notebook选项1: qtconsole

可以说,插入新单元格最灵活的选择是使用魔术函数打开IPython控制台

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

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

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笔记本选项2: Scratchpad笔记本扩展

安装成功后,您可以使用ctrl + B启动便签本:

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

JupyterLab选项1: %qtconsole

以与笔记本相同的方式工作

Works the same way as for a Notebook

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

为新版本的JupyterLab内置了与qtconsole类似的选项,但可以说更加优雅.现在,您可以将标记放在一行上,或突出显示一个选择,然后使用菜单选项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:

您仍然可以在IPython控制台中获得结果,但是不必使用%qtconsole添加额外的行,并且在单元格中运行选择的行要容易得多:

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:

您可以通过分配键盘快捷键使事情变得更加轻松 进入菜单选项Run > Run Selected Text or Current Line in Console,如下所示:

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-转到Settings并选择Advanced Settings editor:

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

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-复制该部件并将其粘贴到User Overrides下,并在keys下键入所需的快捷方式,如下所示:

3 - Copy that part and paste it under User Overrides and type in your desired shortcut below keys like so:

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

4-单击File下的Save All.

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

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

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

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

您首选的方法将取决于相关行的输出性质.以下是 plotly 的示例.可能会随时间增加更多示例.

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.-密谋

密谋数字将不会直接显示在Jupyter QtConsole中(可能与

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 Juphyterlab using Run > Run Selected Text or Current Line in Console will handle plotly figures just fine.

代码段:

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 -使用便签本

1.2 -使用突出显示的行和键盘快捷键在JupyterLab控制台上进行绘图:

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

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

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