Skip to content

Support formatting Jupyter Notebooks in GitHub Actions #3280

@aaossa

Description

@aaossa

Is your feature request related to a problem? Please describe.

I'm trying to setup a GitHub Action that runs Black on a project that includes *.py and *.ipynb files, but the default action does not include the Jupyter extra. I followed the integration described in this piece of documentation but the option to include the Jupyter extra (black[jupyter]) is not available.

Describe the solution you'd like

If the action included an argument to include the Jupyter extra, the GitHub Action would work in as expected (when using pip install black[jupyter] locally).

Describe alternatives you've considered

I considered a custom GitHub Action and installing Black manually, but found out that modifying part of the action available in this repository is cleaner and would bring support to users with a similar need without affecting those that already use the GitHub Action.

Additional context

I was trying different things out and arrived to a solution that works as expected and can be included in this project without affecting users that already use the GitHub Action. Add a new option to the GitHub Action to enable the Jupyter extra dependency. I think that a boolean value might do the trick and using false as default maintains the current behavior.

diff --git a/action.yml b/action.yml
index cfa6ef9..ed6c32e 100644
--- a/action.yml
+++ b/action.yml
@@ -8,6 +8,10 @@ inputs:
       '--check --diff'"
     required: false
     default: "--check --diff"
+  jupyter:
+    description: "Include the required extra dependencies to format Jupyter Notebooks."
+    required: false
+    default: false
   src:
     description: "Source to run Black. Default: '.'"
     required: false
@@ -38,6 +42,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

In this file, if the flag is enabled (if the INPUT_JUPYTER envar has a true value) then the jupyter extra is included in the installation step. Colorama is already included by default.

diff --git a/action/main.py b/action/main.py
index cd920f5..fbf6e73 100644
--- a/action/main.py
+++ b/action/main.py
@@ -10,11 +10,16 @@ ENV_BIN = ENV_PATH / ("Scripts" if sys.platform == "win32" else "bin")
 OPTIONS = os.getenv("INPUT_OPTIONS", default="")
 SRC = os.getenv("INPUT_SRC", default="")
 BLACK_ARGS = os.getenv("INPUT_BLACK_ARGS", default="")
+JUPYTER = os.getenv("INPUT_JUPYTER")
 VERSION = os.getenv("INPUT_VERSION", default="")

 run([sys.executable, "-m", "venv", str(ENV_PATH)], check=True)

-req = "black[colorama]"
+
+if JUPYTER:
+    req = "black[colorama,jupyter]"
+else:
+    req = "black[colorama]"
 if VERSION:
     req += f"=={VERSION}"
 pip_proc = run(

The only difference would be visible in case I want to use the Jupyter extra, which can be enabled by passing the value explicitly:

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: psf/black@stable
+         jupyter: true
          options: "--check --diff --verbose"

I forked this project to test the GitHub Action and it does work as expected (aaossa@7af4287). If you agree with this feature request, I can submit a PR with these changes and update the relevant documentation 👌

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: integrationsEditor plugins and other integrationsC: jupyterJupyter Notebooks, any color you likeS: acceptedThe changes in this design / enhancement issue have been accepted and can be implementedT: enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions