温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Stop Jupyter auto-indenting new lines a large, non-integer amount of tabs?
python jupyter-notebook

python - 停止Jupyter自动缩进新行中大量的非整数制表符?

发布于 2020-03-27 12:02:06

在OSX上运行Jupyter(核心:4.4.0,笔记本:5.7.8)python3笔记本。

笔记本似乎容易混淆如何自动缩进新行。具体来说,我的更复杂的单元格具有嵌套的defs,ifs和whiles等缩进,导致Jupyter使用与我的任何缩进级别都不匹配的怪异的4.5选项卡自动缩进,并导致大量浪费的按键修复缩进每个。单。新。线。

明确地说,我不是在问如何更改默认制表符间距,而是在问到“返回”以换行时,如何将jupyter自动缩进停止为疯狂。

一种潜在的错误来源是我对f弦的使用。Jupyter有时会搞乱哈希和嵌套引号的语法突出显示。print(f"Output for #{myNum} | {myDict['namedItem']}")

欢迎提供任何可能导致此问题的提示。

查看更多

查看更多

提问者
Mark_Anderson
被浏览
144
Mark_Anderson 2020-01-22 03:31

答:是的,它是f字符串文字。f字符串中的嵌套引号会破坏单元格中的缩进,将新行默认设置为f字符串中引号的开头。

例如。

print(f"Output for #{myNum} | {myDict['namedItem']}")
                                      # Newline starts here
                                      # and here
# Manual de-indent here for readibility.
x=5 # and manually force python code to an indent where it actually runs
                                      # Default newline is still here though  
                                      # And will stay here for the rest of the cell

20171月以来,f字符串通常打破了jupyter笔记本底层的CodeMirror语法荧光笔已经进行了一些较小的修复以使其损坏程度更小,但问题并未得到解决-请参阅最近的Git问题

编辑:自2020年1月jupyter 6.0.2起,通过更新CodeMirror 5.48.4来解决此问题。