Reputation: 115
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.
Upvotes: 1
Views: 2394
Reputation: 1413
You're missing one small detail in your 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()
# add this
pdf_exporter.exclude_input = True
pdf_data, resources = pdf_exporter.from_notebook_node(nb)
You can also use Ploomber for this; it has a few benefits, like running notebooks in parallel:
# convert.py
from pathlib import Path
from ploomber import DAG
from ploomber.tasks import NotebookRunner
from ploomber.products import File
from ploomber.executors import Parallel
dag = DAG(executor=Parallel())
# hide input and convert to PDF using the LaTeX converter
NotebookRunner(
Path('input.ipynb'),
File('output-latex.pdf'),
dag=dag,
# do not include input code (only cell's output)
nbconvert_export_kwargs={'exclude_input': True},
name='latex')
# hide input and convert to PDF using the web PDF converter (no need to install LaTeX!
NotebookRunner(
Path('input.ipynb'),
File('output-web.pdf'),
dag=dag,
# do not include input code (only cell's output)
nbconvert_export_kwargs={'exclude_input': True},
# use webpdf converter
nbconvert_exporter_name='webpdf',
name='web')
# generate both PDFs
if __name__ == '__main__':
dag.build()
Note: as of Ploomber 0.20, notebooks must have a "parameters" cell (you can add an empty one). See instructions here.
To run it:
pip install ploomber
python convert.py
Upvotes: 0
Reputation: 1967
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')
Upvotes: 3