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?
Meet Dill. Dill enables you to save the state of a Jupyter Notebook session and restore it with a single command. Dill also works with python interpreter sessions.
Saving your edits is simple. There is a disk icon in the upper left of the Jupyter tool bar. Click the save icon and your notebook edits are saved. It's important to realize that you will only be saving edits you've made to the text sections and to the coding windows.
By default, a Jupyter Notebook saves your work every 2 minutes, and if you want to change this time interval you can do so by using the %autosave n magic command; where n is the number of seconds, and if n=0 this means no autosaving.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With