Last active
March 25, 2022 13:44
-
-
Save tgarc/7d6901858ef708030c19 to your computer and use it in GitHub Desktop.
Jekyll IPython notebook converter
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
ipython nbconvert --to markdown <notebook>.ipynb --config jekyll.py |
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/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 |
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']}}" | |
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