You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按标签筛选并导出Jupyter单元格至Python脚本

从Jupyter Notebook提取带标签的指定单元格

方法1:用官方库nbformat实现精准提取

这是最稳定的方案,直接依托Jupyter官方的Notebook格式处理库:

  1. 给单元格添加标签:在Notebook里右键目标单元格→「Edit Metadata」,在弹出的JSON框中添加"tags": ["keep"](把keep换成你自定义的标签名),示例:
{
  "tags": ["keep"],
  ... 其他原有metadata内容
}
  1. 编写提取脚本:创建Python脚本读取Notebook并筛选带目标标签的单元格:
import nbformat as nbf

# 替换为你的源Notebook路径
source_nb = "your_notebook.ipynb"
# 替换为目标保存路径
target_nb = "filtered_notebook.ipynb"
target_script = "extracted_code.py"
# 替换为你设置的标签
target_tag = "keep"

# 读取源Notebook
nb = nbf.read(source_nb, as_version=4)

# 筛选符合条件的单元格
filtered_cells = [
    cell for cell in nb.cells 
    if "tags" in cell.metadata and target_tag in cell.metadata["tags"]
]

# 保存为新的Notebook
new_nb = nbf.v4.new_notebook()
new_nb.cells = filtered_cells
nbf.write(new_nb, target_nb)

# 可选:导出为纯Python脚本
with open(target_script, "w", encoding="utf-8") as f:
    for cell in filtered_cells:
        if cell.cell_type == "code":
            f.write(cell.source + "\n\n")
        elif cell.cell_type == "markdown":
            # 将Markdown转为注释保留
            f.write(f"# {cell.source.replace('\n', '\n# ')}\n\n")
  1. 运行脚本:执行后会生成筛选后的Notebook和可选的Python脚本,先确保安装依赖:pip install nbformat

方法2:用papermill批量提取(适合自动化场景)

如果需要批量处理多个Notebook,papermill提供命令行工具直接筛选带标签的单元格:

  1. 给单元格加标签:和方法1步骤一致
  2. 执行命令行提取:
# 替换为你的源文件、目标文件和标签名
papermill input_notebook.ipynb output_notebook.ipynb --extract-only --tags keep

安装依赖:pip install papermill

关于nbmanips失效的可能原因

nbmanips对Notebook metadata的解析逻辑可能和官方库存在差异,比如标签需要放在特定层级,或者存在版本兼容问题。优先使用官方nbformat方案,能避免这类兼容性问题。

内容的提问来源于stack exchange,提问作者Jonathan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 00:42:18