Skip to content

Instantly share code, notes, and snippets.

@tgarc
Last active March 25, 2022 13:44
Show Gist options
  • Save tgarc/7d6901858ef708030c19 to your computer and use it in GitHub Desktop.
Save tgarc/7d6901858ef708030c19 to your computer and use it in GitHub Desktop.
Jekyll IPython notebook converter
ipython nbconvert --to markdown <notebook>.ipynb --config jekyll.py
# modification of config created here: https://gist.github.com/cscorley/9144544
try:
from urllib.parse import quote # Py 3
except ImportError:
from urllib2 import quote # Py 2
import os
import sys
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 = ['/home/tdos/.ipython/templates'] # point this to your jekyll template file
c.MarkdownExporter.template_file = 'jekyll'
#c.Application.verbose_crash=True
# modify this function to point your images to a custom path
# by default this saves all images to a directory 'images' in the root of the blog directory
def path2support(path):
"""Turn a file path into a URL"""
return '{{ BASE_PATH }}/images/' + os.path.basename(path)
c.MarkdownExporter.filters = {'path2support': path2support}
if f:
c.NbConvertApp.output_base = f.lower().replace(' ', '-')
c.FilesWriter.build_directory = '/home/tdos/git/tgarc.github.io/notebooks' # point this to your build directory
{% extends 'markdown.tpl' %}
{%- block header -%}
---
layout: post
title: "{{resources['metadata']['name']}}"
tags:
- python
- notebook
---
{%- endblock header -%}
{% block in_prompt %}
**In [{{ cell.execution_count }}]:**
{% endblock in_prompt %}
{% block input %}
{{ '{% highlight python %}' }}
{{ cell.source }}
{{ '{% endhighlight %}' }}
{% endblock input %}
{% block data_svg %}
![svg]({{ output.metadata.filenames['image/svg+xml'] | path2support }})
{% endblock data_svg %}
{% block data_png %}
![png]({{ output.metadata.filenames['image/png'] | path2support }})
{% endblock data_png %}
{% block data_jpg %}
![jpeg]({{ output.metadata.filenames['image/jpeg'] | path2support }})
{% endblock data_jpg %}
{% block markdowncell scoped %}
{{ cell.source | wrap_text(80) }}
{% endblock markdowncell %}
{% block headingcell scoped %}
{{ '#' * cell.level }} {{ cell.source | replace('\n', ' ') }}
{% endblock headingcell %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment