Skip to main content

Linting and formatting Python code in Google Colab.

Project description

release

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 with simple commands.

Magic cell Commands

Command Description
%clautoformat Activates or deactivates automatic import sorting, formatting, and execution time display before every cell.
%%clcheck Displays a linting report for the current cell.

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

Installation

Just run it in colab:

!uv pip install colablinter

The extension must be explicitly loaded in the notebook session before use.

%load_ext colablinter

Once loaded, %clautoformat is activated by default to keep your code clean from the start.

Usage

  1. Activate/Deactivate Auto Fix (%clautoformat)

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

    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)
    

    To deactivate auto fixing:

    %clautoformat off # %clautoformat on when you want to activate
    
  2. Check cell quality (%%clcheck)

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

    %%clcheck
    
    def invalid_code(x):
        return x + y
    

    Output:

    [ColabLinter:INFO] F821 Undefined name `y`
    --> tmp.py:2:16
      |
    1 | def invalid_code(x):
    2 |     return x + y
      |                ^
      |
    Found 1 error.
    [ColabLinter:INFO]  Done | 0.015s
    

    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.

Known Caveats & Troubleshooting

Magic Command Execution: When using magic or terminal commands while %clautoformat 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 %clautoformat 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.3.0.tar.gz (5.7 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.3.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: colablinter-0.3.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","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.3.0.tar.gz
Algorithm Hash digest
SHA256 b12f835f7eb86cc3ded0dd1f8324c94b2bd7119897e88274d5c022746f58252a
MD5 f603baa0b5ed0a31402d4504176761f6
BLAKE2b-256 ed440d4ca18ae1a0ff6280f908f54af5bfc0f738ca6c6b38e1288629d34a3c45

See more details on using hashes here.

File details

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

File metadata

  • Download URL: colablinter-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 545b94f2b1d61034ef602971c9c42a9b5aba4e4f885149175ece6d80f51a6263
MD5 1a5cc4e6a87b7dead2d97724634c2b5b
BLAKE2b-256 200c790c5e422e0302cfb8e4b78ac86634918ff76e2ab1587f0fce69b7e44c81

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