自动刷新当前单元格

2024-09-30 08:33:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经按照这里的建议将此函数添加到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] Saving file at /Untitled.ipynb

Stripping WS

但我有一个问题:

该功能是否仅在保存笔记本时运行

我想是的,因为我必须刷新当前选项卡才能看到多余的空间被删除。在执行stripWS函数后,有没有办法自动刷新当前单元格


Tags: 函数modelifwssavetypecelljupyter

热门问题