python-3.xjupyter-notebooknbconvertpapermill

Hide code cells in Jupyter Notebook, execute with Papermill, transform to PDF with nbconvert


I want to run a python program (not command line) using papermill to execute a jupyter notebook and then transform it into a PDF. The idea is working, but I'm unable to hide the input cells.

For papermill report_mode = True is supposed to hide input cells, but there seems to be a problem with jupyter Classic (https://github.com/nteract/papermill/issues/130)

Other extension like hide_input or html scripts are also not sufficient. Maybe a nbconvert template for hide cells is a solution, but I didn't get that running.

My minimal Code:

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)

So I'm looking for a way to execute the Notebook, hide the input cells and transform the Notebook to a PDF. I want to use nbconvert in Python and not as a command line tool, since the Script shall run daily.


Solution

  • I know you said you "don't want to use command line", but how about having your python script execute a subprocess command after running papermill? Mixing with this answer:

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