Search code examples
pythonautomationjupyter-notebook

Is there a way to run all Jupyter Notebooks inside a directory?


Introduction

I have a lot of Jupyter Notebooks inside a directory and I want to run them all to see them output.

What I actually do

I have to open them each one, click on "Restart kernel and re-run the whole notebook?", wait a few minutes and then go for the next one.

What I wish to do

Find a way to just "press a button" (it can be a script, command, or everything), go away for a walk, and come back reading what's the output.

Thanks in advance!


Solution

  • You can achieve this with nbconvert or papermill. See also this answer.

    This is an example in papermill:

    Installation with Anaconda:

    conda install -c conda-forge papermill
    

    Create a new notebook that runs all the notebooks in a specific directory:

    import papermill as pm
    from pathlib import Path
    
    for nb in Path('./run_all').glob('*.ipynb'):
        pm.execute_notebook(
            input_path=nb,
            output_path=nb  # Path to save executed notebook
        )