From 1e9db8ce52cc87abd75dd5a7158b3825ea812fda Mon Sep 17 00:00:00 2001 From: Antonio Ossa Guerra Date: Fri, 23 Sep 2022 09:47:05 -0300 Subject: [PATCH 1/5] Support formatting Jupyter Notebooks in GH Action 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]: https://github.com/psf/black/issues/3280 Signed-off-by: Antonio Ossa Guerra --- action.yml | 6 ++++++ action/main.py | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index cfa6ef9fb7e..61775ecba64 100644 --- a/action.yml +++ b/action.yml @@ -12,6 +12,11 @@ inputs: description: "Source to run Black. Default: '.'" required: false default: "." + jupyter: + description: + "Set this option to true to include Jupyter Notebooks files. Default: false" + required: false + default: false black_args: description: "[DEPRECATED] Black input arguments." required: false @@ -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 diff --git a/action/main.py b/action/main.py index 03228cb13e8..ff9d4112aed 100644 --- a/action/main.py +++ b/action/main.py @@ -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="") @@ -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, From dd9de60cae07ca0d673a34ec2c11ce112902f491 Mon Sep 17 00:00:00 2001 From: Antonio Ossa Guerra Date: Fri, 23 Sep 2022 10:03:42 -0300 Subject: [PATCH 2/5] Update CHANGES.md Include the new GitHub Action optional argument as a change Signed-off-by: Antonio Ossa Guerra --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 67d007c21ae..922098b24e7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -47,6 +47,7 @@ +- Update GitHub Action to support formatting of Jupyter Notebook files (#3282) - Update GitHub Action to support use of version specifiers (e.g. `<23`) for Black version (#3265) From f8dd5dadc4d832a8c4f9c48568368cbe34f17751 Mon Sep 17 00:00:00 2001 From: Antonio Ossa Guerra Date: Fri, 23 Sep 2022 11:25:58 -0300 Subject: [PATCH 3/5] Add new `jupyter` option to documentation This new argument enables the installation of the `jupyter` extra when using the Black GitHub Action. The new behaviour is exaplained, and the option is included in the provided example Signed-off-by: Antonio Ossa Guerra --- docs/integrations/github_actions.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/integrations/github_actions.md b/docs/integrations/github_actions.md index d77b9693678..12bcb21fee6 100644 --- a/docs/integrations/github_actions.md +++ b/docs/integrations/github_actions.md @@ -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 `'.'`) @@ -49,6 +53,7 @@ Here's an example configuration: with: options: "--check --verbose" src: "./src" + jupyter: true version: "21.5b1" ``` From d28bd550394ae4474e85a224270f45d35b0cb812 Mon Sep 17 00:00:00 2001 From: Antonio Ossa Guerra Date: Mon, 26 Sep 2022 13:16:12 -0300 Subject: [PATCH 4/5] Add Antonio Ossa Guerra as contributor After the contribution to improve the GitHub Action supported by Black Signed-off-by: Antonio Ossa Guerra --- AUTHORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.md b/AUTHORS.md index 533606240d3..f2d599dd878 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -29,6 +29,7 @@ Multiple contributions by: - [Andrey](mailto:dyuuus@yandex.ru) - [Andy Freeland](mailto:andy@andyfreeland.net) - [Anthony Sottile](mailto:asottile@umich.edu) +- [Antonio Ossa Guerra](mailto:aaossa+black@uc.cl) - [Arjaan Buijk](mailto:arjaan.buijk@gmail.com) - [Arnav Borbornah](mailto:arnavborborah11@gmail.com) - [Artem Malyshev](mailto:proofit404@gmail.com) From 6f66160790f2140498e9f3e4c4da3cc43c2c23f2 Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Mon, 26 Sep 2022 17:40:55 -0400 Subject: [PATCH 5/5] Mention `jupyter` option in changelog and typo fix --- CHANGES.md | 3 ++- action.yml | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 922098b24e7..75948cc8c7d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -47,7 +47,8 @@ -- Update GitHub Action to support formatting of Jupyter Notebook files (#3282) +- 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) diff --git a/action.yml b/action.yml index 61775ecba64..35705e99414 100644 --- a/action.yml +++ b/action.yml @@ -14,7 +14,7 @@ inputs: default: "." jupyter: description: - "Set this option to true to include Jupyter Notebooks files. Default: false" + "Set this option to true to include Jupyter Notebook files. Default: false" required: false default: false black_args: