Search code examples
pythonjupyter-notebooknbconvert

jupyter nbconvert: How to execute and output multiple format without execute multiple time?


I am using nbconvert.

From the SimpleNotebook.ipynb, I want to output two file:

  • Execute SimpleNotebook.ipynb, then save the notebook with output cell.
  • A HTML without code.

My command line for doing the job:

jupyter nbconvert SimpleNotebook.ipynb --to notebook --execute 
jupyter nbconvert SimpleNotebook.ipynb --to html --execute --no-input

output console

[NbConvertApp] Converting notebook SimpleNotebook.ipynb to notebook
[NbConvertApp] Executing notebook with kernel: python3
[NbConvertApp] Writing 18547 bytes to SimpleNotebook.nbconvert.ipynb
[NbConvertApp] Converting notebook SimpleNotebook.ipynb to html
[NbConvertApp] Executing notebook with kernel: python3
[NbConvertApp] Writing 280188 bytes to SimpleNotebook.html

It created

  • SimpleNotebook.nbconvert.ipynb (a notebook with output cell)

  • SimpleNotebook.html (the web output)

jupyter nbconvert SimpleNotebook.ipynb --to notebook --execute --no-input jupyter nbconvert SimpleNotebook.ipynb --to html --execute --no-input

The problem: It run twice. As I can see in the console output:

[NbConvertApp] Executing notebook with kernel: python3

Happen twice, because it execute notebook again for each command line.

I would like to output both file SimpleNotebook.nbconvert.ipynb and SimpleNotebook.html but only execute the notebook once. How should I write the command line ?


Solution

  • You don't want --execute in your second command. And make sure to use the output of the first command as the input for the second.

    The following would replace your two commands using your example:

    jupyter nbconvert SimpleNotebook.ipynb --to notebook --execute 
    jupyter nbconvert SimpleNotebook.nbconvert.ipynb --to html --no-input