/format-ipy-cells

Code formatter for iPython cells

Primary LanguagePythonMIT LicenseMIT

Format iPython Cells

Tests pre-commit.ci status Requires Python 3.10+ pre-commit PyPI

Python code formatter (and pre-commit hook) for cell delimiters (# %%) in VS Code-style interactive Python notebooks.

This formatter ensures

  • cell delimiters are preceded by two empty lines:

    # %% before
    foo='bar'
    # %%
    # %% after
    foo='bar'
    
    
    # %%
  • empty cells are removed:

    # %% before
    
    # %%
    # %% after
  • comments on the same line as cell delimiters are separated by a single space:

    # %%some comment before
    foo = 'bar'
    # %%    another comment
    # %% some comment after
    foo = 'bar'
    # %% another comment

Installation

pip install format-ipy-cells

Usage

CLI

format-ipy-cells path/to/file.py
# or
format-ipy-cells **/*.py

As pre-commit hook

# .pre-commit-config.yaml

repos
  - repo: https://github.com/janosh/format-ipy-cells
    rev: v0.1.10
    hooks:
      - id: format-ipy-cells