How to save Jupyter Notebook to HTML by code?

you should be able to find a script in the same directory that has the jupyter-notebook.exe file. Its name is jupyter-nbconvert.exe. Run it like this:

./jupyter-nbconvert.exe --to html 'path/to/nb.ipynb'`

Docs


I'll give an answer myself.

from IPython.display import Javascript
from nbconvert import HTMLExporter

def save_notebook():
    display(
        Javascript("IPython.notebook.save_notebook()"),
        include=['application/javascript']
    )

def output_HTML(read_file, output_file):
    import codecs
    import nbformat
    exporter = HTMLExporter()
    # read_file is '.ipynb', output_file is '.html'
    output_notebook = nbformat.read(read_file, as_version=4)
    output, resources = exporter.from_notebook_node(output_notebook)
    codecs.open(output_file, 'w', encoding='utf-8').write(output)

In the last cell of the notebook, something like

import time

save_notebook()
time.sleep(3)
current_file = 'GMM.ipynb'
output_file = 'output_file.html'
output_HTML(current_file, output_file)