Skip to content

Commit

Permalink
Add option to format Jupyter Notebooks in GitHub Action (#3282)
Browse files Browse the repository at this point in the history
To run the formatter on Jupyter Notebooks, Black must be installed
with an extra dependency (`black[jupyter]`). This commit adds an
optional argument to install Black with this dependency when using the
official GitHub Action [1]. To enable the formatter on Jupyter
Notebooks, just add `jupyter: true` as an argument. Feature requested
at [2].

[1]: https://black.readthedocs.io/en/stable/integrations/github_actions.html
[2]: #3280

Signed-off-by: Antonio Ossa Guerra <[email protected]>
  • Loading branch information
aaossa committed Sep 26, 2022
1 parent 1f2ad77 commit 6b42c2b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Multiple contributions by:
- [Andrey](mailto:[email protected])
- [Andy Freeland](mailto:[email protected])
- [Anthony Sottile](mailto:[email protected])
- [Antonio Ossa Guerra](mailto:[email protected])
- [Arjaan Buijk](mailto:[email protected])
- [Arnav Borbornah](mailto:[email protected])
- [Artem Malyshev](mailto:[email protected])
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@

<!-- For example, Docker, GitHub Actions, pre-commit, editors -->

- Update GitHub Action to support formatting of Jupyter Notebook files via a `jupyter`
option (#3282)
- Update GitHub Action to support use of version specifiers (e.g. `<23`) for Black
version (#3265)

Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ inputs:
description: "Source to run Black. Default: '.'"
required: false
default: "."
jupyter:
description:
"Set this option to true to include Jupyter Notebook files. Default: false"
required: false
default: false
black_args:
description: "[DEPRECATED] Black input arguments."
required: false
Expand All @@ -38,6 +43,7 @@ runs:
# TODO: Remove once https://github.com/actions/runner/issues/665 is fixed.
INPUT_OPTIONS: ${{ inputs.options }}
INPUT_SRC: ${{ inputs.src }}
INPUT_JUPYTER: ${{ inputs.jupyter }}
INPUT_BLACK_ARGS: ${{ inputs.black_args }}
INPUT_VERSION: ${{ inputs.version }}
pythonioencoding: utf-8
Expand Down
7 changes: 6 additions & 1 deletion action/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
ENV_BIN = ENV_PATH / ("Scripts" if sys.platform == "win32" else "bin")
OPTIONS = os.getenv("INPUT_OPTIONS", default="")
SRC = os.getenv("INPUT_SRC", default="")
JUPYTER = os.getenv("INPUT_JUPYTER") == "true"
BLACK_ARGS = os.getenv("INPUT_BLACK_ARGS", default="")
VERSION = os.getenv("INPUT_VERSION", default="")

Expand All @@ -17,7 +18,11 @@
version_specifier = VERSION
if VERSION and VERSION[0] in "0123456789":
version_specifier = f"=={VERSION}"
req = f"black[colorama]{version_specifier}"
if JUPYTER:
extra_deps = "[colorama,jupyter]"
else:
extra_deps = "[colorama]"
req = f"black{extra_deps}{version_specifier}"
pip_proc = run(
[str(ENV_BIN / "python"), "-m", "pip", "install", req],
stdout=PIPE,
Expand Down
5 changes: 5 additions & 0 deletions docs/integrations/github_actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ or just the version number if you want an exact version. The action defaults to
latest release available on PyPI. Only versions available from PyPI are supported, so no
commit SHAs or branch names.

If you want to include Jupyter Notebooks, _Black_ must be installed with the `jupyter`
extra. Installing the extra and including Jupyter Notebook files can be configured via
`jupyter` (default is `false`).

You can also configure the arguments passed to _Black_ via `options` (defaults to
`'--check --diff'`) and `src` (default is `'.'`)

Expand All @@ -49,6 +53,7 @@ Here's an example configuration:
with:
options: "--check --verbose"
src: "./src"
jupyter: true
version: "21.5b1"
```

Expand Down

0 comments on commit 6b42c2b

Please sign in to comment.