Jupyter-ruff: A JupyterLab extension to format code using Ruff

I’ve recently put some time into developing a new extension for JupyterLab and Jupyter Notebook. That extension, jupyter-ruff does what it says on the tin, and can be used to format Python code in Jupyter notebooks with Ruff. It is currently available on PyPi and NPM.

Think nb_black but more modern and maintained, or jupyterlab_code_formatter but much easier to set up. You simply have to install the extension, no extra configuration required. It will then allow you to format Python code using menus, keyboard shortcuts, automatically format cells on execution, or format cells when a notebook is saved. Optionally, it can also sort your imports using an isort compatible algorithm.

On the more technical side, it actually uses a WASM build of Ruff internally, so it is a pure client-side extension. This may ease installation in some cases. Still, it will read and discover your local Ruff configuration.

In any case, I would love to hear your feedback and maybe see some people other than me start using the project!

7 Likes

The extension is now also available on conda-forge!

https://anaconda.org/conda-forge/jupyter-ruff

3 Likes

I got this to work according to the instructions. Thanks.

But I couldn’t get to when using jupyter-lab via uv run --with jupyter jupyter lab. Should that work?

I assume you would have to install the extension in the environment for it to work.

For me, the command uv run --with jupyter --with jupyter-ruff jupyter lab works. So maybe try that and let me know it it solves your problem. Cheers!

2 Likes

That works fine. Thanks. I didn’t understand that you could use multiple --with.

To highlight that since it is easy to miss…
Here is how it is presently stated on the current UV documentation ‘Running scripts’ page, under the section ‘Running a script with dependencies’, towards the bottom:

“Multiple dependencies can be requested by repeating with --with option.”