我按照这里的建议将这个函数添加到jupyter配置文件中.
https://github.com/jupyter/notebook/issues/1455
def stripWS(t):
return '\n'.join([i.rstrip() for i in t.split('\n')])
def scrub_output_pre_save(model=None, **kwargs):
"""strip trailing space before saving"""
if model['type'] == 'notebook':
# only run on nbformat v4
if model['content']['nbformat'] != 4:
print("skipping WS stripping since `nbformat` != 4")
return
print("Stripping WS")
for cell in model['content']['cells']:
if cell['cell_type'] != 'code':
continue
cell['source'] = stripWS(cell['source'])
elif model['type'] == 'file':
if model['format'] == 'text':
print("Stripping WS")
model['content'] = stripWS(model['content'])
c.ContentsManager.pre_save_hook = scrub_output_pre_save这起作用,并消除额外的空间,如广告。我在命令提示符下收到这条消息:
I 06:45:34.823 NotebookApp保存文件在/Untitled.ipynb
汽提WS
但我有个问题:
这个功能只在保存笔记本的时候运行吗?
我想是这样的,因为我必须刷新当前选项卡,才能看到多余的空间被删除。执行stripWS函数后,有什么方法可以自动刷新当前单元格吗?
发布于 2020-04-21 04:30:12
木星扩展nb_black将移除细胞内任何地方的额外空间。只需在笔记本顶部使用此命令即可。
%load_ext nb_black
在使用之前,您可能需要使用pip安装模块。
https://stackoverflow.com/questions/59083282
复制相似问题