Automate Jupyter Notebooks with Papermill

papermill is a tool for parameterizing, executing, and analyzing Jupyter Notebooks.

This unlocks new use cases for notebooks, such as:

  • Creating reusable reports with variable input parameters
  • Chaining notebooks together for sequential execution
  • Integrating notebooks into automated workflows

The following example shows how to run a quarterly report notebook with different parameters.

import papermill as pm

pm.execute_notebook(
    input_path='quarterly_report.ipynb',
    output_path='Q1_2024_report.ipynb',
    parameters=dict(
        quarter="Q1",
        year=2024,
        include_forecast=True
    )
)

In this example, the execute_notebook function takes the input notebook (quarterly_report.ipynb), creates a new output notebook (Q1_2024_report.ipynb), and injects the specified parameters.

You can also run papermill from the command line using the following syntax:

papermill quarterly_report.ipynb Q1_2024_report.ipynb -p quarter Q1 -p year 2024 -p include_forecast True

By using papermill, you can automate the execution of your Jupyter Notebooks and make them more flexible and reusable.

Link to papermill.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Work with Khuyen Tran

Work with Khuyen Tran