【问题标题】:Hide code cells in Jupyter Notebook, execute with Papermill, transform to PDF with nbconvert在 Jupyter Notebook 中隐藏代码单元,使用 Papermill 执行,使用 nbconvert 转换为 PDF
【发布时间】:2019-07-31 12:39:55
【问题描述】:

我想使用papermill 运行python 程序(不是命令行)来执行jupyter 笔记本,然后将其转换为PDF。这个想法很有效,但我无法隐藏输入单元格。

对于papermill report_mode = True 应该隐藏输入单元格,但jupyter Classic 似乎有问题(https://github.com/nteract/papermill/issues/130

其他扩展,如 hide_input 或 html 脚本也不够用。 也许隐藏单元格的nbconvert 模板是一个解决方案,但我没有运行它。

我的最小代码:

pm.execute_notebook(
        "Input.ipynb",
        "Output.ipynb",
        parameters=dict(id=id),
        report_mode=True,
    )
notebook_filename = "Output.ipynb"

with open(notebook_filename) as f:
    nb = nbformat.read(f, as_version=4)
pdf_exporter = PDFExporter()
pdf_data, resources = pdf_exporter.from_notebook_node(nb)

所以我正在寻找一种方法来执行 Notebook、隐藏输入单元格并将 Notebook 转换为 PDF。我想在Python 中使用nbconvert 而不是作为命令行工具,因为脚本应该每天运行。

【问题讨论】:

    标签: python-3.x jupyter-notebook nbconvert papermill


    【解决方案1】:

    我知道你说过你“不想使用命令行”,但是让你的 python 脚本在运行papermill 之后执行subprocess 命令怎么样?和这个answer混在一起:

    import subprocess
    
    subprocess.call('jupyter nbconvert --to pdf --TemplateExporter.exclude_input=True Output.ipynb')
    

    【讨论】:

    • 感谢您的想法,可以使用一些 twerking。我在尝试这种方法时遇到了 Shell / Interpreter 的问题。我找到的解决方案是,只需添加以下行:pdf_exporter.exclude_input = True
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    • 1970-01-01
    • 2018-04-25
    • 2018-10-11
    • 2020-11-09
    • 2019-08-27
    • 2020-04-03
    相关资源
    最近更新 更多