在IPython / Jupyter笔记本中显示行号

问题:在IPython / Jupyter笔记本中显示行号

在IPython / Jupyter Notebook中运行的大多数语言内核的错误报告都指出发生错误的行;但是(至少默认情况下)在笔记本电脑中没有显示行号。

是否可以将行号添加到IPython / Jupyter Notebook?

Error reports from most language kernels running in IPython/Jupyter Notebooks indicate the line on which the error occurred; but (at least by default) no line numbers are indicated in Notebooks.

Is it possibile to add the line numbers to IPython/Jupyter Notebooks?


回答 0

CTRLML在CodeMirror区域中切换行号。有关其他键盘快捷键,请参见快速帮助。

更详细地讲CTRLM(或ESC)将您带入命令模式,然后L按键将切换当前单元格行号的可见性。在较新的笔记本电脑版本中,Shift-L应切换所有单元格。

如果您忘记了快捷方式,请调出命令面板Ctrl-Shift+PCmd+Shift+P在Mac上为Mac,然后搜索“行号”),它应该允许切换并显示快捷方式。

CTRLML toggles line numbers in the CodeMirror area. See the QuickHelp for other keyboard shortcuts.

In more details CTRLM (or ESC) bring you to command mode, then pressing the L keys should toggle the visibility of current cell line numbers. In more recent notebook versions Shift-L should toggle for all cells.

If you can’t remember the shortcut, bring up the command palette Ctrl-Shift+P (Cmd+Shift+P on Mac), and search for “line numbers”), it should allow to toggle and show you the shortcut.


回答 1

在IPython 2.2.0上,只需在命令模式(通过键入Esc激活)中键入l(小写L)即可。有关其他快捷方式,请参见[帮助]-[键盘快捷方式]。

另外,您可以设置默认行为以通过编辑显示行号custom.js

On IPython 2.2.0, just typing l (lowercase L) on command mode (activated by typing Esc) works. See [Help] – [Keyboard Shortcuts] for other shortcuts.

Also, you can set default behavior to display line numbers by editing custom.js.


回答 2

View->中选择切换行号选项 Toggle Line Number

Select the Toggle Line Number Option from the View -> Toggle Line Number.


回答 3

要在启动时默认打开所有单元中的行号,我建议使用此链接。我引用:

  1. 导航到jupyter配置目录,您可以通过在命令行中键入以下内容来找到该目录:

    jupyter --config-dir
  2. 从那里打开或创建custom文件夹。

  3. 在该文件夹中,您应该找到一个custom.js文件。如果没有,则应该可以创建一个。在文本编辑器中将其打开并添加以下代码:

    define([
        'base/js/namespace',
        'base/js/events'
        ],
        function(IPython, events) {
            events.on("app_initialized.NotebookApp",
                function () {
                    IPython.Cell.options_default.cm_config.lineNumbers = true;
                }
            );
        }
    );

To turn line numbers on by default in all cells at startup I recommend this link. I quote:

  1. Navigate to your jupyter config directory, which you can find by typing the following at the command line:

    jupyter --config-dir
    
  2. From there, open or create the custom folder.

  3. In that folder, you should find a custom.js file. If there isn’t one, you should be able to create one. Open it in a text editor and add this code:

    define([
        'base/js/namespace',
        'base/js/events'
        ],
        function(IPython, events) {
            events.on("app_initialized.NotebookApp",
                function () {
                    IPython.Cell.options_default.cm_config.lineNumbers = true;
                }
            );
        }
    );
    

回答 4

为了我, ctrl + m用于将网页另存为png,因此无法正常工作。但是我找到了另一种方式。

在工具栏上,有一个名为打开命令paletee的底部,您可以单击它并键入该行,并在此处看到切换单元格的行号。

For me, ctrl + m is used to save the webpage as png, so it does not work properly. But I find another way.

On the toolbar, there is a bottom named open the command paletee, you can click it and type in the line, and you can see the toggle cell line number here.


回答 5

这是了解活动快捷方式的方法(取决于您的操作系统和笔记本电脑的版本,它可能会更改)

Help > Keyboard Shortcuts > toggle line numbers

在运行ipython3的OSX上, ESC L

Here is how to know active shortcut (depending on your OS and notebook version, it might change)

Help > Keyboard Shortcuts > toggle line numbers

On OSX running ipython3 it was ESC L


回答 6

您还可以Toggle Line NumbersView浏览器的Jupyter笔记本顶部工具栏上的下找到。这将添加/删除所有行号笔记本单元。

对我而言,Esc+ l仅添加/删除了活动单元格的行号。

You can also find Toggle Line Numbers under View on the top toolbar of the Jupyter notebook in your browser. This adds/removes the lines numbers in all notebook cells.

For me, Esc+l only added/removed the line numbers of the active cell.


回答 7

正在寻找这个:Shift-L在JupyterLab 1.0.0中

Was looking for this: Shift-L in JupyterLab 1.0.0


回答 8

1.按esc进入命令模式2.按l(小写L)显示行号

1.press esc to enter the command mode 2.perss l(it L in lowcase) to show the line number