| 1 |
# Using _Black_ with Jupyter Notebooks |
| 2 |
|
| 3 |
_Black_ supports formatting Jupyter Notebooks (`.ipynb` files) natively. |
| 4 |
|
| 5 |
## Installation |
| 6 |
|
| 7 |
To format Jupyter Notebooks, install _Black_ with the `jupyter` extra: |
| 8 |
|
| 9 |
```sh |
| 10 |
pip install "black[jupyter]" |
| 11 |
``` |
| 12 |
|
| 13 |
```{note} |
| 14 |
Without the `jupyter` extra, _Black_ will not be able to format `.ipynb` files. |
| 15 |
``` |
| 16 |
|
| 17 |
## Basic usage |
| 18 |
|
| 19 |
Once installed, you can format notebooks the same way you format Python files: |
| 20 |
|
| 21 |
```sh |
| 22 |
black notebook.ipynb |
| 23 |
``` |
| 24 |
|
| 25 |
You can also format entire directories containing a mix of `.py` and `.ipynb` files: |
| 26 |
|
| 27 |
```sh |
| 28 |
black . |
| 29 |
``` |
| 30 |
|
| 31 |
_Black_ will automatically detect Jupyter Notebooks by their `.ipynb` extension and |
| 32 |
format them accordingly. |
| 33 |
|
| 34 |
### Formatting via standard input |
| 35 |
|
| 36 |
If you're piping notebook content on standard input, use the `--ipynb` flag to tell |
| 37 |
_Black_ to treat the input as a Jupyter Notebook: |
| 38 |
|
| 39 |
```sh |
| 40 |
cat notebook.ipynb | black --ipynb - |
| 41 |
``` |
| 42 |
|
| 43 |
## What is (and isn't) formatted |
| 44 |
|
| 45 |
_Black_ formats the Python code cells in your notebook while preserving: |
| 46 |
|
| 47 |
- Markdown cells |
| 48 |
- Cell outputs |
| 49 |
- Cell metadata |
| 50 |
- Notebook metadata |
| 51 |
|
| 52 |
Only the source code within Python code cells is reformatted. |
| 53 |
|
| 54 |
### Cells that _Black_ will skip |
| 55 |
|
| 56 |
_Black_ is cautious about formatting notebook cells. The following cells will **not** be |
| 57 |
formatted: |
| 58 |
|
| 59 |
- **Automagics** — e.g. `pip install black` (without the `%` prefix) |
| 60 |
- **Non-Python cell magics** — e.g.: |
| 61 |
```python |
| 62 |
%%writefile script.py |
| 63 |
print("hello") |
| 64 |
``` |
| 65 |
- **Multiline magics** — e.g.: |
| 66 |
```python |
| 67 |
%timeit f(1, \ |
| 68 |
2, \ |
| 69 |
3) |
| 70 |
``` |
| 71 |
- **IPython internal calls** — code that `IPython`'s `TransformerManager` would |
| 72 |
transform, e.g.: |
| 73 |
```python |
| 74 |
get_ipython().system('ls') |
| 75 |
``` |
| 76 |
- **Invalid syntax** — as it cannot be safely distinguished from automagics without a |
| 77 |
running IPython kernel. |
| 78 |
|
| 79 |
If you notice a cell is not being formatted, it is likely because it contains one of the |
| 80 |
above constructs. |
| 81 |
|
| 82 |
Additionally, _Black_ cannot format Jupyter Notebooks with the `--line-ranges` option. |
| 83 |
|
| 84 |
### Cell magics |
| 85 |
|
| 86 |
_Black_ understands IPython magics, but is conservative about which cells it will |
| 87 |
format. By default, _Black_ recognizes standard IPython magics. |
| 88 |
|
| 89 |
#### Custom python cell magics |
| 90 |
|
| 91 |
If you use custom cell magics that contain Python code, you can tell _Black_ about them |
| 92 |
using the `--python-cell-magics` flag: |
| 93 |
|
| 94 |
```sh |
| 95 |
black --python-cell-magics writefile notebook.ipynb |
| 96 |
``` |
| 97 |
|
| 98 |
This also works in `pyproject.toml`: |
| 99 |
|
| 100 |
```toml |
| 101 |
[tool.black] |
| 102 |
python-cell-magics = ["writefile", "my_custom_magic"] |
| 103 |
``` |
| 104 |
|
| 105 |
## Integrations |
| 106 |
|
| 107 |
### pre-commit |
| 108 |
|
| 109 |
Simply replace the `black` hook with `black-jupyter`. |
| 110 |
|
| 111 |
```yaml |
| 112 |
repos: |
| 113 |
- repo: https://github.com/psf/black-pre-commit-mirror |
| 114 |
rev: 26.5.1 |
| 115 |
hooks: |
| 116 |
- id: black-jupyter |
| 117 |
language_version: python3.11 |
| 118 |
``` |
| 119 |
|
| 120 |
See the [source version control integration](../integrations/source_version_control.md) |
| 121 |
docs for more examples of using Black with pre-commit. |
| 122 |
|
| 123 |
### GitHub Actions |
| 124 |
|
| 125 |
Set the `jupyter` option to `true`. |
| 126 |
|
| 127 |
```yaml |
| 128 |
- uses: psf/black@stable |
| 129 |
with: |
| 130 |
jupyter: true |
| 131 |
``` |
| 132 |
|
| 133 |
See the [GitHub Actions integration](../integrations/source_version_control.md) docs for |
| 134 |
more examples of using Black with GitHub Actions. |