Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format on save for a notebook #120432

Closed
rchiodo opened this issue Apr 2, 2021 · 10 comments
Closed

Format on save for a notebook #120432

rchiodo opened this issue Apr 2, 2021 · 10 comments
Assignees
Labels
api feature-request Request for new features or functionality formatting Source formatter issues notebook on-testplan
Milestone

Comments

@rchiodo
Copy link
Contributor

rchiodo commented Apr 2, 2021

Formatting with 'Format Cell' and 'Format Notebook' works but saving a notebook does not apply the formatters to each cell like a normal file would.

Example notebook cell that would be formatted if it was python:

\





print("hello, world")

# output


print("hello, world")
@vscodebot
Copy link

vscodebot bot commented Apr 2, 2021

(Experimental duplicate detection)
Thanks for submitting this issue. Please also check if it is already covered by an existing one, like:

@rebornix
Copy link
Member

rebornix commented Apr 2, 2021

I'm wondering if it's something we can benefit from the file/workingCopy service.

@jucor
Copy link

jucor commented Oct 9, 2021

If that makes it easier, black now supports formatting .ipynb files natively, like it supports .py files.

@NickCrews
Copy link

I'd also love format on save for notebooks. As mentioned above, I can use black **/*.ipynb and it works great (you need to install black with the jupyter extra, eg as python -m pip install black[jupyter]), but I'm now just banging my head against the wall trying to get this to run automatically...

@alanwilter

This comment was marked as spam.

@waydegg

This comment was marked as spam.

@janosh
Copy link

janosh commented May 22, 2022

@NickCrews There is "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 call to black path/to/file.ipynb on save events.

Sadly even that doesn't work atm since VS Code does not yet expose lifecycle events for notebooks. The issues to upvote for that are

If both get implemented, you should be able to add this to your settings to auto-format Jupyter notebooks:

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

@jrieken
Copy link
Member

jrieken commented Dec 6, 2022

same/related to #130799

@jrieken jrieken added the api label Dec 6, 2022
@rebornix rebornix self-assigned this Mar 7, 2023
@rebornix
Copy link
Member

This can now be turned on by setting "notebook.formatOnSave.enabled": true.

@github-actions github-actions bot locked and limited conversation to collaborators May 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api feature-request Request for new features or functionality formatting Source formatter issues notebook on-testplan
Projects
None yet
Development

No branches or pull requests

9 participants