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 specifically for Jupyter and Google Colab notebooks.

It integrates the high-speed linter ruff and import sorter isort to perform code quality checks, fix issues, and enforce formatting standards.

It allows developers to lint and format code on a cell-by-cell basis or check the entire notebook with simple commands.

Magic cell Commands

Command Type Role Description
%%cl_check Cell Magic Quality Check Displays a linting report for the current cell's code.
%%cl_format Cell Magic Format Preview Formats the current cell's code.
%cl_check Line Magic Quality Check Displays a linting report for the entire saved notebook (requires Google Drive mount).
%cl_auto_format Line Magic Auto Format Activates or deactivates automatic code formatting before every cell execution.

After executing a magic command, the original code of the cell is executed (if applicable to the command).

Installation

Requires Python 3.12 or newer.

pip install colablinter

Usage

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

%load_ext colablinter
  1. Check cell quality (%%cl_check)

    Use %%cl_check to see linting reports for the code below the command.

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

    Output examples:

    ---- Code Quality & Style Check Report ----
    F821 Undefined name `y`
    --> notebook_cell.py:3:16
    |
    2 | def invalid_code(x):
    3 |     return x + y # 'y' is not defined
    |                  ^
    |
    
    Found 1 error.
    -------------------------------------------
    

    Note: After the report is displayed, the code in the cell executes as normal. If errors exist (like F821), execution will fail.

  2. Format cell preview (%%cl_format)

    %%cl_format runs the formatter and corrects code. The cell executes the original code.

    %%cl_format
    import sys
    import os
    def calculate_long_sum(a,b,c,d,e,f):
        return (a+b+c)*(d+e+f)  # messy
    

    Replaced code:

    import os
    import sys
    from datetime import datetime
    
    
    def calculate_long_sum(a, b, c, d, e, f):
        return (a + b + c) * (d + e + f)  # messy
    
  3. Check entire notebook (%cl_check)

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

    %cl_check
    
  4. Activate/Deactivate Auto Format (%cl_auto_format) The %cl_auto_format line magic allows you to automatically format code before every code cell is executed.

    To Activate Auto Formatting:

    %cl_auto_format on # off when you want to deactivate
    

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.0.14.tar.gz (7.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.0.14-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: colablinter-0.0.14.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","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.0.14.tar.gz
Algorithm Hash digest
SHA256 749b3d7981c79bbed0e6f942fca9099e49d833885102fb74e20e5fedf01484b9
MD5 82fd2d1c40d353e289b3d3f4eddf25cd
BLAKE2b-256 35377960074b2a43264a01785a0735950dd83d6e5e3855c727aba0a01c3b63be

See more details on using hashes here.

File details

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

File metadata

  • Download URL: colablinter-0.0.14-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","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.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 2d00857e8922d6ac931ba692e99f1204d7a07226affe5f162ed84c87f5b05e57
MD5 fbf21cc8e8bd152dcfe1781300ed57e8
BLAKE2b-256 6bd9920af7640d6c5fea2325d5149dfd8b1546533025bc37a32da1db6903e969

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