Skip to content

Instantly share code, notes, and snippets.

@NHDaly
Created November 28, 2019 03:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NHDaly/4e8bf10eb38852cf0e3673ae6982ba0c to your computer and use it in GitHub Desktop.
Save NHDaly/4e8bf10eb38852cf0e3673ae6982ba0c to your computer and use it in GitHub Desktop.
Jupyter configuration to auto-export scripts with markdown->comments, even for non-python languages (Julia)
import io
import os
from notebook.utils import to_api_path
_script_exporter = None
_html_exporter = None
def script_post_save(model, os_path, contents_manager, **kwargs):
if model['type'] != 'notebook':
return
from nbconvert.exporters.script import ScriptExporter
from nbconvert.exporters.html import HTMLExporter
global _script_exporter
if _script_exporter is None:
_script_exporter = ScriptExporter(parent=contents_manager)
_script_exporter.template_file = os.path.dirname(__file__) + '/jupyter_script_export_template.tpl'
global _html_exporter
if _html_exporter is None:
_html_exporter = HTMLExporter(parent=contents_manager)
log = contents_manager.log
#export_script(_html_exporter, model, os_path, contents_manager, **kwargs)
export_script(_script_exporter, model, os_path, contents_manager, **kwargs)
def export_script(exporter, model, os_path, contents_manager, **kwargs):
"""convert notebooks to Python script after save with nbconvert
replaces `ipython notebook --script`
"""
base, ext = os.path.splitext(os_path)
script, resources = exporter.from_filename(os_path)
script_fname = base + resources.get('output_extension', '.txt')
log = contents_manager.log
log.info("Saving script /%s", to_api_path(script_fname, contents_manager.root_dir))
with io.open(script_fname, 'w', encoding='utf-8') as f:
f.write(script)
c.FileContentsManager.post_save_hook = script_post_save
{# Lines inside these brackets are comments #}
{#- Brackets with a `-` mean to skip whitespace before or after the block. -#}
{#-
# This file defines a Jinja Template for converting .ipynb files into scripts
# for either delve or julia. We need this because the default script exporter
# doesn't render Markdown for any languages except Python.
# Exporting the markdown makes github reviews of .ipynb files easier.
# This template is invoked by our custom `jupyter_notebook_config.py`. You can
# read more about the Jinja template specification here:
# http://jinja.pocoo.org/docs/2.10/templates/#comments
# and here:
# https://nbconvert.readthedocs.io/en/latest/customizing.html
# And the filter functions used in this file are defined here:
# https://github.com/pallets/jinja/blob/master/jinja2/filters.py
# and here:
# https://github.com/jupyter/nbconvert/blob/master/nbconvert/filters/strings.py
-#}
{#- ---------
# Lines up here, before the `extends` section, go at the top of the file, before any other
# content from the notebook itself.
#----------- -#}
{%- if 'name' in nb.metadata.get('kernelspec', {}) and
nb.metadata.kernelspec.name == 'julia' -%}
# This file was generated from a Julia language jupyter notebook.
{% endif -%}
{% extends 'script.tpl'%}
{% block markdowncell %}
{#- Turn the contents of the markdown cell into a wrapped comment block, and trim empty lines. -#}
{#-
# NOTE: We used `kernelspec.name` not `language_info.name`, for reasons specific to
# our custom jupyter kernel. I think `language_info.name` might be more robust?
-#}
{%- if 'name' in nb.metadata.get('kernelspec', {}) and
nb.metadata.kernelspec.name == 'julia' -%}
{#- Delve language files are now converted to .jl scripts, so use `#` -#}
{%- set commentprefix = '# ' -%}
{#-
# Add other languages here as if-else block, e.g. C++ would use '// '
-#}
{%- else -%}
{%- set commentprefix = '# ' -%}
{%- endif -%}
{%- set commentlen = 92-(commentprefix|length) -%}
{{- '\n' -}}
{{- commentprefix ~ '-' * commentlen -}}
{{- '\n' -}}
{#- Turn the contents of the markdown cell into a wrapped comment block, and trim empty lines. -#}
{#- Note: `comment_lines` and `wrap_text` are defined in nbconvert/filters/strings.py -#}
{{- cell.source | wrap_text(width=commentlen) | comment_lines(prefix=commentprefix) | replace(commentprefix~"\n", commentprefix|trim ~ "\n") -}}
{{- '\n' -}}
{{- commentprefix ~ '-' * commentlen -}}
{{- '\n' -}}
{% endblock markdowncell %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment