-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.py
29 lines (20 loc) · 910 Bytes
/
hook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os
import nbformat
from nbconvert.writers import FilesWriter
_md_exporter = None
def md_post_save(model, os_path, contents_manager, **kwargs):
"""convert notebooks to markdown README.md after save with nbconvert"""
from nbconvert.exporters.markdown import MarkdownExporter
log = contents_manager.log
if model['type'] != 'notebook':
return
global _md_exporter
if _md_exporter is None:
_md_exporter = MarkdownExporter(parent=contents_manager)
nb_node = nbformat.read(model['path'], nbformat.NO_CONVERT)
(output, resources) = _md_exporter.from_notebook_node(nb_node)
c.FilesWriter.build_directory = os.path.dirname(os_path)
fw = FilesWriter(config=c)
fw.write(output, resources, notebook_name="README")
log.info("Saving README.md & dependencies files%s", os.path.dirname(os_path))
c.FileContentsManager.post_save_hook = md_post_save