Last active
September 22, 2020 13:25
-
-
Save mwouts/04a6dfa571bda5cc59fa1429d130998f to your computer and use it in GitHub Desktop.
A save hook for Jupyter that automatically exports notebooks as slides
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""This is a save hook for Jupyter that automatically exports your notebook to reveal slides, | |
every time you save it, if | |
- it has at least one cell with a "slideshow" metadata | |
- and if the notebook is not `Untitled...` | |
Add the following to your `.jupyter/jupyter_notebook_config.py` file: | |
(source: https://gist.github.com/mwouts/04a6dfa571bda5cc59fa1429d130998f ) | |
""" | |
import io | |
import os | |
import nbformat | |
from nbconvert.exporters import SlidesExporter | |
from notebook.utils import to_api_path | |
def export_to_slides(model, path, contents_manager, **kwargs): | |
"""A pre-save hook that exports notebooks as slides if one of their cell has a "slideshow" metadata""" | |
if model["type"] != "notebook": | |
return | |
nb = model['content'] | |
assert nb is not None, "Please call this function as a pre_save_hook" | |
try: | |
# Is this a paired notebook? | |
from jupytext.paired_paths import paired_paths | |
if path in contents_manager.paired_notebooks: | |
fmt, formats = contents_manager.paired_notebooks.get(path) | |
alt_paths = paired_paths(path, fmt, formats) | |
# run the export only if this function is called on the first element of the pair | |
if len(alt_paths) > 1 and path != alt_paths[0][0]: | |
return | |
except (AttributeError, ImportError): | |
pass | |
slideshow = False | |
for cell in nb['cells']: | |
if 'slideshow' in cell['metadata']: | |
slideshow = True | |
break | |
if not slideshow: | |
return | |
log = contents_manager.log | |
log.debug('Notebook %s has a slideshow cell metadata', path) | |
os_path = contents_manager._get_os_path(path) | |
if os.path.basename(os_path).startswith('Untitled'): | |
log.info("Not exporting %s as slides because it's untitled", path) | |
return | |
exporter = SlidesExporter(parent=contents_manager) | |
slides, resources = exporter.from_notebook_node(nbformat.from_dict(nb)) | |
base, __ = os.path.splitext(os_path) | |
slides_fname = base + resources.get('output_extension', '.html') | |
log.info("Saving slides at /%s", to_api_path(slides_fname, contents_manager.root_dir)) | |
with io.open(slides_fname, "w", encoding="utf-8") as f: | |
f.write(slides) | |
c.FileContentsManager.pre_save_hook = export_to_slides |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment