如何在Jupyter笔记本中配置缩进大小?

rd1*_*d11 2 jupyter jupyter-notebook

我希望在我的Jupyter笔记本中有一个默认缩进大小为2个空格而不是4个空格.我怎样才能做到这一点?

注意:这不是重复如何在IPython笔记本中将autoindent更改为2个空格,因为该问题适用于(已弃用)IPython笔记本而非(当前)Jupyter笔记本.

rd1*_*d11 6

这样做的正确方法是bpirvu埋藏的答案如何在IPython笔记本中将autoindent更改为2个空格:

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

{
  "CodeCell": {
    "cm_config": {
      "indentUnit": 2
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

还有一个更为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)
Run Code Online (Sandbox Code Playgroud)

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