How to execute a single line or selected code in a Jupyter Notebook or JupyterLab cell?

…衆ロ難τιáo~ 提交于 2019-12-22 07:52:13

问题


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?


回答1:


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 Juphyterlab 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:



来源:https://stackoverflow.com/questions/56460834/how-to-execute-a-single-line-or-selected-code-in-a-jupyter-notebook-or-jupyterla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!