默认情况下如何在 IPython Notebook 代码单元格中显示行号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20197471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:50:18  来源:igfitidea点击:

How to display line numbers in IPython Notebook code cell by default

pythonipythonipython-notebook

提问by khstacking

I would like my default display for IPython notebook code cells to include line numbers.

我希望 IPython 笔记本代码单元格的默认显示包含行号。

I learned from Showing line numbers in IPython/Jupyter Notebooksthat I can toggle this with ctrl-M L, which is great, but manual. In order to include line numbers by default, I would need to add something to my ipython_notebook_config.py file. Unless I've missed something, there is not an explanation of how to do this in the documentation.

我从Showing line numbers in IPython/Jupyter Notebooks 中了解到,我可以使用 ctrl- ML来切换它,这很棒,但是是手动的。为了默认包含行号,我需要在 ipython_notebook_config.py 文件中添加一些内容。除非我遗漏了什么,否则文档中没有解释如何做到这一点。

采纳答案by William Denman

In your custom.jsfile (location depends on your OS) put

在您的custom.js文件(位置取决于您的操作系统)中放置

IPython.Cell.options_default.cm_config.lineNumbers = true;

IPython.Cell.options_default.cm_config.lineNumbers = true;

If you can't find custom.js, you can just search for it, but generally it will be in your profile_default folder. If it doesn't exist, create the file at $(ipython locate profile)/static/custom/custom.js

如果你找不到 custom.js,你可以搜索它,但通常它会在你的 profile_default 文件夹中。如果它不存在,请在以下位置创建文件$(ipython locate profile)/static/custom/custom.js

If for whatever reason that doesn't work, you can always edit the custom.jsfile in the site-packages/IPython/html/static/custom/in the same way.

如果由于某种原因不起作用,您始终可以以相同的方式编辑custom.js文件site-packages/IPython/html/static/custom/

回答by Mindstormer619

(For Jupyter 4+)In the latest Jupyter versions, they have documentedthe place to make config changes. So basically, in the Jupyter update, they've removed the concept of profiles, so the custom.jsfile location is now .jupyter/custom/custom.js, depending on where your .jupyterfolder is. So if you don't have a customfolder or the custom.jsfile, just create them, then put these lines into the newly created file:

(对于 Jupyter 4+)在最新的 Jupyter 版本中,他们记录了进行配置更改的位置。所以基本上,在 Jupyter 更新中,他们删除了配置文件的概念,因此custom.js文件位置现在是.jupyter/custom/custom.js,具体取决于您的.jupyter文件夹所在的位置。因此,如果您没有custom文件夹或custom.js文件,只需创建它们,然后将这些行放入新创建的文件中:

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

The above is for setting line numbers to all your cell typesat the same time. Code, Markdown and Raw cells will all get line numbers if you do this. If you want line numbers only for code cells, there is a simpler approach. Select a code cell, open the Chrome/Firefox JavaScript console, type the following lines:

以上用于同时为所有单元格类型设置行号。如果你这样做,代码、降价和原始单元格都会得到行号。如果您只需要代码单元格的行号,则有一种更简单的方法。选择一个代码单元,打开 Chrome/Firefox JavaScript 控制台,输入以下几行:

var cell = Jupyter.notebook.get_selected_cell();
var config = cell.config;
var patch = {
    CodeCell:{
        cm_config:{lineNumbers:true}
    }
}
config.update(patch)

Then reload the page. These changes persist because Jupyter will create a json config file in .jupyter/nbconfigto store them. This method is from this pageof the documentation, so read the docs for more config changes that you can make.

然后重新加载页面。这些更改会持续存在,因为 Jupyter 将创建一个 json 配置文件.jupyter/nbconfig来存储它们。此方法来自文档的此页面,因此请阅读文档以了解您可以进行的更多配置更改。



(old answer)

(旧答案)

In the latest version of IPython Notebook (v3.1.0), go to ~/.ipython/<profile_name>/static/custom/custom.jsand add these lines:

在最新版本的 IPython Notebook (v3.1.0) 中,转到~/.ipython/<profile_name>/static/custom/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;
            }
        );
    }
);

The IPython.Cell.options_default.cm_config.lineNumbers = true;line alone will not work as it needs to load the IPython.Cell object before it tries this. Adding this line alone will cause an undefined error in the console. You need to encase it in the event handler as shown.

IPython.Cell.options_default.cm_config.lineNumbers = true;行,因为它需要它试图在此之前加载IPython.Cell对象还不行。单独添加这一行会导致控制台中出现未定义的错误。您需要将其封装在事件处理程序中,如图所示。

@William-Denman's code might have worked for an earlier version, but now you will need to do this.

@William-Denman 的代码可能适用于早期版本,但现在您需要这样做。

EDIT:The line of code right in the middle has to be changed to require("notebook/js/cell").Cell.options_default.cm_config.lineNumbers = true;for the latest version of IPython/Jupyter (IPython 4.0.0, Jupyter 4.0.6). The old IPython.Cellobject will also work, but your web console will throw a deprecation warning, so you can expect the old line to not be supported in future versions.

编辑:中间的代码行必须更改require("notebook/js/cell").Cell.options_default.cm_config.lineNumbers = true;为最新版本的 IPython/Jupyter ( IPython 4.0.0, Jupyter 4.0.6)。旧IPython.Cell对象也可以使用,但您的 Web 控制台会发出弃用警告,因此您可以预期旧版本在未来版本中不受支持。

Also, in the latest IPython/Jupyter, which I'm running using the WinPython portable, I couldn't find the custom.jsfile within the profile folder. I found it (after muchsearching) in WinPython-64bit-2.7.10.3\python-2.7.10.amd64\Lib\site-packages\notebook\static\custom. I don't know if this is a WinPython thing or a Jupyter thing. If someone has Jupyter (latest version) installed normally (using pip or whatever) and can still find the custom.jsfile in the profile folder, please comment.

此外,在最新的 IPython/Jupyter 中,我使用WinPython 便携版运行,我custom.js在配置文件文件夹中找不到该文件。我发现它(后在搜索)WinPython-64bit-2.7.10.3\python-2.7.10.amd64\Lib\site-packages\notebook\static\custom。我不知道这是 WinPython 的东西还是 Jupyter 的东西。如果有人正常安装了 Jupyter(最新版本)(使用 pip 或其他方式)并且仍然可以custom.js在配置文件文件夹中找到该文件,请发表评论。

回答by Stephen Cowley

Above didn't work for me in 2018

以上在 2018 年对我不起作用

I found that inside ~/.jupyter/nbconfig/notebook.jsonI needed to add to add the following lines:

我发现在里面~/.jupyter/nbconfig/notebook.json我需要添加添加以下几行:

"CodeCell": {
  "cm_config": {
  "lineNumbers": true
}

inside the object that was there. So the final object would look like:

在那里的对象内部。所以最终的对象看起来像:

{
  "CodeCell": {
    "cm_config": {
      "lineNumbers": true
    }
  }
}