14

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

enter image description here

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.

5 Answers 5

22

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)
Sign up to request clarification or add additional context in comments.

2 Comments

you forgot to import Javascript from Ipython.display before save_notebook()
from IPython.display import Javascript
6

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

Comments

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.

Comments

3

just execute this snippet

!!jupyter nbconvert *.ipynb

3 Comments

This is a great solution. I use !!jupyter nbconvert MY_FILENAME.ipynb to only convert the one notebook. Note that it outputs MY_FILENAME.html. There doesn't appear to be a way to specify the output filename, so instead I use os.rename('MY_FILENAME.html', 'new_path/new_name.html') after generating the HTML file.
From where? Within the notebook, or from a console?
Use !!jupyter nbconvert MY_FILENAME.ipynb --to html in a code cell in the jupyter notebook. Be sure to save the notebook before running it.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.