jupyterlab-contrib / jupyterlab_code_formatter

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

formatters don't work when installed via pipx #178

Open mikeage opened 3 years ago

mikeage commented 3 years ago

Checklist prior to opening an issue

Describe the bug I realize that this is not according to the installation instructions, but I'd like to know if there's any way to make it work.

I run jupyter as a globally installed package, but I prefer to install tools like black and isort using pipx (https://github.com/pipxproject/pipx). This way, I can access the same tools from within any virtualenv, rather than being limited to the global environment only. However, if I do this, even though they're accessible from the command line (they're in ~/.local/bin, which is in my $PATH, they cannot be imported, and therefore they fail to run in jupyterlab_code_formatter.

Is there any way to run formatters that are not available as python modules but are executable from the command line? I realize it might be slower to do a fork/exec (i.e., subprocess) rather than an import, but it also seems like it'd be more flexible if I could run formatters using a command line without needing a python import.

E.g.,

$ python -c "import black"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named black

but

$ which black
/Users/mikemi/.local/bin/black

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

- `jupyter serverextension list`

config dir: /Users/mikemi/.jupyter jupyterlab_code_formatter enabled

Screenshots n/a

Additional context n/a

ryantam626 commented 3 years ago

Hi, that would invovles a fair bit of code change, something that I don't have the time to do and properly test right now. If you would like to have a go at it, implementing a new class based on https://github.com/ryantam626/jupyterlab_code_formatter/blob/3bd3bff8c9ee63f94cc25749e2920c0c217e2c38/serverextension/jupyterlab_code_formatter/formatters.py#L20 is probably a good start.

mikeage commented 3 years ago

Thanks for the response. My first time using jupyter lab (let alone extensions) was yesterday, so I'm not sure if I'm going to be able to tackle this one now... maybe one day.

inigohidalgo commented 1 year ago

FWIW, it seems it might not be a super simple change to do this, especially bc of the complications around how formatted code is returned in formatters.

As a workaround, you can install whatever formatter you want to use into the environment pipx created for jupyterlab.

pipx inject jupyter jupyter-code-formatter
pipx inject jupyter black

This makes the packages available to the extension.