7

I would like to export a JupyterLab notebook (not Jupyter Notebook) to HTML.

I am using the following code inside of the notebook itself, that correctly exports the notebook:

os.popen('jupyter nbconvert current_notebook.ipynb --to html').read()

However, nbconvert is not getting current notebook but the last saved state, on disk, of the notebook.

So, I need to save the state before trying to export it.

I am trying to use the following code:

%%javascript
IPython.notebook.save_notebook()

But apparently JupyterLab does not support JS API, so it is returning the following message:

Javascript Error: IPython is not defined

Do you know a way to save the current state of the notebook before exporting it?

1 Answer 1

9
+50

If it's a fresh notebook and you run it from top to bottom, you can use the following command in the last cell:

import os
%notebook -e test.ipynb
os.system('jupyter nbconvert --to html test.ipynb')

It will give a test.html file.

Or, you can use javascript and HTML to emulate the CTRL + s event,

from IPython.display import display, HTML

### emulate Ctrl + s
script = """
this.nextElementSibling.focus();
this.dispatchEvent(new KeyboardEvent('keydown', {key:'s', keyCode: 83, ctrlKey: true}));
"""
display(HTML((
    '<input style="width:0;height:0;border:0">'
).format(script)))

import os

os.system('jupyter nbconvert --to html test.ipynb') # here, test is the name of your notebook

Now, keyCode: 83 this line can change based on your OS. If you are in windows, 83 should do, else you may have to check the keycode for 's', the easiest way I found was to go to this website http://keycode.info/ and type s.

ref: https://unixpapa.com/js/key.html

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.