Automatically save notebook as HMTL with... JavaScript?

37 views
Skip to first unread message

Mayeshh

unread,
Aug 21, 2017, 4:14:07 PM8/21/17
to Project Jupyter
Hello,

I am very new to python, and I am currently using Jupyter notebooks to analyze microscopy data. I have looked into using `nbconvert` as a library, however, I have to run these particular notebooks manually (centering images, choose files through globing, etc.). For this reason, I don't want to implement `nbconvert` from the command line. 

My not-solution has been to use the Jupyter notebook GUI to save the output as HTML (required for record keeping) and update the name manually. This is problematic because I can easily forget to save the notebook or name it incorrectly. I am also trying to speed up my workflow, and I have identified this step as a bottleneck...

How can I automate this action within the notebook? Will it require JavaScript? If so, I lack any knowledge of JavaScript... Can anyone offer a solution to this use-case? Any advice is greatly appreciated! 

MinRK

unread,
Aug 22, 2017, 2:45:33 AM8/22/17
to Project Jupyter

I suspect what you want is a post-save hook that calls nbconvert. For example:

import io
from nbcovert import export, HTMLExporter

def script_post_save(model, os_path, contents_manager, **kwargs):
    """convert notebooks to HTML after save with nbconvert"""

    if model['type'] != 'notebook':
        return

    global _exporter

    if _exporter is None:
        _exporter = HTMLExporter(parent=contents_manager)

    base, ext = os.path.splitext(os_path)
    # your rename logic goes here
    html_fname = base + '.html'
    html, resources = _exporter.from_filename(os_path)

    with io.open(html_fname, 'w', encoding='utf-8') as f:
        f.write(html)

c.FileContentsManager.post_save_hook = script_post_save

-Min


--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/487c5080-c4cc-45a2-90f1-7b5066f663e6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mayeshh

unread,
Aug 22, 2017, 5:41:49 PM8/22/17
to Project Jupyter
Yes, this is exactly what I am looking for. Thank you for your detailed reply!!! This is all very helpful.


To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages