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

Feature request: Support for formatting the python code in jupyter notebooks #298

Closed
MrinalJain17 opened this issue Jun 4, 2018 · 12 comments
Labels
C: jupyter Jupyter Notebooks, any color you like

Comments

@MrinalJain17
Copy link

Jupyter notebooks are quite popular and are used a lot (especially in tasks related to data analysis, visualization etc.).
These notebooks are also used as presentations and tutorials.
It would be great if black could format the code within the cells of the notebooks.

@jgirardet
Copy link
Contributor

@MrinalJain17
Copy link
Author

This is great.. Thanks!

@cdeil
Copy link

cdeil commented Aug 30, 2018

Is there something like

$ black-notebook analysis.ipynb

that runs black on all code cells in the notebook(s) from the command line and saves them back in-place?

I guess it's just ~ 10 lines using the Jupyter API, but I'd still prefer using it over writing it if someone has written that already.

@srstevenson
Copy link
Contributor

@cdeil and others interested in being able to format notebooks from the command line: mcflugen/nbblack is a fairly new project that looks to offer that.

@KwatMDPhD
Copy link

https://github.com/KwatME/clean_ipynb will do.

@MarvinT
Copy link

MarvinT commented Mar 27, 2019

It's pretty easy to configure the code-prettify Nbextension to use Black. Heres the configuration I use and some install instructions.

https://gist.github.com/MarvinT/a072aa992e977496974aaf492287b08c
and
https://marvint.github.io/Black-Jupyter/

@MarcoGorelli
Copy link
Contributor

MarcoGorelli commented Oct 13, 2020

Is there something like

$ black-notebook analysis.ipynb

that runs black on all code cells in the notebook(s) from the command line and saves them back in-place?

I guess it's just ~ 10 lines using the Jupyter API, but I'd still prefer using it over writing it if someone has written that already.

@cdeil you can do this with nbQA https://github.com/nbQA-dev/nbQA

e.g. from the command line:

nbqa black notebook.ipynb --nbqa-mutate

e.g. as pre-commit hook:

repos:
- repo: https://github.com/nbQA-dev/nbQA
  rev: 0.13.1
  hooks:
  - id: nbqa-black
    args: [--nbqa-mutate]

Disclaimer: I'm the author of nbQA


As of 21.8b0, black itself now does this!

@KwatMDPhD
Copy link

Try this https://github.com/KwatME/clean_ipynb

@SamuelMarks
Copy link

It's now builtin, just pip install black[jupyter]

@KwatMDPhD
Copy link

For julia https://github.com/KwatMDPhD/Clean.jl

@cdeil
Copy link

cdeil commented Dec 9, 2021

Thank you very much!

It wasn't obvious to me at first from the README what this does.

It looks like what you implemented is the command line part so that black spam.ipynb formats the notebook.

The other workflow of doing the formatting from within the Jupyter IDE isn't built-in yet in black or jupyter lab yet, for that some third-party plugin should be used, right?

I see pre-commit support was also added:
https://black.readthedocs.io/en/stable/integrations/source_version_control.html

Nice!

@MarcoGorelli
Copy link
Contributor

The other workflow of doing the formatting from within the Jupyter IDE isn't built-in yet in black or jupyter lab yet, for that some third-party plugin should be used, right?

Some third-party plugins do exist, but most don't use black's Jupyter API.

One that does, which IMO deserves more attention, is @n8henrie 's https://github.com/n8henrie/jupyter-black

@JelleZijlstra JelleZijlstra added the C: jupyter Jupyter Notebooks, any color you like label Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: jupyter Jupyter Notebooks, any color you like
Projects
None yet
Development

No branches or pull requests

9 participants