Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce030cc

Browse files
toroleapincpre-commit-ci[bot]cobaltt7veeceeyint-arsh
authored
docs: add dedicated Jupyter Notebooks guide (psf#5009)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: cobalt <[email protected]> Co-authored-by: Varun Chawla <[email protected]> Co-authored-by: Akash <[email protected]> Co-authored-by: edvatar <[email protected]>
1 parent 148efe4 commit ce030cc

10 files changed

Lines changed: 221 additions & 69 deletions

File tree

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ repos:
88
name: Check pre-commit rev in example
99
language: python
1010
entry: python -m scripts.check_pre_commit_rev_in_example
11-
files: '(CHANGES\.md|source_version_control\.md)$'
11+
files: '(CHANGES|source_version_control|using_black_with_jupyter_notebooks)\.md$'
1212
additional_dependencies:
1313
- beautifulsoup4>=4.14.2
1414
- commonmark>=0.9.1
@@ -18,7 +18,7 @@ repos:
1818
name: Check black version in the basics example
1919
language: python
2020
entry: python -m scripts.check_version_in_basics_example
21-
files: '(CHANGES\.md|the_basics\.md)$'
21+
files: '(CHANGES|the_basics)\.md$'
2222
additional_dependencies:
2323
- beautifulsoup4>=4.14.2
2424
- commonmark>=0.9.1

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
- Expand preview style documentation with detailed examples for `wrap_comprehension_in`,
7171
`simplify_power_operator_hugging`, and `wrap_long_dict_values_in_parens` features
7272
(#4987)
73+
- Add detailed documentation for formatting Jupyter Notebooks (#5009)
7374

7475
## 26.1.0
7576

docs/faq.md

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,14 @@ _Black_ is timid about formatting Jupyter Notebooks. Cells containing any of the
5858
following will not be formatted:
5959

6060
- automagics (e.g. `pip install black`)
61-
- non-Python cell magics (e.g. `%%writefile`). These can be added with the flag
62-
`--python-cell-magics`, e.g. `black --python-cell-magics writefile hello.ipynb`.
63-
- multiline magics, e.g.:
61+
- non-Python cell magics (e.g. `%%writefile`)
62+
- multiline magics
63+
- code which `IPython`'s `TransformerManager` would transform magics into
64+
- invalid syntax
6465

65-
```python
66-
%timeit f(1, \
67-
2, \
68-
3)
69-
```
70-
71-
- code which `IPython`'s `TransformerManager` would transform magics into, e.g.:
72-
73-
```python
74-
get_ipython().system('ls')
75-
```
76-
77-
- invalid syntax, as it can't be safely distinguished from automagics in the absence of
78-
a running `IPython` kernel.
66+
See
67+
[Using _Black_ with Jupyter Notebooks](guides/using_black_with_jupyter_notebooks.md#cells-that-black-will-skip)
68+
for details.
7969

8070
## Why does Flake8 report warnings?
8171

docs/getting_started.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ Also, you can try out _Black_ online for minimal fuss on the
1717
## Installation
1818

1919
_Black_ can be installed by running `pip install black`. It requires Python 3.10+ to
20-
run. If you want to format Jupyter Notebooks, install with
21-
`pip install "black[jupyter]"`.
20+
run.
2221

2322
If you use pipx, you can install Black with `pipx install black`.
2423

24+
If you want to format Jupyter Notebooks, install with `pip install "black[jupyter]"`.
25+
See the [Jupyter Notebooks guide](./guides/using_black_with_jupyter_notebooks.md) for
26+
more details.
27+
2528
If you can't wait for the latest _hotness_ and want to install from GitHub, use:
2629

2730
`pip install git+https://github.com/psf/black`

docs/guides/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ hidden:
77
88
introducing_black_to_your_project
99
using_black_with_other_tools
10+
using_black_with_jupyter_notebooks
1011
```
1112

1213
Wondering how to do something specific? You've found the right place! Listed below are
1314
topic specific guides available:
1415

1516
- {doc}`introducing_black_to_your_project`
1617
- {doc}`using_black_with_other_tools`
18+
- {doc}`using_black_with_jupyter_notebooks`
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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.1.0
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.

docs/integrations/github_actions.md

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,59 +32,74 @@ We recommend the use of the `@stable` tag, but per version tags also exist if yo
3232
that. Note that the action's version you select is independent of the version of _Black_
3333
the action will use.
3434

35-
The version of _Black_ the action will use can be configured via `version` or read from
36-
the `pyproject.toml` file. `version` can be any
37-
[valid version specifier](https://packaging.python.org/en/latest/glossary/#term-Version-Specifier)
38-
or just the version number if you want an exact version. To read the version from the
39-
`pyproject.toml` file instead, set `use_pyproject` to `true`. This will first look into
40-
the `tool.black.required-version` field, then the `dependency-groups` table, then the
41-
`project.dependencies` array and finally the `project.optional-dependencies` table. The
42-
action defaults to the latest release available on PyPI. Only versions available from
43-
PyPI are supported, so no commit SHAs or branch names.
35+
### Versions
4436

45-
If you want to include Jupyter Notebooks, _Black_ must be installed with the `jupyter`
46-
extra. Installing the extra and including Jupyter Notebook files can be configured via
47-
`jupyter` (default is `false`).
37+
The version of _Black_ the action will use can be configured via `version` or read from
38+
the `pyproject.toml` file. The action defaults to the latest release available on PyPI.
4839

49-
You can also configure the arguments passed to _Black_ via `options` (defaults to
50-
`'--check --diff'`) and `src` (default is `'.'`). Please note that the
51-
[`--check` flag](labels/exit-code) is required so that the workflow fails if _Black_
52-
finds files that need to be formatted.
40+
`version` can be any
41+
[valid version specifier](https://packaging.python.org/en/latest/glossary/#term-Version-Specifier)
42+
or just the version number if you want an exact version.
5343

54-
Here's an example configuration:
44+
If you want to match versions covered by Black's
45+
[stability policy](labels/stability-policy), you can use the compatible release operator
46+
(`~=`):
5547

5648
```yaml
5749
- uses: psf/black@stable
5850
with:
5951
options: "--check --verbose"
6052
src: "./src"
61-
jupyter: true
62-
version: "21.5b1"
53+
version: "~= 22.0"
6354
```
6455

65-
If you want to match versions covered by Black's
66-
[stability policy](labels/stability-policy), you can use the compatible release operator
67-
(`~=`):
56+
To read the version from the `pyproject.toml` file instead, set `use_pyproject` to
57+
`true`. This will first look into the `tool.black.required-version` field, then the
58+
`dependency-groups` table, then the `project.dependencies` array and finally the
59+
`project.optional-dependencies` table. Note that this requires Python >= 3.11, so using
60+
the setup-python action may be required, for example:
6861

6962
```yaml
63+
- uses: actions/setup-python@v5
64+
with:
65+
python-version: "3.13"
7066
- uses: psf/black@stable
7167
with:
7268
options: "--check --verbose"
7369
src: "./src"
74-
version: "~= 22.0"
70+
use_pyproject: true
7571
```
7672

77-
If you want to read the version from `pyproject.toml`, set `use_pyproject` to `true`.
78-
Note that this requires Python >= 3.11, so using the setup-python action may be
79-
required, for example:
73+
Only versions available from PyPI are supported, so no commit SHAs or branch names.
74+
75+
### Jupyter Notebooks
76+
77+
If you want to include Jupyter Notebooks, it can be enabled by setting `jupyter` to
78+
`true` (default is `false`):
8079

8180
```yaml
82-
- uses: actions/setup-python@v5
81+
- uses: psf/black@stable
8382
with:
84-
python-version: "3.13"
83+
jupyter: true
84+
```
85+
86+
See the [Jupyter Notebooks guide](../guides/using_black_with_jupyter_notebooks.md) for
87+
more details.
88+
89+
### CLI Options
90+
91+
You can also configure the arguments passed to _Black_ via `options` (defaults to
92+
`'--check --diff'`) and `src` (default is `'.'`). Please note that the
93+
[`--check` flag](labels/exit-code) is required so that the workflow fails if _Black_
94+
finds files that need to be formatted.
95+
96+
Here's an example configuration:
97+
98+
```yaml
8599
- uses: psf/black@stable
86100
with:
87101
options: "--check --verbose"
88102
src: "./src"
89-
use_pyproject: true
103+
jupyter: true
104+
version: "21.5b1"
90105
```

docs/integrations/source_version_control.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,20 @@ include Jupyter Notebooks. To use this hook, simply replace the hook's `id: blac
3333

3434
```yaml
3535
repos:
36-
# Using this mirror lets us use mypyc-compiled black, which is about 2x faster
3736
- repo: https://github.com/psf/black-pre-commit-mirror
3837
rev: 26.1.0
3938
hooks:
4039
- id: black-jupyter
41-
# It is recommended to specify the latest version of Python
42-
# supported by your project here, or alternatively use
43-
# pre-commit's default_language_version, see
44-
# https://pre-commit.com/#top_level-default_language_version
4540
language_version: python3.11
4641
```
4742

4843
```{note}
4944
The `black-jupyter` hook became available in version 21.8b0.
5045
```
5146

47+
See the [Jupyter Notebooks guide](../guides/using_black_with_jupyter_notebooks.md) for
48+
more details.
49+
5250
## Excluding files with pre-commit
5351

5452
When using pre-commit, there's an important distinction in how file exclusions work.

docs/usage_and_configuration/the_basics.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ useful when piping source on standard input.
123123
#### `--python-cell-magics`
124124

125125
When processing Jupyter Notebooks, add the given magic to the list of known python-
126-
magics. Useful for formatting cells with custom python magics.
126+
magics. Useful for formatting cells with custom python magics. See the
127+
[Jupyter Notebooks guide](../guides/using_black_with_jupyter_notebooks.md) for more
128+
details.
127129

128130
#### `-x, --skip-source-first-line`
129131

@@ -248,8 +250,8 @@ Each range must be specified as two integers connected by a `-`: `<START>-<END>`
248250
`<START>` and `<END>` integer indices are 1-based and inclusive on both ends.
249251

250252
_Black_ may still format lines outside of the ranges for multi-line statements.
251-
Formatting more than one file or any ipynb files with this option is not supported. This
252-
option cannot be specified in the `pyproject.toml` config.
253+
Formatting more than one file or any Jupyter Notebooks with this option is not
254+
supported. This option cannot be specified in the `pyproject.toml` config.
253255

254256
Example: `black --line-ranges=1-10 --line-ranges=21-30 test.py` will format lines from
255257
`1` to `10` and `21` to `30`.

0 commit comments

Comments
 (0)