【问题标题】:How can I configure indentation size in my Jupyter notebooks?如何在 Jupyter 笔记本中配置缩进大小?
【发布时间】:2016-11-25 08:10:44
【问题描述】:

我希望 Jupyter 笔记本中的默认缩进大小为 2 个空格,而不是 4 个空格。我该怎么做?

注意:这不是 How do I change the autoindent to 2 space in IPython notebook 的重复,因为该问题适用于(已弃用的)IPython 笔记本,而不是(当前的)Jupyter 笔记本。

【问题讨论】:

    标签: jupyter-notebook jupyter


    【解决方案1】:

    这样做的正确方法是 bpirvu 对How do I change the autoindent to 2 space in IPython notebook 的隐藏回答:

    只需编辑~/.jupyter/nbconfig/notebook.json。这是一个完整的notebook.json,其中包括相关设置:

    {
      "CodeCell": {
        "cm_config": {
          "indentUnit": 2
        }
      }
    }
    

    还有一个更 hacky 的解决方案在网络上更流行,可能是因为 Jupyter 文档缺乏这个主题,indentUnit 在这里只提到:http://jupyter-notebook.readthedocs.io/en/latest/frontend_config.html。这个解决方法是打开浏览器的JavaScript控制台,输入

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

    最终会为您编辑~/.jupyter/nbconfig/notebook.json

    【讨论】:

    • 尝试第二个控制台解决方案对我产生了以下错误:Uncaught TypeError: config.update is not a function; at <anonymous>:8:8;。嗯。
    • 这不会改变自动缩进长度。例如,如果我在输入 for i in range(10) : 后按回车键,我会得到 4 个空格而不是 2 个。
    猜你喜欢
    • 2021-06-26
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 2020-05-17
    相关资源
    最近更新 更多