Skip to main content

Linting and formatting Python code in Google Colab.

Project description

colablinter

PyPI version License: MIT

Overview

colablinter is an IPython magic command extension designed for Jupyter and Google Colab notebooks. It integrates the high-speed linter ruff to perform code quality checks and formatting directly within Jupyter/Colab cells. It allows developers to lint code on a cell-by-cell basis or the entire notebook with simple commands.

Magic cell Commands

Command Description
%%cformat Sorts imports and Formats the current cell's code.
%%ccheck Displays a linting report for the current cell.
%lautoformat Activates or deactivates automatic import sorting, formatting, and execution time display before every cell.
%lcheck Displays a linting report for the entire saved notebook (requires Google Drive mount).

After executing a cell magic command, the checked/formatted code is immediately executed (if applicable), maintaining the notebook workflow.

Installation

Requires Python 3.10 or newer.

pip install colablinter

Usage

The extension must be explicitly loaded in the notebook session before use. Once the extension is loaded, %lautoformat is activated by default.

%load_ext colablinter
  1. Sorts imports and Formats cell (%%cformat)

    %%cformat corrects code and runs the formatter. The cell executes after cell is formatted.

    %%cformat
    import math, sys;
    
    class Example(   object ):
        def __init__    ( self, bar ):
          if bar : bar+=1;  bar=bar* bar   ; return bar
          else:
                        some_string = "foo"
                        return (sys.path, some_string)
    

    Fixed Cell:

    import math
    import sys
    
    
    class Example(object):
        def __init__(self, bar):
            if bar:
                bar += 1
                bar = bar * bar
                return bar
            else:
                some_string = "foo"
                return (sys.path, some_string)
    
  2. Check cell quality (%%ccheck)

    Use %%ccheck to see linting reports for the code below the command. After the report is displayed, the code in the cell executes as normal.

    %%ccheck
    
    def invalid_code(x):
        return x + y # 'y' is not defined
    

    Output:

    [ColabLinter:INFO] F821 Undefined name `y`
    --> notebook_cell.py:2:16
    |
    1 | def invalid_code(x):
    2 |     return x + y # 'y' is not defined
    |                  ^
    |
    
    Found 1 error.
    

    Note on F401: The linter is explicitly configured to ignore F401 errors (unused imports). This is to ensure compatibility with the stateful nature of Jupyter/Colab notebooks, where imports in one cell may be necessary for code execution in subsequent cells, preventing unintended breakage of the notebook's execution flow.

  3. Activate/Deactivate Auto Fix (%lautoformat)

    The %lautoformat line magic allows you to automatically fix code before every code cell is executed.

    To Activate Auto Fixing:

    %lautoformat on # %lautoformat off when you want to deactivate
    
  4. Check entire notebook (%lcheck)

    Use line magic %lcheck to check across the entire saved notebook file (requires the notebook to be saved to Google Drive and mounted).

    %lcheck /content/drive/MyDrive/Colab Notebooks/path/to/notebook.ipynb
    

Known Caveats & Troubleshooting

Magic Command Execution: When using magic or terminal commands while %lautoformat is active, the auto-format mechanism is temporarily suppressed during the final execution step to prevent infinite loops or dual checks. If you want to disable auto-formatting, use %lautoformat off

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

colablinter-0.2.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

colablinter-0.2.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file colablinter-0.2.0.tar.gz.

File metadata

  • Download URL: colablinter-0.2.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for colablinter-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a51fc79aa824bf113290bad5fcd8d8708bea0a63ef9d333cf7a49eac6e1bf0e7
MD5 956b20e126c4df12ff27c4ee31bf77ef
BLAKE2b-256 2f3f9be1387121b28838c276e4a035c006e053acb154a1e33e72630168f9296d

See more details on using hashes here.

File details

Details for the file colablinter-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: colablinter-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for colablinter-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 63466f4b1dff302f2644504893cf78ad3bc8a4b18e4221166e132a693858bd51
MD5 d752c5aedf10a6fa100c2d65e40a8b74
BLAKE2b-256 60faf1b6b6716309c9374d63d1abb41f02ac9f574c370576664aa76378943b9f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page