microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
164.86k stars 29.51k forks source link

Language specific settings for ipynb (jupyter-notebook) files [feature-request]. #195011

Open MarkAkritov opened 1 year ago

MarkAkritov commented 1 year ago

Currently there is no language specific selector to modify editor settings for .ipynb files in VSCode. I could not find any similar issue or feature request. Specifically, with last update (Version: 1.83.0) Jupyter notebooks support files.trimTrailingWhitespace, files.trimFinalNewlines, files.insertFinalNewline settings and I am trying to make editor insert new line to all files except .ipynbs. Currently the editor inserts a new line at the end of each cell.

As an example, I can do same for Python in the following way:

// settings.json
  "files.trimFinalNewlines": true,
  "files.trimTrailingWhitespace": true,
  "files.insertFinalNewline": true,

  "[python]": {
    "files.insertFinalNewline": false
  }

What I ask and suggest is to a have an option to modify these kind of settings using language selector like [ipynb] or [jupyter-notebook].

  "[ipynb]": {
    "editor.lineHeight": 26,
    "files.insertFinalNewline": false,
   // etc.
  }
jryans commented 1 year ago

I have the same request as well... I was quite surprised to see 1.83 add new lines to all notebook cells, as I don't consider notebooks cells the same as "files" in this respect.

It would be great to have some way to separately tune at least these whitespace and newline settings for notebooks, whether that's via language-specific settings or something other means.

vscodenpa commented 1 year ago

This feature request is now a candidate for our backlog. The community has 60 days to upvote the issue. If it receives 20 upvotes we will move it to our backlog. If not, we will close it. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

fwcd commented 1 year ago

This was actually the first thing I looked for after stumbling on #195223. While the trim methods make sense for notebook cells, I don't think inserting final newlines in every cell is particularly useful.

jdanbrown commented 1 year ago

Strong agree, inserting a final newline into every notebook cell is very disruptive to notebook DX.

Yoyokrazy commented 1 year ago

Hey everyone, thanks for all of the input. Right now, the team needs to put a bit more thought into how best to address this, as the override would need to be based on the editor type (notebook editor in this case) rather than a language. This isn't something that has been implemented before, so will need some planning and forethought before executing on this.

For now, although it isn't optimal, the best solution may be to leverage the Profiles feature. This would allow you to set up a specific "notebook" profile tailored with your desired settings for editing files that utilize the notebook editor (*.ipynb, *.ghinb, etc).

MarkAkritov commented 1 year ago

For now, although it isn't optimal, the best solution may be to leverage the Profiles feature. This would allow you to set up a specific "notebook" profile tailored with your desired settings for editing files that utilize the notebook editor (*.ipynb, *.ghinb, etc).

Thanks for suggestion, however there are no projects on my side that use only notebooks, and no python files. So creating profile will not work, I guess.

mjs271 commented 1 year ago

+1!

I ended up here after thinking I was losing my mind with the notebook cell newlines and trying to remedy it. I'd love to see jupyter-notebook-specific language settings... or just default to disabling the auto-newline in every cell 🙃 (#195223)

RafalSkolasinski commented 1 year ago

Hope this can be resolved or workaround provided. I had to downgrade to 1.82 for now due to https://github.com/microsoft/vscode/issues/195223

blakeNaccarato commented 1 year ago

Here's a workaround for the final newlines problem, for now. This involves disabling format on save for notebooks via keybind because disabling notebook.formatOnSave in settings.json doesn't work. In this workaround we also offload formatting of notebooks to a VSCode task that pipes through nb-clean and ruff (fix then format), then back to the file. This workflow does confuse Pylance, causing it to persist a bunch of non-existent pylance-notebook-cell temp files in the Problems pane. You have to filter your problems pane with !**/*==.py to work around that.

Note the task is written for pwsh, but you can modify it for bash just as well. By the way, we need Set-Content in the pwsh version instead of > because reasons. Also, once the Ruff VSCode extension natively supports notebook formatting, this workaround will become less attractive.

settings.json

  //? Notebook cells gain new lines on save, language settings can't stop it, nor
  //? does disabling `notebook.formatOnSave`
  //? https://github.com/microsoft/vscode/issues/195011#issuecomment-1797050133
  "editor.formatOnSave": true,
  "files.insertFinalNewline": true,

keybindings.json

  //* Save without formatting w/ Ctrl+S for notebooks.
  //? Notebook cells gain new lines on save, language settings can't stop it, nor
  //? does disabling `notebook.formatOnSave`
  //? https://github.com/microsoft/vscode/issues/195011#issuecomment-1797050133
  {
    "key": "ctrl+s",
    "command": "workbench.action.files.saveWithoutFormatting",
    "when": "notebookEditorFocused"
  },
  {
    "key": "ctrl+s",
    "command": "workbench.action.files.save",
    "when": "!notebookEditorFocused"
  },
  {
    "key": "ctrl+s",
    "command": "-workbench.action.files.save"
  },

  //* Format notebooks externally
  {
    "key": "ctrl+k ctrl+v",
    "command": "workbench.action.tasks.runTask",
    "args": "user: format notebook"
  },

User tasks.json (via Tasks: Open User Tasks)

    //? Notebook cells gain new lines on save, language settings can't stop it, nor
    //? does disabling `notebook.formatOnSave`
    //? https://github.com/microsoft/vscode/issues/195011#issuecomment-1797050133
    {
      "label": "user: format notebook",
      "type": "shell",
      "options": { "shell": { "executable": "pwsh", "args": ["-Command"] } },
      "command": "Get-Content ${file} | nb-clean clean --remove-empty-cells --preserve-cell-outputs --preserve-cell-metadata tags | ruff --fix-only --stdin-filename ${file} | ruff format --stdin-filename ${file} | Set-Content ${file}",
      "icon": { "id": "notebook" },
      "problemMatcher": []
    },
vscodenpa commented 1 year ago

:slightly_smiling_face: This feature request received a sufficient number of community upvotes and we moved it to our backlog. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

Yoyokrazy commented 1 year ago

Appreciate the input from folks. Currently working to push a quick change to separate the final newline behavior into a notebook specific setting until a solution or path forward for notebook specific settings is decided upon. I will update both this and the related issue with the setting name when things are merged.

Yoyokrazy commented 1 year ago

This setting is now unreleased, so upon the next insider build you will be able to add the following to your settings json:

"notebook.insertFinalNewline": false

RafalSkolasinski commented 1 year ago

"notebook.insertFinalNewline": false - will this apply per-cell? will "files.insertFinalNewline": false be ignored?

Yoyokrazy commented 1 year ago

Yes, this will apply per-cell. Upon a save or autosave the notebook will attempt to add a final newline to any code cell not already ending with one.

files.insertFinalNewline is now not relevant to the notebook editor and will work as it previously did in the standard editor.

Example settings.json:

{
    "notebook.insertFinalNewline": false,
    "files.insertFinalNewline": true
}
MarkAkritov commented 1 year ago

Yes, this will apply per-cell. Upon a save or autosave the notebook will attempt to add a final newline to any code cell not already ending with one.

files.insertFinalNewline is now not relevant to the notebook editor and will work as it previously did in the standard editor.

Example settings.json:

{
    "notebook.insertFinalNewline": false,
    "files.insertFinalNewline": true
}
  • In this situation, saving a notebook (either autosave or explicit save) will NOT add a final newline to cells when in the notebook editor. Due to "notebook.insertFinalNewline": false
  • All standard editor files will have a final newline added when saved (if necessary), due to "files.insertFinalNewline": true

Thanks for quick solution.

starball5 commented 6 months ago

Related on Stack Overflow: Notebook-specific settings in VS Code

nonoash commented 4 months ago

the fix is specifically for newlines but we still can't add custom settings for notebooks, for example if i want to have a different line length for .py and .ipynb files

amarcin commented 1 month ago

Agreed, looking for the original ask, a [ipynb] option.

  "[ipynb]": {
    "editor.lineHeight": 26,
    "files.insertFinalNewline": false,
   // etc.
  }