跳转到主要内容

格式化交互式Python笔记本中的单元格。

项目描述

格式化iPython单元格

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

Python代码格式化器(和pre-commit钩子)用于VS Code风格的交互式Python笔记本中的单元格分隔符(# %%)。

此格式化器确保:

  • 单元格分隔符之前有两行空行

    # %% before
    foo='bar'
    # %%
    
    # %% after
    foo='bar'
    
    
    # %%
    
  • 删除空单元格

    # %% before
    
    # %%
    
    # %% after
    
  • 与单元格分隔符在同一行的注释之间用一个空格分隔

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

安装

pip install format-ipy-cells

使用

命令行界面

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

作为pre-commit钩子

# .pre-commit-config.yaml

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

支持者