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.
| 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.
Requires Python 3.10 or newer.
pip install colablinterThe extension must be explicitly loaded in the notebook session before use. Once the extension is loaded, %lautoformat is activated by default.
%load_ext colablinter-
Sorts imports and Formats cell (
%%cformat)%%cformatcorrects 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)
-
Check cell quality (
%%ccheck)Use
%%ccheckto 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.
-
Activate/Deactivate Auto Fix (
%lautoformat)The
%lautoformatline 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
-
Check entire notebook (
%lcheck)Use line magic
%lcheckto 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
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