问题
I have a Jupyter Notebook program, which do analysis for me. After it's been run, I want to save it as HTML so I can view it later. (And then I can change the input data file to do analysis for other data.)
Typically, I do this by hand. This would look like
But this feels very tedious for me. So I'm wondering if there is any code can do this for me? Maybe something like
%save_html
# or with a file_name
%save_html file_name
Note: I have figured out a workaround for this. But I didn't find too much info by search, so I post it here and it may help someone else having the same problem. I'll post my solution as an answer.
回答1:
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
回答2:
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)
    回答3:
First open console and go to directory where your notebook is.
Second enter this command:
ipython nbconvert --to HTML your_notebook.ipynb
After that you will have new file with name "your_notebook.html". Thats all.
You can check here for more information.
回答4:
If you want a solution purely based on code, executed within the notebook, you can use the following short code snippet, which will name your file 'test_1':
a = 1
command = f'jupyter nbconvert Untitled.ipynb --output test_{a}.html'
subprocess.call(command)
    回答5:
just execute this snippet
!!jupyter nbconvert *.ipynb
    来源:https://stackoverflow.com/questions/37657547/how-to-save-jupyter-notebook-to-html-by-code