Skip to content

Instantly share code, notes, and snippets.

@cscorley
Last active January 9, 2021 21:47
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save cscorley/9144544 to your computer and use it in GitHub Desktop.
Save cscorley/9144544 to your computer and use it in GitHub Desktop.
IPython to Jekyll Markdown
try:
from urllib.parse import quote # Py 3
except ImportError:
from urllib2 import quote # Py 2
import os
import sys
BLOG_DIR = os.environ['BLOG_DIR']
# BLOG_DIR = '/Users/cscorley/git/cscorley.github.io/'
f = None
for arg in sys.argv:
if arg.endswith('.ipynb'):
f = arg.split('.ipynb')[0]
break
c = get_config()
c.NbConvertApp.export_format = 'markdown'
c.MarkdownExporter.template_path = ['/Users/cscorley/.ipython/templates']
c.MarkdownExporter.template_file = 'jekyll'
#c.Exporter.file_extension = 'md'
def path2support(path):
"""Turn a file path into a URL"""
parts = path.split(os.path.sep)
return '{{ site.baseurl}}notebooks/' + '/'.join(quote(part) for part in parts)
c.MarkdownExporter.filters = {'path2support': path2support}
if f:
c.NbConvertApp.output_base = f.lower().replace(' ', '-')
c.FilesWriter.build_directory = BLOG_DIR + '/notebooks'
{% extends 'display_priority.tpl' %}
{%- block header -%}
---
layout: post
title: "{{resources['metadata']['name']}}"
tags:
- python
- notebook
---
{%- endblock header -%}
{% block in_prompt %}
**In [{{ cell.prompt_number }}]:**
{% endblock in_prompt %}
{% block output_prompt %}
{%- endblock output_prompt %}
{% block input %}
{{ '{% highlight python %}' }}
{{ cell.input }}
{{ '{% endhighlight %}' }}
{% endblock input %}
{% block pyerr %}
{{ super() }}
{% endblock pyerr %}
{% block traceback_line %}
{{ line | indent | strip_ansi }}
{% endblock traceback_line %}
{% block pyout %}
{% block data_priority scoped %}
{{ super() }}
{% endblock %}
{% endblock pyout %}
{% block stream %}
{{ output.text | indent }}
{% endblock stream %}
{% block data_svg %}
![svg]({{ output.svg_filename | path2support }})
{% endblock data_svg %}
{% block data_png %}
![png]({{ output.png_filename | path2support }})
{% endblock data_png %}
{% block data_jpg %}
![jpeg]({{ output.jpeg_filename | path2support }})
{% endblock data_jpg %}
{% block data_latex %}
{{ output.latex }}
{% endblock data_latex %}
{% block data_html scoped %}
{{ output.html }}
{% endblock data_html %}
{% block data_text scoped %}
{{ output.text | indent }}
{% endblock data_text %}
{% block markdowncell scoped %}
{{ cell.source | wrap_text(80) }}
{% endblock markdowncell %}
{% block headingcell scoped %}
{{ '#' * cell.level }} {{ cell.source | replace('\n', ' ') }}
{% endblock headingcell %}
{% block unknowncell scoped %}
unknown type {{ cell.type }}
{% endblock unknowncell %}
@ketch
Copy link

ketch commented Dec 31, 2014

Thanks! I found this (and your blog post) very useful.

@tgarc
Copy link

tgarc commented Jul 26, 2015

Thanks! This and your blog post were very useful to me. Note this doesn't work with newer versions of IPython since the underlying Exporter classes and ipynb output format have changed a bit. I've written a slightly modified version that should be a little more robust to updates in the IPython package which can be found here:

https://gist.github.com/tgarc/7d6901858ef708030c19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment