Skip to content

Instantly share code, notes, and snippets.

@jbwhit
Last active October 23, 2025 03:44
Show Gist options
  • Select an option

  • Save jbwhit/eecdd1cac2756df85ad165f437445b0b to your computer and use it in GitHub Desktop.

Select an option

Save jbwhit/eecdd1cac2756df85ad165f437445b0b to your computer and use it in GitHub Desktop.
Steps to use `ruff` in JupyterLab with the `jupyterlab_code_formatter` plugin.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dlimpid

dlimpid commented Jan 25, 2024

Copy link
Copy Markdown

Thank you very much for the great guide! A problem for me was that cells containing non-ascii unicode characters were not formatted. I could fix this by adding encoding="utf-8" in the subprocess.run() call.

@jbwhit

jbwhit commented Jan 25, 2024

Copy link
Copy Markdown
Author

Thank you for the comment! I'm thinking of revisiting this and seeing if I can make a PR to the extension itself. I'll add your change.

@Gr1nse

Gr1nse commented Mar 26, 2024

Copy link
Copy Markdown

Hi!
Nice Guide!
in this Guide you just use the formater right?
Do you know how to get the ruff linter to work in jupyterlab?

@nicornk

nicornk commented Apr 19, 2024

Copy link
Copy Markdown

I had to add the import "from typing import List" to the config file snippet. @jbwhit

@jbwhit

jbwhit commented Apr 19, 2024

Copy link
Copy Markdown
Author

Thanksk @nicornk -- I've added that to my snippet!

@aliavni

aliavni commented Apr 23, 2024

Copy link
Copy Markdown

❤️

@amano-takahisa

Copy link
Copy Markdown

I am very happy with your config that can also reflect ruff configs written in pyproject.toml such as:

[tool.ruff.format]
quote-style = "single"

@brurucy

brurucy commented Jul 17, 2024

Copy link
Copy Markdown

If you guys also want it to run check before format, then use this:

    @handle_line_ending_and_magic
    def format_code(
        self, code: str, notebook: bool, args: List[str] = [], **options
    ) -> str:
        # Lint
        linting = subprocess.run(
            [self.ruff_bin, "check", "--fix", "--exit-zero", "-"],
            input=code,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True,
            encoding="utf-8",
        )
        
        # Format
        linted_code = linting.stdout
        process = subprocess.run(
            [self.ruff_bin, "format", "-"],
            input=linted_code,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True,
            encoding="utf-8",
        )

        if process.stderr:
            logger.info(process.stderr)
            return code
        else:
            return process.stdout

@JordanTheDodger

Copy link
Copy Markdown

I followed steps from YT vid, still could not get Formatter_icon work w/ ruff_format in active Jupyter notebook. I have installed python 3.10<= using venv. I liked keyboard shortcut which also doesn't work ss attached below. Any help appreciated. Discord: JordanTheDodger image

Update: I finally figured out the error. I am elated to capture my first error with new kid on the block RUFFFFF :100 Kudos @jbwhit

@jbwhit

jbwhit commented Apr 23, 2025

Copy link
Copy Markdown
Author

I followed steps from YT vid, still could not get Formatter_icon work w/ ruff_format in active Jupyter notebook. I have installed python 3.10<= using venv. I liked keyboard shortcut which also doesn't work ss attached below. Any help appreciated. Discord: JordanTheDodger image

Update: I finally figured out the error. I am elated to capture my first error with new kid on the block RUFFFFF :100 Kudos @jbwhit

Was the error on from this document or something on your side?

@JordanTheDodger

Copy link
Copy Markdown

I configured virtual env using conda. For some reason, ruff configuration didn't register. I had to shutdown Jupyter and virtual env. Technically not a "true" error just operational hiccup. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment