gpt4 book ai didi

python - 如何在Jupyter Notebook或JupyterLab单元中运行一行或选定的代码?

转载 作者:太空狗 更新时间:2023-10-30 02:51:20 60 4
gpt4 key购买 nike

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

代码:

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

单元格和输出:

cell and output

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

最佳答案

更新的答案
自从我的第一个答案以来,JupyterLab进行了一些更新(我现在在1.1.4上),并且有人说JupyterLab 1.0 will eventually replace the classic Jupyter Notebook,这是我认为目前最好的方法,并且在以后的时间里甚至更多:
在JupyterLab中,使用Run > Run selected line or highlighted text和分配的键盘快捷键在控制台中运行代码。
这是使用键盘快捷键逐行运行三个打印语句时的外观:
enter image description here
这是在Settings > Advanced Settings > Keyboard shortcuts中设置快捷方式的方法:
enter image description here
这是您需要在Settings > Keyboard Shortcuts > User preferences > 下添加的内容:

{
// List of Keyboard Shortcuts
"shortcuts": [
{
"command": "notebook:run-in-console",
"keys": [
"F9"
],
"selector": ".jp-Notebook.jp-mod-editMode"
},
]
}
快捷方式甚至会显示在菜单中。我选择使用 F9 enter image description here

较早版本的原始答案:

简短答案:
Jupyter笔记本:
  • qtconsole
  • 便笺簿

  • JupyterLab:
  • qtconsole
  • Run > Run Selected Text or Current Line in Console,可以选择使用键盘快捷键

  • 在答案的最后,通过编辑了解一下下面的详细信息,以及一些 特殊情况

    细节:
    Jupyter Notebook选项1: qtconsole可以说,插入新单元格最灵活的替代方法是使用magic函数打开IPython控制台。
    %qtconsole
    对于更高级的控制台,您可以使用
    %qtconsole --style vim
    在Jupyter Notebook中,在该控制台中执行的代码行的结果也将可用,因为它仍与正在运行的内核相同。缺点之一是,您必须复制并粘贴或在控制台中键入所需的行。
    [
    Jupyter Notebook选项2: Scratchpad Notebook Extension
    安装成功后,您可以使用 ctrl + B启动Scratchpad:
    enter image description here
    JupyterLab选项1: %qtconsole工作方式与笔记本电脑相同
    JupyterLab选项2: Run > Run Selected Text or Current Line in Console对于较新版本的JupyterLab,内置了与qtconsole类似的选项,但可以说更加优雅。现在,您可以将标记放在一行上,或突出显示一个选择,然后使用菜单选项 Run > Run Selected Text or Current Line in Console:
    enter image description here
    您仍将在IPython控制台中获得结果,但不必在 %qtconsole中添加额外的行,并且在单元格中运行选择的行要容易得多:
    enter image description here
    您可以通过分配键盘快捷键使事情变得更加轻松
    菜单选项 Run > Run Selected Text or Current Line in Console像这样:
    1-转到 Settings并选择 Advanced Settings editor:
    2-Keyboard shortcuts tab下,对 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下键入所需的快捷方式,如下所示:
    [...]
    "keys": [
    "F9"
    ],
    [...]
    4-单击 Save All下的 File
    5-如果过程顺利进行,您将看到菜单选项已更改:
    enter image description here
    6-您可能必须重新启动JupyterLab,但是现在您可以使用所需的快捷方式轻松地运行一行或选择几行。
    ## EDIT:特殊情况
    您首选的方法将取决于相关行的输出性质。以下是 plotly的示例。随着时间的流逝,可能会添加更多示例。
    1. -密谋
    可打印的图形不会直接显示在Jupyter QtConsole中(可能与 this相关),但是Jupyter Notebook中的Scratchpad和Jupyterlab中使用 Run > Run Selected Text or Current Line in Console的集成控制台都可以很好地处理可打印图形。
    代码段:
    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 -使用便签本
    enter image description here
    1.2 -使用突出显示的行和键盘快捷键在JupyterLab控制台上进行绘图:
    enter image description here

    关于python - 如何在Jupyter Notebook或JupyterLab单元中运行一行或选定的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56460834/

    60 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com