【发布时间】:2016-11-25 08:10:44
【问题描述】:
我希望 Jupyter 笔记本中的默认缩进大小为 2 个空格,而不是 4 个空格。我该怎么做?
注意:这不是 How do I change the autoindent to 2 space in IPython notebook 的重复,因为该问题适用于(已弃用的)IPython 笔记本,而不是(当前的)Jupyter 笔记本。
【问题讨论】:
我希望 Jupyter 笔记本中的默认缩进大小为 2 个空格,而不是 4 个空格。我该怎么做?
注意:这不是 How do I change the autoindent to 2 space in IPython notebook 的重复,因为该问题适用于(已弃用的)IPython 笔记本,而不是(当前的)Jupyter 笔记本。
【问题讨论】:
这样做的正确方法是 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 个。