1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Qiita100万記事感謝祭!記事投稿キャンペーン開催のお知らせ

VSCodeのJupyter上でコードセルをフォーマットする

Last updated at Posted at 2025-01-11

VSCodeのJupyter上ではeditor.formatOnSaveを指定しても保存時にフォーマットが実行されません.そういうものだと思っていましたが,専用の設定を追加すればフォーマットできるみたいです

検証用リポジトリ

settings.json

settings.jsonnotebook.formatOnSavenotebook.codeActionsOnSaveを追加します.これでコード保存時にフォーマットが走るはずです

settings.json
{
    "files.insertFinalNewline": true,
    "editor.tabSize": 4,
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports.ruff": "explicit"
    },
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "[python]": {
        "editor.defaultFormatter": "charliermarsh.ruff"
    },
    "[toml]": {
        "editor.defaultFormatter": "tamasfe.even-better-toml"
    },
    "notebook.formatOnSave.enabled": true,
    "notebook.codeActionsOnSave": {
        "source.organizeImports.ruff": "explicit"
    }
}

フォーマッタには,セルの言語ごとにeditor.defaultFormatterで指定したものが使われます.つまり,markdownセルはprettier,pythonセルはruffでフォーマットみたいなことができます

notebook.defaultFormatternotebook.insertFinalNewlineのようなオプションもあるみたいです.必要に応じて使えそうですね

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?