jupyterlab-contrib / jupyterlab_code_formatter

A JupyterLab plugin to facilitate invocation of code formatters.
https://jupyterlab-code-formatter.readthedocs.io/
MIT License
855 stars 55 forks source link

Cannot configure to replace black with blue #238

Closed michaelaye closed 2 years ago

michaelaye commented 2 years ago

Checklist prior to opening an issue

Describe the bug Replacing black with blue is not possible. I tried both:

{
    "formatOnSave": false,
    "preferences": {
        "default_formatter": {
            "python": [
                "isort",
                "/home/maye/miniconda3/envs/py39/bin/blue"
            ],
            "r": "formatR"
        }
    },

}

and

{
    "formatOnSave": false,
    "preferences": {
        "default_formatter": {
            "python": [
                "isort",
                "blue"
            ],
            "r": "formatR"
        }
    },

}

Diagnostic commands Please attach the output of the following commands (please format them properly)

Other labextensions (built into JupyterLab) app dir: /home/maye/miniconda3/envs/py39/share/jupyter/lab jupyterlab-theme-solarized-dark v2.0.1 enabled OK

- `jupyter serverextension list`

❯ jupyter serverextension list Config option kernel_spec_manager_class not recognized by ListServerExtensionsApp. config dir: /home/maye/miniconda3/envs/py39/etc/jupyter dask_labextension enabled

image

As you can see, the command blue is in the PATH.

michaelaye commented 2 years ago

I see now that a replacement formatter formally shall be supported by the formatters.py file first, however, the claim of blue is that it works as a drop-in replacement?

ryantam626 commented 2 years ago

Sorry for the late reply, was extremely burnt out and just switched off.

Yes this needs to be formatters.py to work, I will fix this in the coming days.

For now you can do the following:-

Edit your Jupyter notebook configuration (usually found ~/.jupyter/jupyter_notebook_config.py or something like that)

from jupyterlab_code_formatter.formatters import BaseFormatter, handle_line_ending_and_magic, SERVER_FORMATTERS

class BlueFormatter(BaseFormatter):

    label = "Apply Blue Formatter"

    @property
    def importable(self) -> bool:
        try:
            import black

            return True
        except ImportError:
            return False

    @staticmethod
    def handle_options(**options):
        import black

        file_mode_change_version = version.parse("19.3b0")
        current_black_version = version.parse(black.__version__)
        if current_black_version >= file_mode_change_version:
            return {"mode": black.FileMode(**options)}
        else:
            return options

SERVER_FORMATTERS['blue'] = BlueFormatter()

Though obviously you need to replace the import and the invocation of the library within that snippet.

ryantam626 commented 2 years ago

Merged #250 which add support for this, will let you know once I released this, hopefully in the next few days.

ryantam626 commented 2 years ago

Blue should be now supported with version 1.4.11 :tada:

HeadCase commented 2 years ago

I fear a new bug is causing this to fail once again. I am currently running jupyter code formatter v1.4.11 (and blue 0.9.0) and upon specifying blue as a formatter (identically to the O.P.'s second spec) I encounter the following error:

RuntimeError: A mypyc-compiled version of black has already been imported. This prevents blue from operating properly.

It would seem blue wants to be left to import black on it's own?