Instantly share code, notes, and snippets.

What would you like to do?
Blogging with Jupyter Notebooks and Jekyll
# example use:
# [~/gitrepos/]$ ../scripts/ google_maps.ipynb
# use nbconvert on the file
ipython nbconvert --to markdown $1 --config
# copies the file to a newly named file
dt=`date +%Y-%m-%d`
mv $BUILD_DIR$md_fname $BUILD_DIR$fname
echo "file name changed from $1 to $fname"
# adds the date to the file
dt2=`date +"%b %d, %Y"`
sed -i "3i date: $dt2" $BUILD_DIR$fname
echo "added date $dt2 to line 3"
# Gets the title of the post
echo "What's the title of this post going to be?"
read ttl
sed -i "4i title: \"$ttl\"" $BUILD_DIR$fname
echo "added title $ttl in line 4"
# if the current version is newer than the version in _posts
if [[ $1 -nt $POST_DIR$fname ]]; then
mv $BUILD_DIR$fname $POST_DIR$fname
echo "moved $fname from $BUILD_DIR to $POST_DIR"
echo -e "\e[32m Process Completed Successfully \e[0m"
echo -e "\e[31m $1 older than the version in $POST_DIR, not overwriting $POST_DIR$fname \e[0m"
# modification of config created here:
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]
c = get_config()
c.NbConvertApp.export_format = 'markdown'
c.MarkdownExporter.template_path = ['/home/andrew/gitrepos/'] # point this to your jekyll template file
c.MarkdownExporter.template_file = 'jekyll'
# 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/andrew/gitrepos/' # point this to your build directory
{% extends 'markdown.tpl' %}
{%- block header -%}
layout: post
title: "{{resources['metadata']['name']}}"
categories: blog
author: Andrew Mehrmann
{%- endblock header -%}
{% 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 %}

This comment has been minimized.

Copy link

cgoliver commented Dec 5, 2016

This is a great tool thanks so much!

I tried it on my blog and it seems to be working quite nicely.

However, I am not seeing any syntax highlighting and it would be nice to have the code in something like grey boxes.

Here is a link to the notebook I used the script with:

Did it work as expected?



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