简体   繁体   English

在 VSCode 中保存时格式化 Jupyter notebook

[英]Format a Jupyter notebook on save in VSCode

I use black to automatically format all of my Python code whenever I save in VSCode.每当我在 VSCode 中保存时,我都会使用black自动格式化我所有的 Python 代码。 I'd like the same functionality, but within a Jupyter notebook in VSCode.我想要相同的功能,但在 VSCode 的 Jupyter 笔记本中。

This answer shows how to right click and format a cell or a whole notebook from the right click context menu, or a keyboard shortcut. 此答案显示如何从右键单击上下文菜单或键盘快捷键右键单击并设置单元格或整个笔记本的格式。 Can I make this happen on save instead?我可以在保存时发生这种情况吗?

It looks like there is an issue related to this, but it is over a year old.看起来有一个与此相关的问题,但它已经超过一年了。

Are there any good workarounds?有什么好的解决方法吗? Maybe a way to set the format notebook option to the same keybinding as save?也许是一种将格式笔记本选项设置为与保存相同的键绑定的方法?

UPDATE:更新:

If you like me want this functionality to be added please go to the issue and upvote it, the devs said they will need a bunch of upvotes before it's considered.如果你喜欢我希望添加此功能,请 go 到问题并投票,开发人员说他们需要大量投票才能考虑。

There are no plans yet according to Github .根据Github还没有计划。

I think you can only format it manually.我认为您只能手动格式化。

From janosh's reply on GitHub:来自 janosh 对 GitHub 的回复

There is a setting editor.codeActionsOnSave but it doesn't allow running arbitrary shell commands (for security reasons?) so you'd need to install an extension like Run On Save and get it to call black path/to/file.ipynb on save events.有一个设置editor.codeActionsOnSave但它不允许运行任意 shell 命令(出于安全原因?)所以你需要安装像Run On Save这样的扩展并让它调用black path/to/file.ipynb on保存事件。

Sadly even that doesn't work right now since VS Code does not yet expose lifecycle events for notebooks.可悲的是,由于 VS Code 尚未公开笔记本的生命周期事件,因此即使这样现在也不起作用。 The issue to upvote for that is: Improve workspace API for Notebook lifecycle to support (at least) saving events要对此表示赞同的问题是:改进工作区 API 以支持(至少)保存事件的笔记本生命周期

If both get implemented, you should be able to add this to your settings to auto-format Jupyter notebooks:如果两者都实现了,您应该能够将其添加到您的设置中以自动格式化 Jupyter 笔记本:

"emeraldwalk.runonsave": {
  "commands": [
    {
      "match": "\\.ipynb$",
      "cmd": "black ${file}"
    }
  ]
}

A simple enough solution is to set the format notebook option to the same keybinding as save, as you suggested.一个足够简单的解决方案是按照您的建议将格式笔记本选项设置为与保存相同的键绑定。 Here's how you can do that with VSCode Tasks:以下是如何使用 VSCode 任务执行此操作:

tasks.json (in Command Palette "Tasks: Open User Tasks"): tasks.json(在命令面板“任务:打开用户任务”中):

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cmd:save",
            "command": "${command:workbench.action.files.save}"
        },
        {
            "label": "cmd:format-notebook",
            "command": "${command:notebook.format}"
        },
        {
            "label": "cmd:format-notebook+save",
            "dependsOrder": "sequence",
            "dependsOn": [
                "cmd:format-notebook",
                "cmd:save"
            ]
        }
    ]
}

keybindings.json (in Command Palette "Preferences: Open Keyboard Shortcuts (JSON)"): keybindings.json(在命令面板“首选项:打开键盘快捷方式(JSON)”中):

[
    {
        "key": "ctrl+s",
        "command": "workbench.action.tasks.runTask",
        "args": "cmd:format-notebook+save"
    }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM