A command line tool to generate a Python script, which programmatically generates Jupyter notebooks, using a Jupyter notebook as a template starting point.
Particularly useful for projects where you need to generate many similar Jupyter notebooks in different directories, which differ only by a few lines e.g. the file which they import.
The intention of this script is to speed up the process of programmatically generating Jupyter notebooks, by providing a script template, which you can further customise downstream.
- Python 3.X
- nbformat
First clone the repo
$ git clone https://github.com/ismailuddin/jupyter-nb-templater.git
Then install using
$ python setup.py install
To generate a Python script to programmatically generate a Jupyter notebook, first run nb-templater
on the template Jupyter notebook file:
$ nb-templater -f input.ipynb -o output_script.py [ -n output.ipynb ]
-f
Input filename of the Jupyter notebook to use as a template
-o
Output filename of the Python script used to generate the notebook
-n
(Optional) The output filename of the Jupyter notebook generated by the script
Once the script has been generated, you can edit this file to customise the cells. For example, you may have a variable name in a cell of the notebook, which you substitute with an argument you provide to the script.
The generated Jupyter notebooks will not have been 'executed' by default. Using a shell script, you can execute this scripts from the command line with the following command:
$ jupyter nbconvert --execute --inplace "input.ipynb"
Covered in a bit more detail in my blog post.
The examples/
directory contains an example of the input Jupyter notebook file, and the output files generated by the scripts.
- Only supports converting notebook cells that either markdown or code.