question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Format on save for a notebook

See original GitHub issue

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")

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:67
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
NickCrewscommented, Oct 16, 2021

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…

3reactions
janoshcommented, 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}"
    }
  ]
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Format a Jupyter notebook on save in VSCode - Stack Overflow
This answer shows how to right click and format a cell or a whole notebook from the right click context menu, or a...
Read more >
Save a workbook to text format (.txt or .csv) - Microsoft Support
Save a workbook to text format (.txt or .csv) · Open the workbook you want to save. · Click File > Save As....
Read more >
Code formatting in Jupyter cells. TL;DR - Medium
Code formatting in Jupyter cells. TL;DR: I show you how to get simply pretty code in Jupyter notebooks AND Jupyter Lab.
Read more >
3.2 Notebook | R Markdown: The Definitive Guide - Bookdown
This preview is generated automatically whenever you save the notebook (whether ... R Notebooks, the notebook file format itself is decoupled from RStudio....
Read more >
The Best Format to Save Pandas Data | by Ilia Zaitsev
When working on data analytical projects, I usually use Jupyter notebooks and a great pandas library to process and move my data around....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found