Last active
February 14, 2019 01:36
-
-
Save dkmehrmann/3fd9e8b89a6e442fdc8787a4c1dbf4f2 to your computer and use it in GitHub Desktop.
Blogging with Jupyter Notebooks and Jekyll
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
#!/bin/bash | |
# example use: | |
# [~/gitrepos/dkmehrmann.github.io/_ipynb]$ ../scripts/convert.sh google_maps.ipynb | |
# | |
BUILD_DIR="/home/andrew/gitrepos/dkmehrmann.github.io/_ipynb/" | |
POST_DIR="/home/andrew/gitrepos/dkmehrmann.github.io/_posts/" | |
# use nbconvert on the file | |
ipython nbconvert --to markdown $1 --config jekyll.py | |
# copies the file to a newly named file | |
ipynb_fname="$1" | |
md_fname="${ipynb_fname/ipynb/md}" | |
dt=`date +%Y-%m-%d` | |
fname="$dt-$md_fname" | |
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" | |
else | |
echo -e "\e[31m $1 older than the version in $POST_DIR, not overwriting $POST_DIR$fname \e[0m" | |
fi |
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
# 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/andrew/gitrepos/dkmehrmann.github.io/scripts'] # 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/andrew/gitrepos/dkmehrmann.github.io/_ipynb' # point this to your build directory |
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
{% 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 %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: http://cgoliver.com/blog/2016/12/05/Intro_to_Python
Did it work as expected?
Thanks!
Carlos