简体   繁体   English

在 IPython/Jupyter Notebooks 中显示行号

[英]Showing line numbers in IPython/Jupyter Notebooks

Error reports from most language kernels running in IPython/Jupyter Notebooks indicate the line on which the error occurred;在 IPython/Jupyter Notebooks 中运行的大多数语言内核的错误报告指出了发生错误的行; but (at least by default) no line numbers are indicated in Notebooks.但是(至少在默认情况下)笔记本中没有显示行号。

Is it possibile to add the line numbers to IPython/Jupyter Notebooks?是否可以将行号添加到 IPython/Jupyter Notebooks 中?

CTRL - M L toggles line numbers in the CodeMirror area. CTRL - M L切换 CodeMirror 区域中的行号。 See the QuickHelp for other keyboard shortcuts.有关其他键盘快捷键,请参阅 QuickHelp。

In more details CTRL - M (or ESC ) bring you to command mode, then pressing the L keys should toggle the visibility of current cell line numbers.在更多细节CTRL - M (或ESC )带你到命令模式,然后按L键应该切换当前单元格行号的可见性。 In more recent notebook versions Shift-L should toggle for all cells.在较新的笔记本版本中, Shift-L应该为所有单元格切换。

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.如果您不记得快捷方式,请调出命令面板Ctrl-Shift+P (在 Mac 上为Cmd+Shift+P ),然后搜索“行号”),它应该允许切换并显示快捷方式。

On IPython 2.2.0, just typing l (lowercase L) on command mode (activated by typing Esc) works.在 IPython 2.2.0 上,只需在命令模式下输入 l(小写 L)(通过输入 Esc 激活)即可。 See [Help] - [Keyboard Shortcuts] for other shortcuts.其他快捷键请参见【帮助】-【键盘快捷键】。

Also, you can set default behavior to display line numbers by editing custom.js .此外,您可以通过编辑custom.js来设置默认行为以显示行号

Select the Toggle Line Number Option from the View -> Toggle Line Number .View -> Toggle Line Number 中选择 Toggle Line Number 选项。

菜单看起来像这样

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 配置目录,您可以通过在命令行中键入以下内容来找到该目录:

     jupyter --config-dir
  2. From there, open or create the custom folder.从那里,打开或创建custom文件夹。

  3. In that folder, you should find a custom.js file.在该文件夹中,您应该找到一个custom.js文件。 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; } ); } );

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在运行 ipython3 的 OSX 上,它是ESC L

For me, ctrl + m is used to save the webpage as png, so it does not work properly.对我来说, ctrl + m是用来把网页保存为png的,所以不能正常使用。 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.在工具栏底部有一个叫open the command Paletee 的地方,点击后输入行号,这里可以看到切换单元格的行号。

In Jupyter Lab 2.1.5, it is View -> Show Line Numbers .在 Jupyter Lab 2.1.5 中,它是View -> Show Line Numbers

在此处输入图片说明

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

You can also find Toggle Line Numbers under View on the top toolbar of the Jupyter notebook in your browser.您还可以在浏览器中 Jupyter 笔记本顶部工具栏的View下找到Toggle Line Numbers This adds/removes the lines numbers in all notebook cells.这将添加/删除所有笔记本单元格中的行号。

For me, Esc + l only added/removed the line numbers of the active cell.对我来说, Esc + l只添加/删除了活动单元格的行号。

To show line numbers by default in Jupyter lab,要在 Jupyter 实验室中默认显示行号,

  • Click on the menu SettingsAdvanced Settings Editor (or use the shortcut control + , ).单击菜单SettingsAdvanced Settings Editor (或使用快捷键control + , )。 An environment with three columns will appear: the settings category, System Defaults , and User Preferences .将出现一个包含三列的环境:设置类别、 System DefaultsUser Preferences
  • Select the category notebook选择分类notebook
  • Set the key "lineNumbers" to true in the "codeCellConfig" options."codeCellConfig"选项中将键"lineNumbers"true

For example, (assuming no other user configured settings),:例如,(假设没有其他用户配置的设置),:

{
    "codeCellConfig": {
        "lineNumbers": true,
    }
}
  1. Press Escape so the left part becomes blue.Escape使左侧部分变为蓝色。

在此处输入图片说明

  1. Then press L .然后按L It works!有用!

  2. If you want to hide them, use the same algorithm.如果要隐藏它们,请使用相同的算法。

  3. If you want to impact all Jupiter rows, make sure to do it on the last one, otherwise only one row is impacted.如果您想影响所有木星行,请确保在最后一行执行此操作,否则只会影响一行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在IPython / Jupyter笔记本中自动加载配置文件? - How to automatically load profile in IPython/Jupyter notebooks? 在 iPython/Jupyter Notebooks 中安装多个 Python 3 内核 - Installing multiple Python 3 kernels in iPython/Jupyter Notebooks 将代码片段复制到IPython / Jupyter笔记本中 - Copying code snippets into IPython / Jupyter Notebooks ipython 和 jupyter 笔记本不能自动完成(已修复,解决方案已解释) - ipython and jupyter notebooks not autocompleting(Fixed, Solution explained) 如何在 Jupyter Notebooks for iPython 中安装模块 - How to install module in Jupyter Notebooks for iPython IPython 启动脚本已永久更改 Jupyter Notebooks 行为 - IPython startup script has permanently changed Jupyter Notebooks behaviour 我的图形没有显示,并且我的iPython Notebooks从未停止工作 - My graph is not showing up and my iPython Notebooks never stops working 在make文件/中从命令行执行IPython笔记本 - Executing IPython notebooks in a make file / from the command line Matplotlib 在所有 Jupyter 笔记本中显示空白数字(无错误) - Matplotlib showing blank figures in all Jupyter notebooks (no errors) VSCode 中的 Jupyter 笔记本未显示最新的 Python 内核列表 - Jupyter notebooks in VSCode not showing up to date python kernel list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM