首页
/ nb-clean 项目教程

nb-clean 项目教程

2024-09-01 18:48:56作者:齐添朝

1. 项目的目录结构及介绍

nb-clean/
├── .github/
│   └── workflows/
│       └── ci.yml
├── nb_clean/
│   ├── __init__.py
│   ├── cli.py
│   ├── filter.py
│   ├── hook.py
│   └── notebook.py
├── tests/
│   ├── __init__.py
│   ├── test_cli.py
│   ├── test_filter.py
│   ├── test_hook.py
│   └── test_notebook.py
├── .gitignore
├── .pre-commit-config.yaml
├── LICENSE
├── README.md
├── pyproject.toml
└── setup.py
  • .github/workflows/ci.yml: GitHub Actions 的持续集成配置文件。
  • nb_clean/: 项目的主要代码目录,包含各个模块的实现。
    • init.py: 初始化文件。
    • cli.py: 命令行接口的实现。
    • filter.py: Git 过滤器的实现。
    • hook.py: 预提交钩子的实现。
    • notebook.py: 处理 Jupyter Notebook 的模块。
  • tests/: 测试代码目录,包含各个模块的测试用例。
  • .gitignore: Git 忽略文件配置。
  • .pre-commit-config.yaml: 预提交钩子的配置文件。
  • LICENSE: 项目许可证。
  • README.md: 项目说明文档。
  • pyproject.toml: 项目配置文件。
  • setup.py: 项目安装脚本。

2. 项目的启动文件介绍

项目的启动文件是 nb_clean/cli.py,它定义了命令行接口,允许用户通过命令行使用 nb-clean 的功能。

# nb_clean/cli.py
import click
from nb_clean.filter import add_filter, remove_filter
from nb_clean.hook import add_hook, remove_hook
from nb_clean.notebook import check_notebook, clean_notebook

@click.group()
def cli():
    pass

@cli.command()
@click.argument('notebook')
def check(notebook):
    check_notebook(notebook)

@cli.command()
@click.argument('notebook')
def clean(notebook):
    clean_notebook(notebook)

@cli.command()
def add_filter():
    add_filter()

@cli.command()
def remove_filter():
    remove_filter()

@cli.command()
def add_hook():
    add_hook()

@cli.command()
def remove_hook():
    remove_hook()

if __name__ == '__main__':
    cli()

3. 项目的配置文件介绍

项目的配置文件主要是 pyproject.toml.pre-commit-config.yaml

pyproject.toml

[tool.poetry]
name = "nb-clean"
version = "3.3.0"
description = "Clean Jupyter notebooks for versioning"
authors = ["Scott Stevenson <scott@stevenson.io>"]
license = "ISC"
readme = "README.md"
repository = "https://github.com/srstevenson/nb-clean"

[tool.poetry.dependencies]
python = "^3.8"
click = "^8.0.0"

[tool.poetry.dev-dependencies]
pytest = "^6.2.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

.pre-commit-config.yaml

repos:
  - repo: https://github.com/srstevenson/nb-clean
    rev: v3.3.0
    hooks:
      - id: nb-clean

这两个文件分别定义了项目的依赖和预提交钩子的配置。pyproject.toml 使用 Poetry 管理依赖,而 .pre-commit-config.yaml 配置了预提交钩子,确保每次提交前自动进行清理。

登录后查看全文
热门项目推荐