It took me a little while, but I finally figured out the use of notebook.source.fixAll and notebook.source.organizeImports settings (instead of their source.* counterparts) to prevent vscode from turning my import cells in jupyter notebooks into gibberish when using ruff as the formatter. However, it appears that when a notebook starts with a markdown cell (say, to title the notebook) and then has a python cell with imports after the markdown, notebook.source.organizeImports doesn't work. Here are my relevant vscode settings:
{
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit",
"source.fixAll": "explicit"
},
"notebook.defaultFormatter": "charliermarsh.ruff",
"notebook.formatOnSave.enabled": true,
"notebook.codeActionsOnSave": {
"notebook.source.organizeImports": "explicit",
"notebook.source.fixAll": "explicit"
}
}
And here's the relevant part of my pyproject.toml file in the workspace open in vscode:
[tool.ruff.lint]
unfixable = ["F401"]
select = ["E4", "E7", "E9", "F", "NPY", "I"]
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["E402"]
[tool.ruff.format]
docstring-code-format = true
I'm using ruff 0.6.2.
Side Note:
If I use source.organizeImports instead of notebook.source.organizeImports, it "works" in the sense that something happens, but the effect is to introduce gibberish. Here's an example of the "gibberish" that occurs when using source.* instead of notebook.source.*. The first screenshot is from "pre-save", the second is after saving once, and the third after saving a second time



It took me a little while, but I finally figured out the use of
notebook.source.fixAllandnotebook.source.organizeImportssettings (instead of theirsource.*counterparts) to prevent vscode from turning my import cells in jupyter notebooks into gibberish when using ruff as the formatter. However, it appears that when a notebook starts with a markdown cell (say, to title the notebook) and then has a python cell with imports after the markdown,notebook.source.organizeImportsdoesn't work. Here are my relevant vscode settings:{ "editor.defaultFormatter": "charliermarsh.ruff", "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.organizeImports": "explicit", "source.fixAll": "explicit" }, "notebook.defaultFormatter": "charliermarsh.ruff", "notebook.formatOnSave.enabled": true, "notebook.codeActionsOnSave": { "notebook.source.organizeImports": "explicit", "notebook.source.fixAll": "explicit" } }And here's the relevant part of my
pyproject.tomlfile in the workspace open in vscode:I'm using ruff 0.6.2.
Side Note:



If I use
source.organizeImportsinstead ofnotebook.source.organizeImports, it "works" in the sense that something happens, but the effect is to introduce gibberish. Here's an example of the "gibberish" that occurs when usingsource.*instead ofnotebook.source.*. The first screenshot is from "pre-save", the second is after saving once, and the third after saving a second time