# How to archive jupyter notebooks

**URL:** <https://discourse.slicer.org/t/how-to-archive-jupyter-notebooks/12239>\
**Category:** Support\
**Tags:** jupyter\
**Created:** [June 27, 2020, 3:41am UTC](https://discourse.slicer.org/t/how-to-archive-jupyter-notebooks/12239 "2020-06-27T03:41:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)\
**Post date:** [June 27, 2020, 3:41am UTC](https://discourse.slicer.org/t/how-to-archive-jupyter-notebooks/12239/1 "2020-06-27T03:41:07Z")

</div>

I use SlicerJupyter to run automated experiments with Slicer, generating snapshots of the views. Sometimes I forget to save the notebook with valuable results. For non-Slicer notebooks, I can use an external process to save my notebook as a single html file:  
os.system("jupyter nbconvert --to html " + notebook\_name + " --output " + filename)  
But this doesn’t work in Slicer, because the command ‘jupyter’ is not available in the Slicer environment. Does anybody happen to know a way to automatically (triggered by notebook code) save a Slicer Jupyter notebook after running it?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 27, 2020, 6:17am UTC](https://discourse.slicer.org/t/how-to-archive-jupyter-notebooks/12239/2 "2020-06-27T06:17:31Z")

</div>

You should be able to run nbconvert utility from any python environment, just need to [pip install it](https://nbconvert.readthedocs.io/en/latest/install.html). I don’t think it requires jupyter, you should be able to use it directly - see API description [here](https://nbconvert.readthedocs.io/en/latest/api/nbconvertapp.html).

Slicer’s JupyterNotebooksLib has a convenience function for getting the full path of your notebook:

```python
import JupyterNotebooksLib as slicernb
notebook_path = slicernb.notebookPath()

```

---

<div class="post-metadata">

**Author:** ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)\
**Post date:** [June 27, 2020, 3:57pm UTC](https://discourse.slicer.org/t/how-to-archive-jupyter-notebooks/12239/3 "2020-06-27T15:57:27Z")

</div>

Thanks. This pointed in the right direction. For the record, here is a solution that seems to work:

`notebooks_save_path` – define as the folder for archived notebooks  
`save_timestamp = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')`  
– this will be used to create unique filename for the saved file

Add a cell to save the current state of the notebook:

```python
from IPython.display import Javascript
script = '''
require(["base/js/namespace"],function(Jupyter) {
    Jupyter.notebook.save_checkpoint();
});
'''
Javascript(script)

```

And finally, this cell saves the notebook to an html file:

```python
import nbformat
from nbconvert import HTMLExporter
import json

notebook_path = slicernb.notebookPath()

with open(notebook_path, mode="r") as f:
    file_json = json.load(f)
    
notebook_content = nbformat.reads(json.dumps(file_json), as_version=4)

html_exporter = HTMLExporter()
(body, resources) = html_exporter.from_notebook_node(notebook_content)

this_notebook_name = os.path.splitext(os.path.basename(notebook_path))[0]
save_file_name = this_notebook_name + "_" + save_timestamp + ".html"
notebook_fullpath = os.path.join(notebooks_save_path, save_file_name)

f = open(notebook_fullpath, 'wb')
f.write(body.encode())
f.close()

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 27, 2020, 6:38pm UTC](https://discourse.slicer.org/t/how-to-archive-jupyter-notebooks/12239/4 "2020-06-27T18:38:20Z")

</div>

Thanks a lot. I’ve added convenience functions to make it these features easily accessible:

```python
import JupyterNotebooksLib as slicernb

slicernb.notebookSaveCheckpoint()
slicernb.notebookExportToHtml()

```
